ProxyFactory.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Proxy;
  20. use Doctrine\ORM\EntityManager,
  21. Doctrine\ORM\Mapping\ClassMetadata,
  22. Doctrine\ORM\Mapping\AssociationMapping;
  23. /**
  24. * This factory is used to create proxy objects for entities at runtime.
  25. *
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  28. * @since 2.0
  29. */
  30. class ProxyFactory
  31. {
  32. /** The EntityManager this factory is bound to. */
  33. private $_em;
  34. /** Whether to automatically (re)generate proxy classes. */
  35. private $_autoGenerate;
  36. /** The namespace that contains all proxy classes. */
  37. private $_proxyNamespace;
  38. /** The directory that contains all proxy classes. */
  39. private $_proxyDir;
  40. /**
  41. * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  42. * connected to the given <tt>EntityManager</tt>.
  43. *
  44. * @param EntityManager $em The EntityManager the new factory works for.
  45. * @param string $proxyDir The directory to use for the proxy classes. It must exist.
  46. * @param string $proxyNs The namespace to use for the proxy classes.
  47. * @param boolean $autoGenerate Whether to automatically generate proxy classes.
  48. */
  49. public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false)
  50. {
  51. if ( ! $proxyDir) {
  52. throw ProxyException::proxyDirectoryRequired();
  53. }
  54. if ( ! $proxyNs) {
  55. throw ProxyException::proxyNamespaceRequired();
  56. }
  57. $this->_em = $em;
  58. $this->_proxyDir = $proxyDir;
  59. $this->_autoGenerate = $autoGenerate;
  60. $this->_proxyNamespace = $proxyNs;
  61. }
  62. /**
  63. * Gets a reference proxy instance for the entity of the given type and identified by
  64. * the given identifier.
  65. *
  66. * @param string $className
  67. * @param mixed $identifier
  68. * @return object
  69. */
  70. public function getProxy($className, $identifier)
  71. {
  72. $proxyClassName = str_replace('\\', '', $className) . 'Proxy';
  73. $fqn = $this->_proxyNamespace . '\\' . $proxyClassName;
  74. if (! class_exists($fqn, false)) {
  75. $fileName = $this->_proxyDir . DIRECTORY_SEPARATOR . $proxyClassName . '.php';
  76. if ($this->_autoGenerate) {
  77. $this->_generateProxyClass($this->_em->getClassMetadata($className), $proxyClassName, $fileName, self::$_proxyClassTemplate);
  78. }
  79. require $fileName;
  80. }
  81. if ( ! $this->_em->getMetadataFactory()->hasMetadataFor($fqn)) {
  82. $this->_em->getMetadataFactory()->setMetadataFor($fqn, $this->_em->getClassMetadata($className));
  83. }
  84. $entityPersister = $this->_em->getUnitOfWork()->getEntityPersister($className);
  85. return new $fqn($entityPersister, $identifier);
  86. }
  87. /**
  88. * Generates proxy classes for all given classes.
  89. *
  90. * @param array $classes The classes (ClassMetadata instances) for which to generate proxies.
  91. * @param string $toDir The target directory of the proxy classes. If not specified, the
  92. * directory configured on the Configuration of the EntityManager used
  93. * by this factory is used.
  94. */
  95. public function generateProxyClasses(array $classes, $toDir = null)
  96. {
  97. $proxyDir = $toDir ?: $this->_proxyDir;
  98. $proxyDir = rtrim($proxyDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  99. foreach ($classes as $class) {
  100. /* @var $class ClassMetadata */
  101. if ($class->isMappedSuperclass) {
  102. continue;
  103. }
  104. $proxyClassName = str_replace('\\', '', $class->name) . 'Proxy';
  105. $proxyFileName = $proxyDir . $proxyClassName . '.php';
  106. $this->_generateProxyClass($class, $proxyClassName, $proxyFileName, self::$_proxyClassTemplate);
  107. }
  108. }
  109. /**
  110. * Generates a proxy class file.
  111. *
  112. * @param $class
  113. * @param $originalClassName
  114. * @param $proxyClassName
  115. * @param $file The path of the file to write to.
  116. */
  117. private function _generateProxyClass($class, $proxyClassName, $fileName, $file)
  118. {
  119. $methods = $this->_generateMethods($class);
  120. $sleepImpl = $this->_generateSleep($class);
  121. $cloneImpl = $class->reflClass->hasMethod('__clone') ? 'parent::__clone();' : ''; // hasMethod() checks case-insensitive
  122. $placeholders = array(
  123. '<namespace>',
  124. '<proxyClassName>', '<className>',
  125. '<methods>', '<sleepImpl>', '<cloneImpl>'
  126. );
  127. if(substr($class->name, 0, 1) == "\\") {
  128. $className = substr($class->name, 1);
  129. } else {
  130. $className = $class->name;
  131. }
  132. $replacements = array(
  133. $this->_proxyNamespace,
  134. $proxyClassName, $className,
  135. $methods, $sleepImpl, $cloneImpl
  136. );
  137. $file = str_replace($placeholders, $replacements, $file);
  138. file_put_contents($fileName, $file, LOCK_EX);
  139. }
  140. /**
  141. * Generates the methods of a proxy class.
  142. *
  143. * @param ClassMetadata $class
  144. * @return string The code of the generated methods.
  145. */
  146. private function _generateMethods(ClassMetadata $class)
  147. {
  148. $methods = '';
  149. $methodNames = array();
  150. foreach ($class->reflClass->getMethods() as $method) {
  151. /* @var $method ReflectionMethod */
  152. if ($method->isConstructor() || in_array(strtolower($method->getName()), array("__sleep", "__clone")) || isset($methodNames[$method->getName()])) {
  153. continue;
  154. }
  155. $methodNames[$method->getName()] = true;
  156. if ($method->isPublic() && ! $method->isFinal() && ! $method->isStatic()) {
  157. $methods .= "\n" . ' public function ';
  158. if ($method->returnsReference()) {
  159. $methods .= '&';
  160. }
  161. $methods .= $method->getName() . '(';
  162. $firstParam = true;
  163. $parameterString = $argumentString = '';
  164. foreach ($method->getParameters() as $param) {
  165. if ($firstParam) {
  166. $firstParam = false;
  167. } else {
  168. $parameterString .= ', ';
  169. $argumentString .= ', ';
  170. }
  171. // We need to pick the type hint class too
  172. if (($paramClass = $param->getClass()) !== null) {
  173. $parameterString .= '\\' . $paramClass->getName() . ' ';
  174. } else if ($param->isArray()) {
  175. $parameterString .= 'array ';
  176. }
  177. if ($param->isPassedByReference()) {
  178. $parameterString .= '&';
  179. }
  180. $parameterString .= '$' . $param->getName();
  181. $argumentString .= '$' . $param->getName();
  182. if ($param->isDefaultValueAvailable()) {
  183. $parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
  184. }
  185. }
  186. $methods .= $parameterString . ')';
  187. $methods .= "\n" . ' {' . "\n";
  188. $methods .= ' $this->__load();' . "\n";
  189. $methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
  190. $methods .= "\n" . ' }' . "\n";
  191. }
  192. }
  193. return $methods;
  194. }
  195. /**
  196. * Generates the code for the __sleep method for a proxy class.
  197. *
  198. * @param $class
  199. * @return string
  200. */
  201. private function _generateSleep(ClassMetadata $class)
  202. {
  203. $sleepImpl = '';
  204. if ($class->reflClass->hasMethod('__sleep')) {
  205. $sleepImpl .= "return array_merge(array('__isInitialized__'), parent::__sleep());";
  206. } else {
  207. $sleepImpl .= "return array('__isInitialized__', ";
  208. $first = true;
  209. foreach ($class->getReflectionProperties() as $name => $prop) {
  210. if ($first) {
  211. $first = false;
  212. } else {
  213. $sleepImpl .= ', ';
  214. }
  215. $sleepImpl .= "'" . $name . "'";
  216. }
  217. $sleepImpl .= ');';
  218. }
  219. return $sleepImpl;
  220. }
  221. /** Proxy class code template */
  222. private static $_proxyClassTemplate =
  223. '<?php
  224. namespace <namespace>;
  225. /**
  226. * THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
  227. */
  228. class <proxyClassName> extends \<className> implements \Doctrine\ORM\Proxy\Proxy
  229. {
  230. private $_entityPersister;
  231. private $_identifier;
  232. public $__isInitialized__ = false;
  233. public function __construct($entityPersister, $identifier)
  234. {
  235. $this->_entityPersister = $entityPersister;
  236. $this->_identifier = $identifier;
  237. }
  238. /** @private */
  239. public function __load()
  240. {
  241. if (!$this->__isInitialized__ && $this->_entityPersister) {
  242. $this->__isInitialized__ = true;
  243. if (method_exists($this, "__wakeup")) {
  244. // call this after __isInitialized__to avoid infinite recursion
  245. // but before loading to emulate what ClassMetadata::newInstance()
  246. // provides.
  247. $this->__wakeup();
  248. }
  249. if ($this->_entityPersister->load($this->_identifier, $this) === null) {
  250. throw new \Doctrine\ORM\EntityNotFoundException();
  251. }
  252. unset($this->_entityPersister, $this->_identifier);
  253. }
  254. }
  255. <methods>
  256. public function __sleep()
  257. {
  258. <sleepImpl>
  259. }
  260. public function __clone()
  261. {
  262. if (!$this->__isInitialized__ && $this->_entityPersister) {
  263. $this->__isInitialized__ = true;
  264. $class = $this->_entityPersister->getClassMetadata();
  265. $original = $this->_entityPersister->load($this->_identifier);
  266. if ($original === null) {
  267. throw new \Doctrine\ORM\EntityNotFoundException();
  268. }
  269. foreach ($class->reflFields AS $field => $reflProperty) {
  270. $reflProperty->setValue($this, $reflProperty->getValue($original));
  271. }
  272. unset($this->_entityPersister, $this->_identifier);
  273. }
  274. <cloneImpl>
  275. }
  276. }';
  277. }