123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
-
-
- namespace Doctrine\ORM\Proxy;
-
- use Doctrine\ORM\EntityManager,
- Doctrine\ORM\Mapping\ClassMetadata,
- Doctrine\ORM\Mapping\AssociationMapping;
-
-
- class ProxyFactory
- {
-
- private $_em;
-
- private $_autoGenerate;
-
- private $_proxyNamespace;
-
- private $_proxyDir;
-
-
-
- public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false)
- {
- if ( ! $proxyDir) {
- throw ProxyException::proxyDirectoryRequired();
- }
- if ( ! $proxyNs) {
- throw ProxyException::proxyNamespaceRequired();
- }
- $this->_em = $em;
- $this->_proxyDir = $proxyDir;
- $this->_autoGenerate = $autoGenerate;
- $this->_proxyNamespace = $proxyNs;
- }
-
-
-
- public function getProxy($className, $identifier)
- {
- $proxyClassName = str_replace('\\', '', $className) . 'Proxy';
- $fqn = $this->_proxyNamespace . '\\' . $proxyClassName;
-
- if (! class_exists($fqn, false)) {
- $fileName = $this->_proxyDir . DIRECTORY_SEPARATOR . $proxyClassName . '.php';
- if ($this->_autoGenerate) {
- $this->_generateProxyClass($this->_em->getClassMetadata($className), $proxyClassName, $fileName, self::$_proxyClassTemplate);
- }
- require $fileName;
- }
-
- if ( ! $this->_em->getMetadataFactory()->hasMetadataFor($fqn)) {
- $this->_em->getMetadataFactory()->setMetadataFor($fqn, $this->_em->getClassMetadata($className));
- }
-
- $entityPersister = $this->_em->getUnitOfWork()->getEntityPersister($className);
-
- return new $fqn($entityPersister, $identifier);
- }
-
-
-
- public function generateProxyClasses(array $classes, $toDir = null)
- {
- $proxyDir = $toDir ?: $this->_proxyDir;
- $proxyDir = rtrim($proxyDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
- foreach ($classes as $class) {
-
- if ($class->isMappedSuperclass) {
- continue;
- }
-
- $proxyClassName = str_replace('\\', '', $class->name) . 'Proxy';
- $proxyFileName = $proxyDir . $proxyClassName . '.php';
- $this->_generateProxyClass($class, $proxyClassName, $proxyFileName, self::$_proxyClassTemplate);
- }
- }
-
-
-
- private function _generateProxyClass($class, $proxyClassName, $fileName, $file)
- {
- $methods = $this->_generateMethods($class);
- $sleepImpl = $this->_generateSleep($class);
- $cloneImpl = $class->reflClass->hasMethod('__clone') ? 'parent::__clone();' : '';
-
- $placeholders = array(
- '<namespace>',
- '<proxyClassName>', '<className>',
- '<methods>', '<sleepImpl>', '<cloneImpl>'
- );
-
- if(substr($class->name, 0, 1) == "\\") {
- $className = substr($class->name, 1);
- } else {
- $className = $class->name;
- }
-
- $replacements = array(
- $this->_proxyNamespace,
- $proxyClassName, $className,
- $methods, $sleepImpl, $cloneImpl
- );
-
- $file = str_replace($placeholders, $replacements, $file);
-
- file_put_contents($fileName, $file, LOCK_EX);
- }
-
-
-
- private function _generateMethods(ClassMetadata $class)
- {
- $methods = '';
-
- foreach ($class->reflClass->getMethods() as $method) {
-
- if ($method->isConstructor() || in_array(strtolower($method->getName()), array("__sleep", "__clone"))) {
- continue;
- }
-
- if ($method->isPublic() && ! $method->isFinal() && ! $method->isStatic()) {
- $methods .= "\n" . ' public function ';
- if ($method->returnsReference()) {
- $methods .= '&';
- }
- $methods .= $method->getName() . '(';
- $firstParam = true;
- $parameterString = $argumentString = '';
-
- foreach ($method->getParameters() as $param) {
- if ($firstParam) {
- $firstParam = false;
- } else {
- $parameterString .= ', ';
- $argumentString .= ', ';
- }
-
-
- if (($paramClass = $param->getClass()) !== null) {
- $parameterString .= '\\' . $paramClass->getName() . ' ';
- } else if ($param->isArray()) {
- $parameterString .= 'array ';
- }
-
- if ($param->isPassedByReference()) {
- $parameterString .= '&';
- }
-
- $parameterString .= '$' . $param->getName();
- $argumentString .= '$' . $param->getName();
-
- if ($param->isDefaultValueAvailable()) {
- $parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
- }
- }
-
- $methods .= $parameterString . ')';
- $methods .= "\n" . ' {' . "\n";
- $methods .= ' $this->__load();' . "\n";
- $methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
- $methods .= "\n" . ' }' . "\n";
- }
- }
-
- return $methods;
- }
-
-
-
- private function _generateSleep(ClassMetadata $class)
- {
- $sleepImpl = '';
-
- if ($class->reflClass->hasMethod('__sleep')) {
- $sleepImpl .= "return array_merge(array('__isInitialized__'), parent::__sleep());";
- } else {
- $sleepImpl .= "return array('__isInitialized__', ";
- $first = true;
-
- foreach ($class->getReflectionProperties() as $name => $prop) {
- if ($first) {
- $first = false;
- } else {
- $sleepImpl .= ', ';
- }
-
- $sleepImpl .= "'" . $name . "'";
- }
-
- $sleepImpl .= ');';
- }
-
- return $sleepImpl;
- }
-
-
- private static $_proxyClassTemplate =
- '<?php
-
- namespace <namespace>;
-
- /**
- * THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
- */
- class <proxyClassName> extends \<className> implements \Doctrine\ORM\Proxy\Proxy
- {
- private $_entityPersister;
- private $_identifier;
- public $__isInitialized__ = false;
- public function __construct($entityPersister, $identifier)
- {
- $this->_entityPersister = $entityPersister;
- $this->_identifier = $identifier;
- }
- /** @private */
- public function __load()
- {
- if (!$this->__isInitialized__ && $this->_entityPersister) {
- $this->__isInitialized__ = true;
-
- if (method_exists($this, "__wakeup")) {
- // call this after __isInitialized__to avoid infinite recursion
- // but before loading to emulate what ClassMetadata::newInstance()
- // provides.
- $this->__wakeup();
- }
-
- if ($this->_entityPersister->load($this->_identifier, $this) === null) {
- throw new \Doctrine\ORM\EntityNotFoundException();
- }
- unset($this->_entityPersister, $this->_identifier);
- }
- }
-
- <methods>
-
- public function __sleep()
- {
- <sleepImpl>
- }
-
- public function __clone()
- {
- if (!$this->__isInitialized__ && $this->_entityPersister) {
- $this->__isInitialized__ = true;
- $class = $this->_entityPersister->getClassMetadata();
- $original = $this->_entityPersister->load($this->_identifier);
- if ($original === null) {
- throw new \Doctrine\ORM\EntityNotFoundException();
- }
- foreach ($class->reflFields AS $field => $reflProperty) {
- $reflProperty->setValue($this, $reflProperty->getValue($original));
- }
- unset($this->_entityPersister, $this->_identifier);
- }
- <cloneImpl>
- }
- }';
- }
|