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.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Persistence\Mapping\Driver;
22 use Doctrine\Common\Persistence\Mapping\MappingException;
25 * Base driver for file-based metadata drivers.
27 * A file driver operates in a mode where it loads the mapping files of individual
28 * classes on demand. This requires the user to adhere to the convention of 1 mapping
29 * file per class and the file names of the mapping files must correspond to the full
30 * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
32 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
33 * @link www.doctrine-project.com
35 * @author Benjamin Eberlei <kontakt@beberlei.de>
36 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
37 * @author Jonathan H. Wage <jonwage@gmail.com>
38 * @author Roman Borschel <roman@code-factory.org>
40 abstract class FileDriver implements MappingDriver
50 protected $classCache;
55 protected $globalBasename;
58 * Initializes a new FileDriver that looks in the given path(s) for mapping
59 * documents and operates in the specified operating mode.
61 * @param string|array|FileLocator $locator A FileLocator or one/multiple paths where mapping documents can be found.
62 * @param string $fileExtension
64 public function __construct($locator, $fileExtension = null)
66 if ($locator instanceof FileLocator) {
67 $this->locator = $locator;
69 $this->locator = new DefaultFileLocator((array)$locator, $fileExtension);
78 public function setGlobalBasename($file)
80 $this->globalBasename = $file;
84 * Retrieve global basename
88 public function getGlobalBasename()
90 return $this->globalBasename;
94 * Get the element of schema meta data for the class from the mapping file.
95 * This will lazily load the mapping file if it is not loaded yet
97 * @param string $className
99 * @throws MappingException
100 * @return array The element of schema meta data
102 public function getElement($className)
104 if ($this->classCache === null) {
108 if (isset($this->classCache[$className])) {
109 return $this->classCache[$className];
112 $result = $this->loadMappingFile($this->locator->findMappingFile($className));
113 if (!isset($result[$className])) {
114 throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension());
117 return $result[$className];
121 * Whether the class with the specified name should have its metadata loaded.
122 * This is only the case if it is either mapped as an Entity or a
125 * @param string $className
128 public function isTransient($className)
130 if ($this->classCache === null) {
134 if (isset($this->classCache[$className])) {
138 return !$this->locator->fileExists($className);
142 * Gets the names of all mapped classes known to this driver.
144 * @return array The names of all mapped classes known to this driver.
146 public function getAllClassNames()
148 if ($this->classCache === null) {
152 $classNames = (array)$this->locator->getAllClassNames($this->globalBasename);
153 if ($this->classCache) {
154 $classNames = array_merge(array_keys($this->classCache), $classNames);
160 * Loads a mapping file with the given name and returns a map
161 * from class/entity names to their corresponding file driver elements.
163 * @param string $file The mapping file to load.
166 abstract protected function loadMappingFile($file);
169 * Initialize the class cache from all the global files.
171 * Using this feature adds a substantial performance hit to file drivers as
172 * more metadata has to be loaded into memory than might actually be
173 * necessary. This may not be relevant to scenarios where caching of
174 * metadata is in place, however hits very hard in scenarios where no
179 protected function initialize()
181 $this->classCache = array();
182 if (null !== $this->globalBasename) {
183 foreach ($this->locator->getPaths() as $path) {
184 $file = $path.'/'.$this->globalBasename.$this->locator->getFileExtension();
185 if (is_file($file)) {
186 $this->classCache = array_merge(
188 $this->loadMappingFile($file)
196 * Retrieve the locator used to discover mapping files by className
198 * @return FileLocator
200 public function getLocator()
202 return $this->locator;
206 * Set the locator used to discover mapping files by className
208 * @param FileLocator $locator
210 public function setLocator(FileLocator $locator)
212 $this->locator = $locator;