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 is_string; |
18
|
|
|
use function method_exists; |
19
|
|
|
use 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
|
|
|
*/ |
25
|
|
|
class 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
|
|
|
*/ |
39
|
|
|
private $classNames; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array<int, string>|string $paths |
43
|
|
|
*/ |
44
|
2 |
|
public function __construct($paths) |
45
|
|
|
{ |
46
|
2 |
|
if (is_string($paths)) { |
47
|
|
|
$paths = [$paths]; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$this->addPaths($paths); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array<int, string> $paths |
55
|
|
|
*/ |
56
|
2 |
|
public function addPaths(array $paths) : void |
57
|
|
|
{ |
58
|
2 |
|
$this->paths = array_unique(array_merge($this->paths, $paths)); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function loadMetadataForClass(string $className, ClassMetadata $metadata) : void |
65
|
|
|
{ |
66
|
1 |
|
$className::loadMetadata($metadata); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
* |
72
|
|
|
* @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it? |
73
|
|
|
*/ |
74
|
1 |
|
public function getAllClassNames() : array |
75
|
|
|
{ |
76
|
1 |
|
if ($this->classNames !== null) { |
77
|
|
|
return $this->classNames; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
if ($this->paths === []) { |
81
|
|
|
throw MappingException::pathRequired(); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$classes = []; |
85
|
1 |
|
$includedFiles = []; |
86
|
|
|
|
87
|
1 |
|
foreach ($this->paths as $path) { |
88
|
1 |
|
if (! is_dir($path)) { |
89
|
|
|
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$iterator = new RecursiveIteratorIterator( |
93
|
1 |
|
new RecursiveDirectoryIterator($path), |
94
|
1 |
|
RecursiveIteratorIterator::LEAVES_ONLY |
95
|
|
|
); |
96
|
|
|
|
97
|
1 |
|
foreach ($iterator as $file) { |
98
|
1 |
|
if ($file->getBasename('.php') === $file->getBasename()) { |
99
|
1 |
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$sourceFile = realpath($file->getPathName()); |
103
|
1 |
|
require_once $sourceFile; |
104
|
1 |
|
$includedFiles[] = $sourceFile; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$declared = get_declared_classes(); |
109
|
|
|
|
110
|
1 |
|
foreach ($declared as $className) { |
111
|
1 |
|
$rc = new ReflectionClass($className); |
112
|
|
|
|
113
|
1 |
|
$sourceFile = $rc->getFileName(); |
114
|
|
|
|
115
|
1 |
|
if (! in_array($sourceFile, $includedFiles, true) || $this->isTransient($className)) { |
116
|
1 |
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$classes[] = $className; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$this->classNames = $classes; |
123
|
|
|
|
124
|
1 |
|
return $classes; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function isTransient(string $className) : bool |
131
|
|
|
{ |
132
|
1 |
|
return ! method_exists($className, 'loadMetadata'); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|