MetadataFactory.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Metadata;
  18. use Metadata\Driver\DriverInterface;
  19. use Metadata\Cache\CacheInterface;
  20. final class MetadataFactory implements MetadataFactoryInterface
  21. {
  22. private $driver;
  23. private $cache;
  24. private $loadedMetadata = array();
  25. private $loadedClassMetadata = array();
  26. private $hierarchyMetadataClass;
  27. private $includeInterfaces = false;
  28. private $debug;
  29. public function __construct(DriverInterface $driver, $hierarchyMetadataClass = 'Metadata\ClassHierarchyMetadata', $debug = false)
  30. {
  31. $this->driver = $driver;
  32. $this->hierarchyMetadataClass = $hierarchyMetadataClass;
  33. $this->debug = $debug;
  34. }
  35. public function setIncludeInterfaces($bool)
  36. {
  37. $this->includeInterfaces = (Boolean) $bool;
  38. }
  39. public function setCache(CacheInterface $cache)
  40. {
  41. $this->cache = $cache;
  42. }
  43. public function getMetadataForClass($className)
  44. {
  45. if (isset($this->loadedMetadata[$className])) {
  46. return $this->loadedMetadata[$className];
  47. }
  48. $metadata = null;
  49. foreach ($this->getClassHierarchy($className) as $class) {
  50. if (isset($this->loadedClassMetadata[$name = $class->getName()])) {
  51. $this->addClassMetadata($metadata, $this->loadedClassMetadata[$name]);
  52. continue;
  53. }
  54. // check the cache
  55. if (null !== $this->cache
  56. && (null !== $classMetadata = $this->cache->loadClassMetadataFromCache($class))) {
  57. if ($this->debug && !$classMetadata->isFresh()) {
  58. $this->cache->evictClassMetadataFromCache($classMetadata->reflection);
  59. } else {
  60. $this->loadedClassMetadata[$name] = $classMetadata;
  61. $this->addClassMetadata($metadata, $classMetadata);
  62. continue;
  63. }
  64. }
  65. // load from source
  66. if (null !== $classMetadata = $this->driver->loadMetadataForClass($class)) {
  67. $this->loadedClassMetadata[$name] = $classMetadata;
  68. $this->addClassMetadata($metadata, $classMetadata);
  69. if (null !== $this->cache) {
  70. $this->cache->putClassMetadataInCache($classMetadata);
  71. }
  72. continue;
  73. }
  74. }
  75. return $this->loadedMetadata[$className] = $metadata;
  76. }
  77. private function addClassMetadata(&$metadata, $toAdd)
  78. {
  79. if ($toAdd instanceof MergeableInterface) {
  80. if (null === $metadata) {
  81. $metadata = clone $toAdd;
  82. } else {
  83. $metadata->merge($toAdd);
  84. }
  85. } else {
  86. if (null === $metadata) {
  87. $metadata = new $this->hierarchyMetadataClass;
  88. }
  89. $metadata->addClassMetadata($toAdd);
  90. }
  91. }
  92. private function getClassHierarchy($class)
  93. {
  94. $classes = array();
  95. $refl = new \ReflectionClass($class);
  96. do {
  97. $classes[] = $refl;
  98. } while (false !== $refl = $refl->getParentClass());
  99. $classes = array_reverse($classes, false);
  100. if (!$this->includeInterfaces) {
  101. return $classes;
  102. }
  103. $addedInterfaces = array();
  104. $newHierarchy = array();
  105. foreach ($classes as $class) {
  106. foreach ($class->getInterfaces() as $interface) {
  107. if (isset($addedInterfaces[$interface->getName()])) {
  108. continue;
  109. }
  110. $addedInterfaces[$interface->getName()] = true;
  111. $newHierarchy[] = $interface;
  112. }
  113. $newHierarchy[] = $class;
  114. }
  115. return $newHierarchy;
  116. }
  117. }