1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\Common\Persistence\Mapping\Driver; |
---|
21 | |
---|
22 | use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
---|
23 | use Doctrine\Common\Persistence\Mapping\MappingException; |
---|
24 | |
---|
25 | /** |
---|
26 | * The StaticPHPDriver calls a static loadMetadata() method on your entity |
---|
27 | * classes where you can manually populate the ClassMetadata instance. |
---|
28 | * |
---|
29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
30 | * @link www.doctrine-project.org |
---|
31 | * @since 2.2 |
---|
32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
33 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
34 | * @author Jonathan H. Wage <jonwage@gmail.com> |
---|
35 | * @author Roman Borschel <roman@code-factory.org> |
---|
36 | */ |
---|
37 | class StaticPHPDriver implements MappingDriver |
---|
38 | { |
---|
39 | /** |
---|
40 | * Paths of entity directories. |
---|
41 | * |
---|
42 | * @var array |
---|
43 | */ |
---|
44 | private $paths = array(); |
---|
45 | |
---|
46 | /** |
---|
47 | * Map of all class names. |
---|
48 | * |
---|
49 | * @var array |
---|
50 | */ |
---|
51 | private $classNames; |
---|
52 | |
---|
53 | public function __construct($paths) |
---|
54 | { |
---|
55 | $this->addPaths((array) $paths); |
---|
56 | } |
---|
57 | |
---|
58 | public function addPaths(array $paths) |
---|
59 | { |
---|
60 | $this->paths = array_unique(array_merge($this->paths, $paths)); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * {@inheritdoc} |
---|
65 | */ |
---|
66 | public function loadMetadataForClass($className, ClassMetadata $metadata) |
---|
67 | { |
---|
68 | $className::loadMetadata($metadata); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * {@inheritDoc} |
---|
73 | * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it? |
---|
74 | */ |
---|
75 | public function getAllClassNames() |
---|
76 | { |
---|
77 | if ($this->classNames !== null) { |
---|
78 | return $this->classNames; |
---|
79 | } |
---|
80 | |
---|
81 | if (!$this->paths) { |
---|
82 | throw MappingException::pathRequired(); |
---|
83 | } |
---|
84 | |
---|
85 | $classes = array(); |
---|
86 | $includedFiles = array(); |
---|
87 | |
---|
88 | foreach ($this->paths as $path) { |
---|
89 | if (!is_dir($path)) { |
---|
90 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
---|
91 | } |
---|
92 | |
---|
93 | $iterator = new \RecursiveIteratorIterator( |
---|
94 | new \RecursiveDirectoryIterator($path), |
---|
95 | \RecursiveIteratorIterator::LEAVES_ONLY |
---|
96 | ); |
---|
97 | |
---|
98 | foreach ($iterator as $file) { |
---|
99 | if ($file->getBasename('.php') == $file->getBasename()) { |
---|
100 | continue; |
---|
101 | } |
---|
102 | |
---|
103 | $sourceFile = realpath($file->getPathName()); |
---|
104 | require_once $sourceFile; |
---|
105 | $includedFiles[] = $sourceFile; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | $declared = get_declared_classes(); |
---|
110 | |
---|
111 | foreach ($declared as $className) { |
---|
112 | $rc = new \ReflectionClass($className); |
---|
113 | $sourceFile = $rc->getFileName(); |
---|
114 | if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) { |
---|
115 | $classes[] = $className; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | $this->classNames = $classes; |
---|
120 | |
---|
121 | return $classes; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * {@inheritdoc} |
---|
126 | */ |
---|
127 | public function isTransient($className) |
---|
128 | { |
---|
129 | return ! method_exists($className, 'loadMetadata'); |
---|
130 | } |
---|
131 | } |
---|