MetadataFactory.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Cache\CacheInterface;
  19. use Metadata\Driver\DriverInterface;
  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 $debug;
  28. public function __construct(DriverInterface $driver, $hierarchyMetadataClass = 'Metadata\ClassHierarchyMetadata', $debug = false)
  29. {
  30. $this->driver = $driver;
  31. $this->hierarchyMetadataClass = $hierarchyMetadataClass;
  32. $this->debug = $debug;
  33. }
  34. public function setCache(CacheInterface $cache)
  35. {
  36. $this->cache = $cache;
  37. }
  38. public function getMetadataForClass($className)
  39. {
  40. if (isset($this->loadedMetadata[$className])) {
  41. return $this->loadedMetadata[$className];
  42. }
  43. $metadata = new $this->hierarchyMetadataClass;
  44. foreach ($this->getClassHierarchy($className) as $class) {
  45. if (isset($this->loadedClassMetadata[$name = $class->getName()])) {
  46. $metadata->addClassMetadata($this->loadedClassMetadata[$name]);
  47. continue;
  48. }
  49. // check the cache
  50. if (null !== $this->cache
  51. && (null !== $classMetadata = $this->cache->loadClassMetadataFromCache($class))) {
  52. if ($this->debug && !$classMetadata->isFresh()) {
  53. $this->cache->evictClassMetadataFromCache($classMetadata->reflection);
  54. } else {
  55. $this->loadedClassMetadata[$name] = $classMetadata;
  56. $metadata->addClassMetadata($classMetadata);
  57. continue;
  58. }
  59. }
  60. // load from source
  61. if (null !== $classMetadata = $this->driver->loadMetadataForClass($class)) {
  62. $this->loadedClassMetadata[$name] = $classMetadata;
  63. $metadata->addClassMetadata($classMetadata);
  64. if (null !== $this->cache) {
  65. $this->cache->putClassMetadataInCache($classMetadata);
  66. }
  67. continue;
  68. }
  69. }
  70. if (!$metadata->classMetadata) {
  71. return null;
  72. }
  73. return $this->loadedMetadata[$className] = $metadata;
  74. }
  75. private function getClassHierarchy($class)
  76. {
  77. $classes = array();
  78. $refl = new \ReflectionClass($class);
  79. do {
  80. $classes[] = $refl;
  81. } while (false !== $refl = $refl->getParentClass());
  82. return array_reverse($classes, false);
  83. }
  84. }