1 | <?php |
---|
2 | /* |
---|
3 | * $Id$ |
---|
4 | * |
---|
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
16 | * |
---|
17 | * This software consists of voluntary contributions made by many individuals |
---|
18 | * and is licensed under the LGPL. For more information, see |
---|
19 | * <http://www.doctrine-project.org>. |
---|
20 | */ |
---|
21 | |
---|
22 | namespace Doctrine\ORM\Mapping\Driver; |
---|
23 | |
---|
24 | use Doctrine\ORM\Mapping\MappingException; |
---|
25 | |
---|
26 | /** |
---|
27 | * Base driver for file-based metadata drivers. |
---|
28 | * |
---|
29 | * A file driver operates in a mode where it loads the mapping files of individual |
---|
30 | * classes on demand. This requires the user to adhere to the convention of 1 mapping |
---|
31 | * file per class and the file names of the mapping files must correspond to the full |
---|
32 | * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'. |
---|
33 | * |
---|
34 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
35 | * @link www.doctrine-project.com |
---|
36 | * @since 2.0 |
---|
37 | * @version $Revision$ |
---|
38 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
39 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
40 | * @author Jonathan H. Wage <jonwage@gmail.com> |
---|
41 | * @author Roman Borschel <roman@code-factory.org> |
---|
42 | */ |
---|
43 | abstract class AbstractFileDriver implements Driver |
---|
44 | { |
---|
45 | /** |
---|
46 | * The paths where to look for mapping files. |
---|
47 | * |
---|
48 | * @var array |
---|
49 | */ |
---|
50 | protected $_paths = array(); |
---|
51 | |
---|
52 | /** |
---|
53 | * The file extension of mapping documents. |
---|
54 | * |
---|
55 | * @var string |
---|
56 | */ |
---|
57 | protected $_fileExtension; |
---|
58 | |
---|
59 | /** |
---|
60 | * Initializes a new FileDriver that looks in the given path(s) for mapping |
---|
61 | * documents and operates in the specified operating mode. |
---|
62 | * |
---|
63 | * @param string|array $paths One or multiple paths where mapping documents can be found. |
---|
64 | */ |
---|
65 | public function __construct($paths) |
---|
66 | { |
---|
67 | $this->addPaths((array) $paths); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Append lookup paths to metadata driver. |
---|
72 | * |
---|
73 | * @param array $paths |
---|
74 | */ |
---|
75 | public function addPaths(array $paths) |
---|
76 | { |
---|
77 | $this->_paths = array_unique(array_merge($this->_paths, $paths)); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Retrieve the defined metadata lookup paths. |
---|
82 | * |
---|
83 | * @return array |
---|
84 | */ |
---|
85 | public function getPaths() |
---|
86 | { |
---|
87 | return $this->_paths; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Get the file extension used to look for mapping files under |
---|
92 | * |
---|
93 | * @return void |
---|
94 | */ |
---|
95 | public function getFileExtension() |
---|
96 | { |
---|
97 | return $this->_fileExtension; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Set the file extension used to look for mapping files under |
---|
102 | * |
---|
103 | * @param string $fileExtension The file extension to set |
---|
104 | * @return void |
---|
105 | */ |
---|
106 | public function setFileExtension($fileExtension) |
---|
107 | { |
---|
108 | $this->_fileExtension = $fileExtension; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Get the element of schema meta data for the class from the mapping file. |
---|
113 | * This will lazily load the mapping file if it is not loaded yet |
---|
114 | * |
---|
115 | * @return array $element The element of schema meta data |
---|
116 | */ |
---|
117 | public function getElement($className) |
---|
118 | { |
---|
119 | $result = $this->_loadMappingFile($this->_findMappingFile($className)); |
---|
120 | |
---|
121 | if(!isset($result[$className])){ |
---|
122 | throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->_fileExtension); |
---|
123 | } |
---|
124 | return $result[$className]; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Whether the class with the specified name should have its metadata loaded. |
---|
129 | * This is only the case if it is either mapped as an Entity or a |
---|
130 | * MappedSuperclass. |
---|
131 | * |
---|
132 | * @param string $className |
---|
133 | * @return boolean |
---|
134 | */ |
---|
135 | public function isTransient($className) |
---|
136 | { |
---|
137 | $fileName = str_replace('\\', '.', $className) . $this->_fileExtension; |
---|
138 | |
---|
139 | // Check whether file exists |
---|
140 | foreach ((array) $this->_paths as $path) { |
---|
141 | if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) { |
---|
142 | return false; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Gets the names of all mapped classes known to this driver. |
---|
151 | * |
---|
152 | * @return array The names of all mapped classes known to this driver. |
---|
153 | */ |
---|
154 | public function getAllClassNames() |
---|
155 | { |
---|
156 | $classes = array(); |
---|
157 | |
---|
158 | if ($this->_paths) { |
---|
159 | foreach ((array) $this->_paths as $path) { |
---|
160 | if ( ! is_dir($path)) { |
---|
161 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
---|
162 | } |
---|
163 | |
---|
164 | $iterator = new \RecursiveIteratorIterator( |
---|
165 | new \RecursiveDirectoryIterator($path), |
---|
166 | \RecursiveIteratorIterator::LEAVES_ONLY |
---|
167 | ); |
---|
168 | |
---|
169 | foreach ($iterator as $file) { |
---|
170 | if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { |
---|
171 | continue; |
---|
172 | } |
---|
173 | |
---|
174 | // NOTE: All files found here means classes are not transient! |
---|
175 | $classes[] = str_replace('.', '\\', $fileName); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | return $classes; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Finds the mapping file for the class with the given name by searching |
---|
185 | * through the configured paths. |
---|
186 | * |
---|
187 | * @param $className |
---|
188 | * @return string The (absolute) file name. |
---|
189 | * @throws MappingException |
---|
190 | */ |
---|
191 | protected function _findMappingFile($className) |
---|
192 | { |
---|
193 | $fileName = str_replace('\\', '.', $className) . $this->_fileExtension; |
---|
194 | |
---|
195 | // Check whether file exists |
---|
196 | foreach ((array) $this->_paths as $path) { |
---|
197 | if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) { |
---|
198 | return $path . DIRECTORY_SEPARATOR . $fileName; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | throw MappingException::mappingFileNotFound($className, $fileName); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Loads a mapping file with the given name and returns a map |
---|
207 | * from class/entity names to their corresponding elements. |
---|
208 | * |
---|
209 | * @param string $file The mapping file to load. |
---|
210 | * @return array |
---|
211 | */ |
---|
212 | abstract protected function _loadMappingFile($file); |
---|
213 | } |
---|