|
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 return $result[$className]; |
|
122 } |
|
123 |
|
124 /** |
|
125 * Whether the class with the specified name should have its metadata loaded. |
|
126 * This is only the case if it is either mapped as an Entity or a |
|
127 * MappedSuperclass. |
|
128 * |
|
129 * @param string $className |
|
130 * @return boolean |
|
131 */ |
|
132 public function isTransient($className) |
|
133 { |
|
134 $fileName = str_replace('\\', '.', $className) . $this->_fileExtension; |
|
135 |
|
136 // Check whether file exists |
|
137 foreach ((array) $this->_paths as $path) { |
|
138 if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) { |
|
139 return false; |
|
140 } |
|
141 } |
|
142 |
|
143 return true; |
|
144 } |
|
145 |
|
146 /** |
|
147 * Gets the names of all mapped classes known to this driver. |
|
148 * |
|
149 * @return array The names of all mapped classes known to this driver. |
|
150 */ |
|
151 public function getAllClassNames() |
|
152 { |
|
153 $classes = array(); |
|
154 |
|
155 if ($this->_paths) { |
|
156 foreach ((array) $this->_paths as $path) { |
|
157 if ( ! is_dir($path)) { |
|
158 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
|
159 } |
|
160 |
|
161 $iterator = new \RecursiveIteratorIterator( |
|
162 new \RecursiveDirectoryIterator($path), |
|
163 \RecursiveIteratorIterator::LEAVES_ONLY |
|
164 ); |
|
165 |
|
166 foreach ($iterator as $file) { |
|
167 if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { |
|
168 continue; |
|
169 } |
|
170 |
|
171 // NOTE: All files found here means classes are not transient! |
|
172 $classes[] = str_replace('.', '\\', $fileName); |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 return $classes; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Finds the mapping file for the class with the given name by searching |
|
182 * through the configured paths. |
|
183 * |
|
184 * @param $className |
|
185 * @return string The (absolute) file name. |
|
186 * @throws MappingException |
|
187 */ |
|
188 protected function _findMappingFile($className) |
|
189 { |
|
190 $fileName = str_replace('\\', '.', $className) . $this->_fileExtension; |
|
191 |
|
192 // Check whether file exists |
|
193 foreach ((array) $this->_paths as $path) { |
|
194 if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) { |
|
195 return $path . DIRECTORY_SEPARATOR . $fileName; |
|
196 } |
|
197 } |
|
198 |
|
199 throw MappingException::mappingFileNotFound($className, $fileName); |
|
200 } |
|
201 |
|
202 /** |
|
203 * Loads a mapping file with the given name and returns a map |
|
204 * from class/entity names to their corresponding elements. |
|
205 * |
|
206 * @param string $file The mapping file to load. |
|
207 * @return array |
|
208 */ |
|
209 abstract protected function _loadMappingFile($file); |
|
210 } |