summaryrefslogtreecommitdiff
path: root/vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php')
-rw-r--r--vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php132
1 files changed, 132 insertions, 0 deletions
diff --git a/vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php b/vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php
new file mode 100644
index 0000000..bbcff75
--- /dev/null
+++ b/vendor/doctrine/persistence/src/Persistence/Mapping/Driver/StaticPHPDriver.php
@@ -0,0 +1,132 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Persistence\Mapping\Driver;
6
7use Doctrine\Persistence\Mapping\ClassMetadata;
8use Doctrine\Persistence\Mapping\MappingException;
9use RecursiveDirectoryIterator;
10use RecursiveIteratorIterator;
11use ReflectionClass;
12
13use function array_merge;
14use function array_unique;
15use function get_declared_classes;
16use function in_array;
17use function is_dir;
18use function method_exists;
19use function realpath;
20
21/**
22 * The StaticPHPDriver calls a static loadMetadata() method on your entity
23 * classes where you can manually populate the ClassMetadata instance.
24 */
25class StaticPHPDriver implements MappingDriver
26{
27 /**
28 * Paths of entity directories.
29 *
30 * @var array<int, string>
31 */
32 private $paths = [];
33
34 /**
35 * Map of all class names.
36 *
37 * @var array<int, string>
38 * @psalm-var list<class-string>
39 */
40 private $classNames;
41
42 /** @param array<int, string>|string $paths */
43 public function __construct($paths)
44 {
45 $this->addPaths((array) $paths);
46 }
47
48 /**
49 * @param array<int, string> $paths
50 *
51 * @return void
52 */
53 public function addPaths(array $paths)
54 {
55 $this->paths = array_unique(array_merge($this->paths, $paths));
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 public function loadMetadataForClass(string $className, ClassMetadata $metadata)
62 {
63 $className::loadMetadata($metadata);
64 }
65
66 /**
67 * {@inheritDoc}
68 *
69 * @todo Same code exists in ColocatedMappingDriver, should we re-use it
70 * somehow or not worry about it?
71 */
72 public function getAllClassNames()
73 {
74 if ($this->classNames !== null) {
75 return $this->classNames;
76 }
77
78 if ($this->paths === []) {
79 throw MappingException::pathRequiredForDriver(static::class);
80 }
81
82 $classes = [];
83 $includedFiles = [];
84
85 foreach ($this->paths as $path) {
86 if (! is_dir($path)) {
87 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
88 }
89
90 $iterator = new RecursiveIteratorIterator(
91 new RecursiveDirectoryIterator($path),
92 RecursiveIteratorIterator::LEAVES_ONLY
93 );
94
95 foreach ($iterator as $file) {
96 if ($file->getBasename('.php') === $file->getBasename()) {
97 continue;
98 }
99
100 $sourceFile = realpath($file->getPathName());
101 require_once $sourceFile;
102 $includedFiles[] = $sourceFile;
103 }
104 }
105
106 $declared = get_declared_classes();
107
108 foreach ($declared as $className) {
109 $rc = new ReflectionClass($className);
110
111 $sourceFile = $rc->getFileName();
112
113 if (! in_array($sourceFile, $includedFiles, true) || $this->isTransient($className)) {
114 continue;
115 }
116
117 $classes[] = $className;
118 }
119
120 $this->classNames = $classes;
121
122 return $classes;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 public function isTransient(string $className)
129 {
130 return ! method_exists($className, 'loadMetadata');
131 }
132}