AbstractClassMetadataFactory.php 11 KB

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