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\ORM\Mapping\Driver; |
---|
21 | |
---|
22 | use Doctrine\ORM\Mapping\ClassMetadataInfo, |
---|
23 | Doctrine\ORM\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.0 |
---|
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 Driver |
---|
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 | /** |
---|
54 | * The file extension of mapping documents. |
---|
55 | * |
---|
56 | * @var string |
---|
57 | */ |
---|
58 | private $_fileExtension = '.php'; |
---|
59 | |
---|
60 | public function __construct($paths) |
---|
61 | { |
---|
62 | $this->addPaths((array) $paths); |
---|
63 | } |
---|
64 | |
---|
65 | public function addPaths(array $paths) |
---|
66 | { |
---|
67 | $this->_paths = array_unique(array_merge($this->_paths, $paths)); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * {@inheritdoc} |
---|
72 | */ |
---|
73 | public function loadMetadataForClass($className, ClassMetadataInfo $metadata) |
---|
74 | { |
---|
75 | call_user_func_array(array($className, 'loadMetadata'), array($metadata)); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * {@inheritDoc} |
---|
80 | * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it? |
---|
81 | */ |
---|
82 | public function getAllClassNames() |
---|
83 | { |
---|
84 | if ($this->_classNames !== null) { |
---|
85 | return $this->_classNames; |
---|
86 | } |
---|
87 | |
---|
88 | if (!$this->_paths) { |
---|
89 | throw MappingException::pathRequired(); |
---|
90 | } |
---|
91 | |
---|
92 | $classes = array(); |
---|
93 | $includedFiles = array(); |
---|
94 | |
---|
95 | foreach ($this->_paths as $path) { |
---|
96 | if (!is_dir($path)) { |
---|
97 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
---|
98 | } |
---|
99 | |
---|
100 | $iterator = new \RecursiveIteratorIterator( |
---|
101 | new \RecursiveDirectoryIterator($path), |
---|
102 | \RecursiveIteratorIterator::LEAVES_ONLY |
---|
103 | ); |
---|
104 | |
---|
105 | foreach ($iterator as $file) { |
---|
106 | if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { |
---|
107 | continue; |
---|
108 | } |
---|
109 | |
---|
110 | $sourceFile = realpath($file->getPathName()); |
---|
111 | require_once $sourceFile; |
---|
112 | $includedFiles[] = $sourceFile; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | $declared = get_declared_classes(); |
---|
117 | |
---|
118 | foreach ($declared as $className) { |
---|
119 | $rc = new \ReflectionClass($className); |
---|
120 | $sourceFile = $rc->getFileName(); |
---|
121 | if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) { |
---|
122 | $classes[] = $className; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | $this->_classNames = $classes; |
---|
127 | |
---|
128 | return $classes; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * {@inheritdoc} |
---|
133 | */ |
---|
134 | public function isTransient($className) |
---|
135 | { |
---|
136 | return method_exists($className, 'loadMetadata') ? false : true; |
---|
137 | } |
---|
138 | } |
---|