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