2009-05-11 14:43:27 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
2009-07-16 17:29:15 +04:00
|
|
|
namespace Doctrine\ORM\DynamicProxy;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
2009-05-21 23:18:14 +04:00
|
|
|
* The DynamicProxyGenerator is used to generate proxy objects for entities at runtime.
|
2009-05-11 14:43:27 +04:00
|
|
|
*
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-05-13 19:19:27 +04:00
|
|
|
* @since 2.0
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
2009-07-16 17:29:15 +04:00
|
|
|
class Generator
|
2009-05-11 14:43:27 +04:00
|
|
|
{
|
2009-07-16 17:29:15 +04:00
|
|
|
/** The namespace for the generated proxy classes. */
|
2009-05-13 19:19:27 +04:00
|
|
|
private static $_ns = 'Doctrine\Generated\Proxies\\';
|
2009-05-21 23:18:14 +04:00
|
|
|
private $_cacheDir;
|
2009-05-11 14:43:27 +04:00
|
|
|
private $_em;
|
2009-07-16 17:29:15 +04:00
|
|
|
|
2009-05-21 23:18:14 +04:00
|
|
|
/**
|
2009-07-16 17:29:15 +04:00
|
|
|
* Generates and stores proxy class files in the given cache directory.
|
2009-05-21 23:18:14 +04:00
|
|
|
*
|
|
|
|
* @param EntityManager $em
|
|
|
|
* @param string $cacheDir
|
|
|
|
*/
|
2009-05-11 14:43:27 +04:00
|
|
|
public function __construct(EntityManager $em, $cacheDir = null)
|
|
|
|
{
|
|
|
|
$this->_em = $em;
|
|
|
|
if ($cacheDir === null) {
|
2009-05-13 19:19:27 +04:00
|
|
|
$cacheDir = sys_get_temp_dir();
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-07-16 17:29:15 +04:00
|
|
|
$this->_cacheDir = rtrim($cacheDir, '/') . '/';
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-16 17:29:15 +04:00
|
|
|
* Generates a reference proxy class.
|
|
|
|
* This is a proxy for an object which we have the id for retrieval.
|
2009-05-11 14:43:27 +04:00
|
|
|
*
|
2009-05-13 19:19:27 +04:00
|
|
|
* @param string $className
|
2009-07-16 17:29:15 +04:00
|
|
|
* @param string $proxyClassName
|
|
|
|
* @param string $fileName
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
2009-07-16 17:29:15 +04:00
|
|
|
public function generateReferenceProxyClass($className)
|
2009-05-11 14:43:27 +04:00
|
|
|
{
|
2009-05-13 19:19:27 +04:00
|
|
|
$class = $this->_em->getClassMetadata($className);
|
|
|
|
$proxyClassName = str_replace('\\', '_', $className) . 'RProxy';
|
2009-07-16 17:29:15 +04:00
|
|
|
if (!class_exists($proxyClassName, false)) {
|
2009-05-13 19:19:27 +04:00
|
|
|
$this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $class);
|
|
|
|
$fileName = $this->_cacheDir . $proxyClassName . '.g.php';
|
2009-07-16 17:29:15 +04:00
|
|
|
if (file_exists($fileName)) {
|
|
|
|
require $fileName;
|
|
|
|
$proxyClassName = '\\' . self::$_ns . $proxyClassName;
|
|
|
|
return $proxyClassName;
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-16 17:29:15 +04:00
|
|
|
$file = self::$_proxyClassTemplate;
|
|
|
|
$methods = $this->_generateMethods($class);
|
|
|
|
$sleepImpl = $this->_generateSleep($class);
|
|
|
|
|
|
|
|
$placeholders = array(
|
|
|
|
'<proxyClassName>', '<className>',
|
|
|
|
'<methods>', '<sleepImpl>'
|
|
|
|
);
|
|
|
|
$replacements = array(
|
|
|
|
$proxyClassName, $className, $methods, $sleepImpl
|
|
|
|
);
|
|
|
|
|
|
|
|
$file = str_replace($placeholders, $replacements, $file);
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-07-16 17:29:15 +04:00
|
|
|
file_put_contents($fileName, $file);
|
|
|
|
require $fileName;
|
|
|
|
$proxyClassName = '\\' . self::$_ns . $proxyClassName;
|
|
|
|
return $proxyClassName;
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:29:15 +04:00
|
|
|
protected function _generateMethods(ClassMetadata $class)
|
2009-05-11 14:43:27 +04:00
|
|
|
{
|
2009-05-13 19:19:27 +04:00
|
|
|
$methods = '';
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->reflClass->getMethods() as $method) {
|
2009-07-16 17:29:15 +04:00
|
|
|
if ($method->getName() == '__construct') {
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
if ($method->isPublic() && ! $method->isFinal()) {
|
|
|
|
$methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
|
|
|
|
$firstParam = true;
|
|
|
|
$parameterString = '';
|
|
|
|
foreach ($method->getParameters() as $param) {
|
|
|
|
if ($firstParam) {
|
|
|
|
$firstParam = false;
|
|
|
|
} else {
|
|
|
|
$parameterString .= ', ';
|
|
|
|
}
|
|
|
|
$parameterString .= '$' . $param->getName();
|
|
|
|
}
|
|
|
|
$methods .= $parameterString . ') {' . PHP_EOL;
|
|
|
|
$methods .= '$this->_load();' . PHP_EOL;
|
|
|
|
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');';
|
|
|
|
$methods .= '}' . PHP_EOL;
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
}
|
2009-07-16 17:29:15 +04:00
|
|
|
return $methods;
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
2009-07-16 17:29:15 +04:00
|
|
|
public function _generateSleep(ClassMetadata $class)
|
|
|
|
{
|
2009-05-13 19:19:27 +04:00
|
|
|
$sleepImpl = '';
|
2009-05-14 14:03:09 +04:00
|
|
|
if ($class->reflClass->hasMethod('__sleep')) {
|
2009-05-13 19:19:27 +04:00
|
|
|
$sleepImpl .= 'return parent::__sleep();';
|
|
|
|
} else {
|
|
|
|
$sleepImpl .= 'return array(';
|
|
|
|
$first = true;
|
|
|
|
foreach ($class->getReflectionProperties() as $name => $prop) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
$sleepImpl .= ', ';
|
|
|
|
}
|
|
|
|
$sleepImpl .= "'" . $name . "'";
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
$sleepImpl .= ');';
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2009-07-16 17:29:15 +04:00
|
|
|
return $sleepImpl;
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a proxy class.
|
2009-07-16 17:29:15 +04:00
|
|
|
* This is a proxy class for an object which we have the association where
|
|
|
|
* it is involved, but no primary key to retrieve it.
|
2009-05-13 19:19:27 +04:00
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param string $proxyClassName
|
|
|
|
*/
|
2009-07-16 17:29:15 +04:00
|
|
|
public function generateAssociationProxyClass($className, $proxyClassName)
|
2009-05-13 19:19:27 +04:00
|
|
|
{
|
|
|
|
$class = $this->_em->getClassMetadata($className);
|
|
|
|
$file = self::$_assocProxyClassTemplate;
|
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
$methods = '';
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->reflClass->getMethods() as $method) {
|
2009-05-11 14:43:27 +04:00
|
|
|
if ($method->isPublic() && ! $method->isFinal()) {
|
|
|
|
$methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
|
|
|
|
$firstParam = true;
|
|
|
|
$parameterString = '';
|
|
|
|
foreach ($method->getParameters() as $param) {
|
|
|
|
if ($firstParam) {
|
|
|
|
$firstParam = false;
|
|
|
|
} else {
|
|
|
|
$parameterString .= ', ';
|
|
|
|
}
|
|
|
|
$parameterString .= '$' . $param->getName();
|
|
|
|
}
|
|
|
|
$methods .= $parameterString . ') {' . PHP_EOL;
|
|
|
|
$methods .= '$this->_load();' . PHP_EOL;
|
|
|
|
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');';
|
|
|
|
$methods .= '}' . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sleepImpl = '';
|
2009-05-14 14:03:09 +04:00
|
|
|
if ($class->reflClass->hasMethod('__sleep')) {
|
2009-05-11 14:43:27 +04:00
|
|
|
$sleepImpl .= 'return parent::__sleep();';
|
|
|
|
} else {
|
|
|
|
$sleepImpl .= 'return array(';
|
|
|
|
$first = true;
|
|
|
|
foreach ($class->getReflectionProperties() as $name => $prop) {
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
$sleepImpl .= ', ';
|
|
|
|
}
|
|
|
|
$sleepImpl .= "'" . $name . "'";
|
|
|
|
}
|
|
|
|
$sleepImpl .= ');';
|
|
|
|
}
|
|
|
|
|
|
|
|
$placeholders = array(
|
2009-05-13 19:19:27 +04:00
|
|
|
'<proxyClassName>', '<className>',
|
|
|
|
'<methods>', '<sleepImpl>'
|
2009-05-11 14:43:27 +04:00
|
|
|
);
|
|
|
|
$replacements = array(
|
2009-05-13 19:19:27 +04:00
|
|
|
$proxyClassName, $className, $methods, $sleepImpl
|
2009-05-11 14:43:27 +04:00
|
|
|
);
|
2009-05-13 19:19:27 +04:00
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
$file = str_replace($placeholders, $replacements, $file);
|
2009-05-13 19:19:27 +04:00
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
file_put_contents($fileName, $file);
|
2009-07-16 17:29:15 +04:00
|
|
|
return $fileName;
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Proxy class code template */
|
|
|
|
private static $_proxyClassTemplate =
|
|
|
|
'<?php
|
|
|
|
/** This class was generated by the Doctrine ORM. DO NOT EDIT THIS FILE. */
|
|
|
|
namespace Doctrine\Generated\Proxies {
|
|
|
|
class <proxyClassName> extends \<className> {
|
2009-07-16 17:29:15 +04:00
|
|
|
private $_entityPersister;
|
|
|
|
private $_identifier;
|
2009-05-11 14:43:27 +04:00
|
|
|
private $_loaded = false;
|
2009-07-16 17:29:15 +04:00
|
|
|
public function __construct($entityPersister, $identifier) {
|
|
|
|
$this->_entityPersister = $entityPersister;
|
|
|
|
$this->_identifier = $identifier;
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
|
|
|
private function _load() {
|
|
|
|
if ( ! $this->_loaded) {
|
2009-07-16 17:29:15 +04:00
|
|
|
$this->_entityPersister->load($this->_identifier, $this);
|
|
|
|
unset($this->_entityPersister);
|
|
|
|
unset($this->_identifier);
|
2009-05-11 14:43:27 +04:00
|
|
|
$this->_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<methods>
|
|
|
|
|
|
|
|
public function __sleep() {
|
|
|
|
if (!$this->_loaded) {
|
|
|
|
throw new RuntimeException("Not fully loaded proxy can not be serialized.");
|
|
|
|
}
|
|
|
|
<sleepImpl>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
2009-05-13 19:19:27 +04:00
|
|
|
|
|
|
|
private static $_assocProxyClassTemplate =
|
|
|
|
'<?php
|
|
|
|
/** This class was generated by the Doctrine ORM. DO NOT EDIT THIS FILE. */
|
|
|
|
namespace Doctrine\Generated\Proxies {
|
|
|
|
class <proxyClassName> extends \<className> {
|
|
|
|
private $_em;
|
|
|
|
private $_assoc;
|
|
|
|
private $_owner;
|
|
|
|
private $_loaded = false;
|
|
|
|
public function __construct($em, $assoc, $owner) {
|
|
|
|
$this->_em = $em;
|
|
|
|
$this->_assoc = $assoc;
|
|
|
|
$this->_owner = $owner;
|
|
|
|
}
|
|
|
|
private function _load() {
|
|
|
|
if ( ! $this->_loaded) {
|
|
|
|
$this->_assoc->load($this->_owner, $this, $this->_em);
|
|
|
|
unset($this->_em);
|
|
|
|
unset($this->_owner);
|
|
|
|
unset($this->_assoc);
|
|
|
|
$this->_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<methods>
|
|
|
|
|
|
|
|
public function __sleep() {
|
|
|
|
if (!$this->_loaded) {
|
|
|
|
throw new RuntimeException("Not fully loaded proxy can not be serialized.");
|
|
|
|
}
|
|
|
|
<sleepImpl>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}';
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|