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