PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php

https://gitlab.com/reasonat/test8
PHP | 248 lines | 96 code | 35 blank | 117 comment | 6 complexity | b4a30ee5908da390611e01e3b0fa4ed5 MD5 | raw file
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Proxy;
  20. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  21. use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
  22. use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
  23. use Doctrine\Common\Proxy\Exception\OutOfBoundsException;
  24. use Doctrine\Common\Util\ClassUtils;
  25. /**
  26. * Abstract factory for proxy objects.
  27. *
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. */
  30. abstract class AbstractProxyFactory
  31. {
  32. /**
  33. * Never autogenerate a proxy and rely that it was generated by some
  34. * process before deployment.
  35. *
  36. * @var integer
  37. */
  38. const AUTOGENERATE_NEVER = 0;
  39. /**
  40. * Always generates a new proxy in every request.
  41. *
  42. * This is only sane during development.
  43. *
  44. * @var integer
  45. */
  46. const AUTOGENERATE_ALWAYS = 1;
  47. /**
  48. * Autogenerate the proxy class when the proxy file does not exist.
  49. *
  50. * This strategy causes a file exists call whenever any proxy is used the
  51. * first time in a request.
  52. *
  53. * @var integer
  54. */
  55. const AUTOGENERATE_FILE_NOT_EXISTS = 2;
  56. /**
  57. * Generate the proxy classes using eval().
  58. *
  59. * This strategy is only sane for development, and even then it gives me
  60. * the creeps a little.
  61. *
  62. * @var integer
  63. */
  64. const AUTOGENERATE_EVAL = 3;
  65. /**
  66. * @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
  67. */
  68. private $metadataFactory;
  69. /**
  70. * @var \Doctrine\Common\Proxy\ProxyGenerator the proxy generator responsible for creating the proxy classes/files.
  71. */
  72. private $proxyGenerator;
  73. /**
  74. * @var bool Whether to automatically (re)generate proxy classes.
  75. */
  76. private $autoGenerate;
  77. /**
  78. * @var \Doctrine\Common\Proxy\ProxyDefinition[]
  79. */
  80. private $definitions = array();
  81. /**
  82. * @param \Doctrine\Common\Proxy\ProxyGenerator $proxyGenerator
  83. * @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory
  84. * @param bool|int $autoGenerate
  85. */
  86. public function __construct(ProxyGenerator $proxyGenerator, ClassMetadataFactory $metadataFactory, $autoGenerate)
  87. {
  88. $this->proxyGenerator = $proxyGenerator;
  89. $this->metadataFactory = $metadataFactory;
  90. $this->autoGenerate = (int)$autoGenerate;
  91. }
  92. /**
  93. * Gets a reference proxy instance for the entity of the given type and identified by
  94. * the given identifier.
  95. *
  96. * @param string $className
  97. * @param array $identifier
  98. *
  99. * @return \Doctrine\Common\Proxy\Proxy
  100. *
  101. * @throws \Doctrine\Common\Proxy\Exception\OutOfBoundsException
  102. */
  103. public function getProxy($className, array $identifier)
  104. {
  105. $definition = isset($this->definitions[$className])
  106. ? $this->definitions[$className]
  107. : $this->getProxyDefinition($className);
  108. $fqcn = $definition->proxyClassName;
  109. $proxy = new $fqcn($definition->initializer, $definition->cloner);
  110. foreach ($definition->identifierFields as $idField) {
  111. if (! isset($identifier[$idField])) {
  112. throw OutOfBoundsException::missingPrimaryKeyValue($className, $idField);
  113. }
  114. $definition->reflectionFields[$idField]->setValue($proxy, $identifier[$idField]);
  115. }
  116. return $proxy;
  117. }
  118. /**
  119. * Generates proxy classes for all given classes.
  120. *
  121. * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata[] $classes The classes (ClassMetadata instances)
  122. * for which to generate proxies.
  123. * @param string $proxyDir The target directory of the proxy classes. If not specified, the
  124. * directory configured on the Configuration of the EntityManager used
  125. * by this factory is used.
  126. * @return int Number of generated proxies.
  127. */
  128. public function generateProxyClasses(array $classes, $proxyDir = null)
  129. {
  130. $generated = 0;
  131. foreach ($classes as $class) {
  132. if ($this->skipClass($class)) {
  133. continue;
  134. }
  135. $proxyFileName = $this->proxyGenerator->getProxyFileName($class->getName(), $proxyDir);
  136. $this->proxyGenerator->generateProxyClass($class, $proxyFileName);
  137. $generated += 1;
  138. }
  139. return $generated;
  140. }
  141. /**
  142. * Reset initialization/cloning logic for an un-initialized proxy
  143. *
  144. * @param \Doctrine\Common\Proxy\Proxy $proxy
  145. *
  146. * @return \Doctrine\Common\Proxy\Proxy
  147. *
  148. * @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException
  149. */
  150. public function resetUninitializedProxy(Proxy $proxy)
  151. {
  152. if ($proxy->__isInitialized()) {
  153. throw InvalidArgumentException::unitializedProxyExpected($proxy);
  154. }
  155. $className = ClassUtils::getClass($proxy);
  156. $definition = isset($this->definitions[$className])
  157. ? $this->definitions[$className]
  158. : $this->getProxyDefinition($className);
  159. $proxy->__setInitializer($definition->initializer);
  160. $proxy->__setCloner($definition->cloner);
  161. return $proxy;
  162. }
  163. /**
  164. * Get a proxy definition for the given class name.
  165. *
  166. * @param string $className
  167. *
  168. * @return ProxyDefinition
  169. */
  170. private function getProxyDefinition($className)
  171. {
  172. $classMetadata = $this->metadataFactory->getMetadataFor($className);
  173. $className = $classMetadata->getName(); // aliases and case sensitivity
  174. $this->definitions[$className] = $this->createProxyDefinition($className);
  175. $proxyClassName = $this->definitions[$className]->proxyClassName;
  176. if ( ! class_exists($proxyClassName, false)) {
  177. $fileName = $this->proxyGenerator->getProxyFileName($className);
  178. switch ($this->autoGenerate) {
  179. case self::AUTOGENERATE_NEVER:
  180. require $fileName;
  181. break;
  182. case self::AUTOGENERATE_FILE_NOT_EXISTS:
  183. if ( ! file_exists($fileName)) {
  184. $this->proxyGenerator->generateProxyClass($classMetadata, $fileName);
  185. }
  186. require $fileName;
  187. break;
  188. case self::AUTOGENERATE_ALWAYS:
  189. $this->proxyGenerator->generateProxyClass($classMetadata, $fileName);
  190. require $fileName;
  191. break;
  192. case self::AUTOGENERATE_EVAL:
  193. $this->proxyGenerator->generateProxyClass($classMetadata, false);
  194. break;
  195. }
  196. }
  197. return $this->definitions[$className];
  198. }
  199. /**
  200. * Determine if this class should be skipped during proxy generation.
  201. *
  202. * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata
  203. *
  204. * @return bool
  205. */
  206. abstract protected function skipClass(ClassMetadata $metadata);
  207. /**
  208. * @param string $className
  209. *
  210. * @return ProxyDefinition
  211. */
  212. abstract protected function createProxyDefinition($className);
  213. }