PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php

https://github.com/nattaphat/hgis
PHP | 389 lines | 154 code | 49 blank | 186 comment | 19 complexity | ac1bb11fa4c884758e2bb918418db4b7 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\Persistence\Mapping;
  20. use Doctrine\Common\Cache\Cache,
  21. Doctrine\Common\Util\ClassUtils;
  22. /**
  23. * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
  24. * metadata mapping informations of a class which describes how a class should be mapped
  25. * to a relational database.
  26. *
  27. * This class was abstracted from the ORM ClassMetadataFactory
  28. *
  29. * @since 2.2
  30. * @author Benjamin Eberlei <kontakt@beberlei.de>
  31. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  32. * @author Jonathan Wage <jonwage@gmail.com>
  33. * @author Roman Borschel <roman@code-factory.org>
  34. */
  35. abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
  36. {
  37. /**
  38. * Salt used by specific Object Manager implementation.
  39. *
  40. * @var string
  41. */
  42. protected $cacheSalt = "\$CLASSMETADATA";
  43. /**
  44. * @var \Doctrine\Common\Cache\Cache
  45. */
  46. private $cacheDriver;
  47. /**
  48. * @var array
  49. */
  50. private $loadedMetadata = array();
  51. /**
  52. * @var bool
  53. */
  54. protected $initialized = false;
  55. /**
  56. * @var ReflectionService
  57. */
  58. private $reflectionService;
  59. /**
  60. * Sets the cache driver used by the factory to cache ClassMetadata instances.
  61. *
  62. * @param Doctrine\Common\Cache\Cache $cacheDriver
  63. */
  64. public function setCacheDriver(Cache $cacheDriver = null)
  65. {
  66. $this->cacheDriver = $cacheDriver;
  67. }
  68. /**
  69. * Gets the cache driver used by the factory to cache ClassMetadata instances.
  70. *
  71. * @return Doctrine\Common\Cache\Cache
  72. */
  73. public function getCacheDriver()
  74. {
  75. return $this->cacheDriver;
  76. }
  77. /**
  78. * Return an array of all the loaded metadata currently in memory.
  79. *
  80. * @return array
  81. */
  82. public function getLoadedMetadata()
  83. {
  84. return $this->loadedMetadata;
  85. }
  86. /**
  87. * Forces the factory to load the metadata of all classes known to the underlying
  88. * mapping driver.
  89. *
  90. * @return array The ClassMetadata instances of all mapped classes.
  91. */
  92. public function getAllMetadata()
  93. {
  94. if ( ! $this->initialized) {
  95. $this->initialize();
  96. }
  97. $driver = $this->getDriver();
  98. $metadata = array();
  99. foreach ($driver->getAllClassNames() as $className) {
  100. $metadata[] = $this->getMetadataFor($className);
  101. }
  102. return $metadata;
  103. }
  104. /**
  105. * Lazy initialization of this stuff, especially the metadata driver,
  106. * since these are not needed at all when a metadata cache is active.
  107. *
  108. * @return void
  109. */
  110. abstract protected function initialize();
  111. /**
  112. * Get the fully qualified class-name from the namespace alias.
  113. *
  114. * @param string $namespaceAlias
  115. * @param string $simpleClassName
  116. * @return string
  117. */
  118. abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName);
  119. /**
  120. * Return the mapping driver implementation.
  121. *
  122. * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
  123. */
  124. abstract protected function getDriver();
  125. /**
  126. * Wakeup reflection after ClassMetadata gets unserialized from cache.
  127. *
  128. * @param ClassMetadata $class
  129. * @param ReflectionService $reflService
  130. * @return void
  131. */
  132. abstract protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService);
  133. /**
  134. * Initialize Reflection after ClassMetadata was constructed.
  135. *
  136. * @param ClassMetadata $class
  137. * @param ReflectionService $reflService
  138. * @return void
  139. */
  140. abstract protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService);
  141. /**
  142. * Checks whether the class metadata is an entity.
  143. *
  144. * This method should false for mapped superclasses or
  145. * embedded classes.
  146. *
  147. * @param ClassMetadata $class
  148. * @return boolean
  149. */
  150. abstract protected function isEntity(ClassMetadata $class);
  151. /**
  152. * Gets the class metadata descriptor for a class.
  153. *
  154. * @param string $className The name of the class.
  155. * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
  156. */
  157. public function getMetadataFor($className)
  158. {
  159. if (isset($this->loadedMetadata[$className])) {
  160. return $this->loadedMetadata[$className];
  161. }
  162. $realClassName = $className;
  163. // Check for namespace alias
  164. if (strpos($className, ':') !== false) {
  165. list($namespaceAlias, $simpleClassName) = explode(':', $className);
  166. $realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
  167. } else {
  168. $realClassName = ClassUtils::getRealClass($realClassName);
  169. }
  170. if (isset($this->loadedMetadata[$realClassName])) {
  171. // We do not have the alias name in the map, include it
  172. $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
  173. return $this->loadedMetadata[$realClassName];
  174. }
  175. if ($this->cacheDriver) {
  176. if (($cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt)) !== false) {
  177. $this->loadedMetadata[$realClassName] = $cached;
  178. $this->wakeupReflection($cached, $this->getReflectionService());
  179. } else {
  180. foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
  181. $this->cacheDriver->save(
  182. $loadedClassName . $this->cacheSalt, $this->loadedMetadata[$loadedClassName], null
  183. );
  184. }
  185. }
  186. } else {
  187. $this->loadMetadata($realClassName);
  188. }
  189. if ($className != $realClassName) {
  190. // We do not have the alias name in the map, include it
  191. $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
  192. }
  193. return $this->loadedMetadata[$className];
  194. }
  195. /**
  196. * Checks whether the factory has the metadata for a class loaded already.
  197. *
  198. * @param string $className
  199. * @return boolean TRUE if the metadata of the class in question is already loaded, FALSE otherwise.
  200. */
  201. public function hasMetadataFor($className)
  202. {
  203. return isset($this->loadedMetadata[$className]);
  204. }
  205. /**
  206. * Sets the metadata descriptor for a specific class.
  207. *
  208. * NOTE: This is only useful in very special cases, like when generating proxy classes.
  209. *
  210. * @param string $className
  211. * @param ClassMetadata $class
  212. */
  213. public function setMetadataFor($className, $class)
  214. {
  215. $this->loadedMetadata[$className] = $class;
  216. }
  217. /**
  218. * Get array of parent classes for the given entity class
  219. *
  220. * @param string $name
  221. * @return array $parentClasses
  222. */
  223. protected function getParentClasses($name)
  224. {
  225. // Collect parent classes, ignoring transient (not-mapped) classes.
  226. $parentClasses = array();
  227. foreach (array_reverse($this->getReflectionService()->getParentClasses($name)) as $parentClass) {
  228. if ( ! $this->getDriver()->isTransient($parentClass)) {
  229. $parentClasses[] = $parentClass;
  230. }
  231. }
  232. return $parentClasses;
  233. }
  234. /**
  235. * Loads the metadata of the class in question and all it's ancestors whose metadata
  236. * is still not loaded.
  237. *
  238. * Important: The class $name does not necesarily exist at this point here.
  239. * Scenarios in a code-generation setup might have access to XML/YAML
  240. * Mapping files without the actual PHP code existing here. That is why the
  241. * {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface
  242. * should be used for reflection.
  243. *
  244. * @param string $name The name of the class for which the metadata should get loaded.
  245. *
  246. * @return array
  247. */
  248. protected function loadMetadata($name)
  249. {
  250. if ( ! $this->initialized) {
  251. $this->initialize();
  252. }
  253. $loaded = array();
  254. $parentClasses = $this->getParentClasses($name);
  255. $parentClasses[] = $name;
  256. // Move down the hierarchy of parent classes, starting from the topmost class
  257. $parent = null;
  258. $rootEntityFound = false;
  259. $visited = array();
  260. $reflService = $this->getReflectionService();
  261. foreach ($parentClasses as $className) {
  262. if (isset($this->loadedMetadata[$className])) {
  263. $parent = $this->loadedMetadata[$className];
  264. if ($this->isEntity($parent)) {
  265. $rootEntityFound = true;
  266. array_unshift($visited, $className);
  267. }
  268. continue;
  269. }
  270. $class = $this->newClassMetadataInstance($className);
  271. $this->initializeReflection($class, $reflService);
  272. $this->doLoadMetadata($class, $parent, $rootEntityFound, $visited);
  273. $this->loadedMetadata[$className] = $class;
  274. $parent = $class;
  275. if ($this->isEntity($class)) {
  276. $rootEntityFound = true;
  277. array_unshift($visited, $className);
  278. }
  279. $this->wakeupReflection($class, $reflService);
  280. $loaded[] = $className;
  281. }
  282. return $loaded;
  283. }
  284. /**
  285. * Actually load the metadata from the underlying metadata
  286. *
  287. * @param ClassMetadata $class
  288. * @param ClassMetadata|null $parent
  289. * @param bool $rootEntityFound
  290. * @param array $nonSuperclassParents classnames all parent classes that are not marked as mapped superclasses
  291. * @return void
  292. */
  293. abstract protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents);
  294. /**
  295. * Creates a new ClassMetadata instance for the given class name.
  296. *
  297. * @param string $className
  298. * @return ClassMetadata
  299. */
  300. abstract protected function newClassMetadataInstance($className);
  301. /**
  302. * Check if this class is mapped by this Object Manager + ClassMetadata configuration
  303. *
  304. * @param $class
  305. * @return bool
  306. */
  307. public function isTransient($class)
  308. {
  309. if ( ! $this->initialized) {
  310. $this->initialize();
  311. }
  312. // Check for namespace alias
  313. if (strpos($class, ':') !== false) {
  314. list($namespaceAlias, $simpleClassName) = explode(':', $class);
  315. $class = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
  316. }
  317. return $this->getDriver()->isTransient($class);
  318. }
  319. /**
  320. * Set reflectionService.
  321. *
  322. * @param ReflectionService $reflectionService
  323. */
  324. public function setReflectionService(ReflectionService $reflectionService)
  325. {
  326. $this->reflectionService = $reflectionService;
  327. }
  328. /**
  329. * Get the reflection service associated with this metadata factory.
  330. *
  331. * @return ReflectionService
  332. */
  333. public function getReflectionService()
  334. {
  335. if ($this->reflectionService === null) {
  336. $this->reflectionService = new RuntimeReflectionService();
  337. }
  338. return $this->reflectionService;
  339. }
  340. }