StaticPHPDriver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Doctrine\Persistence\Mapping\Driver;
  3. use Doctrine\Persistence\Mapping\ClassMetadata;
  4. use Doctrine\Persistence\Mapping\MappingException;
  5. use RecursiveDirectoryIterator;
  6. use RecursiveIteratorIterator;
  7. use ReflectionClass;
  8. use function array_merge;
  9. use function array_unique;
  10. use function get_declared_classes;
  11. use function in_array;
  12. use function is_dir;
  13. use function method_exists;
  14. use function realpath;
  15. /**
  16. * The StaticPHPDriver calls a static loadMetadata() method on your entity
  17. * classes where you can manually populate the ClassMetadata instance.
  18. */
  19. class StaticPHPDriver implements MappingDriver
  20. {
  21. /**
  22. * Paths of entity directories.
  23. *
  24. * @var string[]
  25. */
  26. private $paths = [];
  27. /**
  28. * Map of all class names.
  29. *
  30. * @var string[]
  31. */
  32. private $classNames;
  33. /**
  34. * @param string[]|string $paths
  35. */
  36. public function __construct($paths)
  37. {
  38. $this->addPaths((array) $paths);
  39. }
  40. /**
  41. * Adds paths.
  42. *
  43. * @param string[] $paths
  44. *
  45. * @return void
  46. */
  47. public function addPaths(array $paths)
  48. {
  49. $this->paths = array_unique(array_merge($this->paths, $paths));
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function loadMetadataForClass($className, ClassMetadata $metadata)
  55. {
  56. $className::loadMetadata($metadata);
  57. }
  58. /**
  59. * {@inheritDoc}
  60. *
  61. * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
  62. */
  63. public function getAllClassNames()
  64. {
  65. if ($this->classNames !== null) {
  66. return $this->classNames;
  67. }
  68. if (! $this->paths) {
  69. throw MappingException::pathRequired();
  70. }
  71. $classes = [];
  72. $includedFiles = [];
  73. foreach ($this->paths as $path) {
  74. if (! is_dir($path)) {
  75. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  76. }
  77. $iterator = new RecursiveIteratorIterator(
  78. new RecursiveDirectoryIterator($path),
  79. RecursiveIteratorIterator::LEAVES_ONLY
  80. );
  81. foreach ($iterator as $file) {
  82. if ($file->getBasename('.php') === $file->getBasename()) {
  83. continue;
  84. }
  85. $sourceFile = realpath($file->getPathName());
  86. require_once $sourceFile;
  87. $includedFiles[] = $sourceFile;
  88. }
  89. }
  90. $declared = get_declared_classes();
  91. foreach ($declared as $className) {
  92. $rc = new ReflectionClass($className);
  93. $sourceFile = $rc->getFileName();
  94. if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
  95. continue;
  96. }
  97. $classes[] = $className;
  98. }
  99. $this->classNames = $classes;
  100. return $classes;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function isTransient($className)
  106. {
  107. return ! method_exists($className, 'loadMetadata');
  108. }
  109. }