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