ProxyFactory.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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\Common\Util\ClassUtils;
  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. * Used to match very simple id methods that don't need
  42. * to be proxied since the identifier is known.
  43. *
  44. * @var string
  45. */
  46. const PATTERN_MATCH_ID_METHOD = '((public\s)?(function\s{1,}%s\s?\(\)\s{1,})\s{0,}{\s{0,}return\s{0,}\$this->%s;\s{0,}})i';
  47. /**
  48. * Initializes a new instance of the <tt>ProxyFactory</tt> class that is
  49. * connected to the given <tt>EntityManager</tt>.
  50. *
  51. * @param EntityManager $em The EntityManager the new factory works for.
  52. * @param string $proxyDir The directory to use for the proxy classes. It must exist.
  53. * @param string $proxyNs The namespace to use for the proxy classes.
  54. * @param boolean $autoGenerate Whether to automatically generate proxy classes.
  55. */
  56. public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false)
  57. {
  58. if ( ! $proxyDir) {
  59. throw ProxyException::proxyDirectoryRequired();
  60. }
  61. if ( ! $proxyNs) {
  62. throw ProxyException::proxyNamespaceRequired();
  63. }
  64. $this->_em = $em;
  65. $this->_proxyDir = $proxyDir;
  66. $this->_autoGenerate = $autoGenerate;
  67. $this->_proxyNamespace = $proxyNs;
  68. }
  69. /**
  70. * Gets a reference proxy instance for the entity of the given type and identified by
  71. * the given identifier.
  72. *
  73. * @param string $className
  74. * @param mixed $identifier
  75. * @return object
  76. */
  77. public function getProxy($className, $identifier)
  78. {
  79. $fqn = ClassUtils::generateProxyClassName($className, $this->_proxyNamespace);
  80. if (! class_exists($fqn, false)) {
  81. $fileName = $this->getProxyFileName($className);
  82. if ($this->_autoGenerate) {
  83. $this->_generateProxyClass($this->_em->getClassMetadata($className), $fileName, self::$_proxyClassTemplate);
  84. }
  85. require $fileName;
  86. }
  87. if ( ! $this->_em->getMetadataFactory()->hasMetadataFor($fqn)) {
  88. $this->_em->getMetadataFactory()->setMetadataFor($fqn, $this->_em->getClassMetadata($className));
  89. }
  90. $entityPersister = $this->_em->getUnitOfWork()->getEntityPersister($className);
  91. return new $fqn($entityPersister, $identifier);
  92. }
  93. /**
  94. * Generate the Proxy file name
  95. *
  96. * @param string $className
  97. * @param string $baseDir Optional base directory for proxy file name generation.
  98. * If not specified, the directory configured on the Configuration of the
  99. * EntityManager will be used by this factory.
  100. * @return string
  101. */
  102. private function getProxyFileName($className, $baseDir = null)
  103. {
  104. $proxyDir = $baseDir ?: $this->_proxyDir;
  105. return $proxyDir . DIRECTORY_SEPARATOR . '__CG__' . str_replace('\\', '', $className) . '.php';
  106. }
  107. /**
  108. * Generates proxy classes for all given classes.
  109. *
  110. * @param array $classes The classes (ClassMetadata instances) for which to generate proxies.
  111. * @param string $toDir The target directory of the proxy classes. If not specified, the
  112. * directory configured on the Configuration of the EntityManager used
  113. * by this factory is used.
  114. * @return int Number of generated proxies.
  115. */
  116. public function generateProxyClasses(array $classes, $toDir = null)
  117. {
  118. $proxyDir = $toDir ?: $this->_proxyDir;
  119. $proxyDir = rtrim($proxyDir, DIRECTORY_SEPARATOR);
  120. $num = 0;
  121. foreach ($classes as $class) {
  122. /* @var $class ClassMetadata */
  123. if ($class->isMappedSuperclass || $class->reflClass->isAbstract()) {
  124. continue;
  125. }
  126. $proxyFileName = $this->getProxyFileName($class->name, $proxyDir);
  127. $this->_generateProxyClass($class, $proxyFileName, self::$_proxyClassTemplate);
  128. $num++;
  129. }
  130. return $num;
  131. }
  132. /**
  133. * Generates a proxy class file.
  134. *
  135. * @param $class
  136. * @param $proxyClassName
  137. * @param $file The path of the file to write to.
  138. */
  139. private function _generateProxyClass($class, $fileName, $file)
  140. {
  141. $methods = $this->_generateMethods($class);
  142. $sleepImpl = $this->_generateSleep($class);
  143. $cloneImpl = $class->reflClass->hasMethod('__clone') ? 'parent::__clone();' : ''; // hasMethod() checks case-insensitive
  144. $placeholders = array(
  145. '<namespace>',
  146. '<proxyClassName>', '<className>',
  147. '<methods>', '<sleepImpl>', '<cloneImpl>'
  148. );
  149. $className = ltrim($class->name, '\\');
  150. $proxyClassName = ClassUtils::generateProxyClassName($class->name, $this->_proxyNamespace);
  151. $parts = explode('\\', strrev($proxyClassName), 2);
  152. $proxyClassNamespace = strrev($parts[1]);
  153. $proxyClassName = strrev($parts[0]);
  154. $replacements = array(
  155. $proxyClassNamespace,
  156. $proxyClassName,
  157. $className,
  158. $methods,
  159. $sleepImpl,
  160. $cloneImpl
  161. );
  162. $file = str_replace($placeholders, $replacements, $file);
  163. $parentDirectory = dirname($fileName);
  164. if ( ! is_dir($parentDirectory)) {
  165. if (false === @mkdir($parentDirectory, 0775, true)) {
  166. throw ProxyException::proxyDirectoryNotWritable();
  167. }
  168. } else if ( ! is_writable($parentDirectory)) {
  169. throw ProxyException::proxyDirectoryNotWritable();
  170. }
  171. $tmpFileName = $fileName . '.' . uniqid("", true);
  172. file_put_contents($tmpFileName, $file);
  173. rename($tmpFileName, $fileName);
  174. }
  175. /**
  176. * Generates the methods of a proxy class.
  177. *
  178. * @param ClassMetadata $class
  179. * @return string The code of the generated methods.
  180. */
  181. private function _generateMethods(ClassMetadata $class)
  182. {
  183. $methods = '';
  184. $methodNames = array();
  185. foreach ($class->reflClass->getMethods() as $method) {
  186. /* @var $method ReflectionMethod */
  187. if ($method->isConstructor() || in_array(strtolower($method->getName()), array("__sleep", "__clone")) || isset($methodNames[$method->getName()])) {
  188. continue;
  189. }
  190. $methodNames[$method->getName()] = true;
  191. if ($method->isPublic() && ! $method->isFinal() && ! $method->isStatic()) {
  192. $methods .= "\n" . ' public function ';
  193. if ($method->returnsReference()) {
  194. $methods .= '&';
  195. }
  196. $methods .= $method->getName() . '(';
  197. $firstParam = true;
  198. $parameterString = $argumentString = '';
  199. foreach ($method->getParameters() as $param) {
  200. if ($firstParam) {
  201. $firstParam = false;
  202. } else {
  203. $parameterString .= ', ';
  204. $argumentString .= ', ';
  205. }
  206. // We need to pick the type hint class too
  207. if (($paramClass = $param->getClass()) !== null) {
  208. $parameterString .= '\\' . $paramClass->getName() . ' ';
  209. } else if ($param->isArray()) {
  210. $parameterString .= 'array ';
  211. }
  212. if ($param->isPassedByReference()) {
  213. $parameterString .= '&';
  214. }
  215. $parameterString .= '$' . $param->getName();
  216. $argumentString .= '$' . $param->getName();
  217. if ($param->isDefaultValueAvailable()) {
  218. $parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
  219. }
  220. }
  221. $methods .= $parameterString . ')';
  222. $methods .= "\n" . ' {' . "\n";
  223. if ($this->isShortIdentifierGetter($method, $class)) {
  224. $identifier = lcfirst(substr($method->getName(), 3));
  225. $cast = in_array($class->fieldMappings[$identifier]['type'], array('integer', 'smallint')) ? '(int) ' : '';
  226. $methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
  227. $methods .= ' return ' . $cast . '$this->_identifier["' . $identifier . '"];' . "\n";
  228. $methods .= ' }' . "\n";
  229. }
  230. $methods .= ' $this->__load();' . "\n";
  231. $methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
  232. $methods .= "\n" . ' }' . "\n";
  233. }
  234. }
  235. return $methods;
  236. }
  237. /**
  238. * Check if the method is a short identifier getter.
  239. *
  240. * What does this mean? For proxy objects the identifier is already known,
  241. * however accessing the getter for this identifier usually triggers the
  242. * lazy loading, leading to a query that may not be necessary if only the
  243. * ID is interesting for the userland code (for example in views that
  244. * generate links to the entity, but do not display anything else).
  245. *
  246. * @param ReflectionMethod $method
  247. * @param ClassMetadata $class
  248. * @return bool
  249. */
  250. private function isShortIdentifierGetter($method, $class)
  251. {
  252. $identifier = lcfirst(substr($method->getName(), 3));
  253. $cheapCheck = (
  254. $method->getNumberOfParameters() == 0 &&
  255. substr($method->getName(), 0, 3) == "get" &&
  256. in_array($identifier, $class->identifier, true) &&
  257. $class->hasField($identifier) &&
  258. (($method->getEndLine() - $method->getStartLine()) <= 4)
  259. && in_array($class->fieldMappings[$identifier]['type'], array('integer', 'bigint', 'smallint', 'string'))
  260. );
  261. if ($cheapCheck) {
  262. $code = file($method->getDeclaringClass()->getFileName());
  263. $code = trim(implode(" ", array_slice($code, $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1)));
  264. $pattern = sprintf(self::PATTERN_MATCH_ID_METHOD, $method->getName(), $identifier);
  265. if (preg_match($pattern, $code)) {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * Generates the code for the __sleep method for a proxy class.
  273. *
  274. * @param $class
  275. * @return string
  276. */
  277. private function _generateSleep(ClassMetadata $class)
  278. {
  279. $sleepImpl = '';
  280. if ($class->reflClass->hasMethod('__sleep')) {
  281. $sleepImpl .= "return array_merge(array('__isInitialized__'), parent::__sleep());";
  282. } else {
  283. $sleepImpl .= "return array('__isInitialized__', ";
  284. $first = true;
  285. foreach ($class->getReflectionProperties() as $name => $prop) {
  286. if ($first) {
  287. $first = false;
  288. } else {
  289. $sleepImpl .= ', ';
  290. }
  291. $sleepImpl .= "'" . $name . "'";
  292. }
  293. $sleepImpl .= ');';
  294. }
  295. return $sleepImpl;
  296. }
  297. /** Proxy class code template */
  298. private static $_proxyClassTemplate =
  299. '<?php
  300. namespace <namespace>;
  301. /**
  302. * THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
  303. */
  304. class <proxyClassName> extends \<className> implements \Doctrine\ORM\Proxy\Proxy
  305. {
  306. private $_entityPersister;
  307. private $_identifier;
  308. public $__isInitialized__ = false;
  309. public function __construct($entityPersister, $identifier)
  310. {
  311. $this->_entityPersister = $entityPersister;
  312. $this->_identifier = $identifier;
  313. }
  314. /** @private */
  315. public function __load()
  316. {
  317. if (!$this->__isInitialized__ && $this->_entityPersister) {
  318. $this->__isInitialized__ = true;
  319. if (method_exists($this, "__wakeup")) {
  320. // call this after __isInitialized__to avoid infinite recursion
  321. // but before loading to emulate what ClassMetadata::newInstance()
  322. // provides.
  323. $this->__wakeup();
  324. }
  325. if ($this->_entityPersister->load($this->_identifier, $this) === null) {
  326. throw new \Doctrine\ORM\EntityNotFoundException();
  327. }
  328. unset($this->_entityPersister, $this->_identifier);
  329. }
  330. }
  331. /** @private */
  332. public function __isInitialized()
  333. {
  334. return $this->__isInitialized__;
  335. }
  336. <methods>
  337. public function __sleep()
  338. {
  339. <sleepImpl>
  340. }
  341. public function __clone()
  342. {
  343. if (!$this->__isInitialized__ && $this->_entityPersister) {
  344. $this->__isInitialized__ = true;
  345. $class = $this->_entityPersister->getClassMetadata();
  346. $original = $this->_entityPersister->load($this->_identifier);
  347. if ($original === null) {
  348. throw new \Doctrine\ORM\EntityNotFoundException();
  349. }
  350. foreach ($class->reflFields AS $field => $reflProperty) {
  351. $reflProperty->setValue($this, $reflProperty->getValue($original));
  352. }
  353. unset($this->_entityPersister, $this->_identifier);
  354. }
  355. <cloneImpl>
  356. }
  357. }';
  358. }