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