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 * @link www.doctrine-project.org
34 * @author Benjamin Eberlei <kontakt@beberlei.de>
35 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
36 * @author Jonathan H. Wage <jonwage@gmail.com>
37 * @author Roman Borschel <roman@code-factory.org>
39 abstract class FileDriver
implements MappingDriver
49 protected $classCache;
54 protected $globalBasename;
57 * Initializes a new FileDriver that looks in the given path(s) for mapping
58 * documents and operates in the specified operating mode.
60 * @param string|array|FileLocator $locator A FileLocator or one/multiple paths
61 * where mapping documents can be found.
62 * @param string|null $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);
74 * Sets the global basename.
80 public function setGlobalBasename($file)
82 $this->globalBasename
= $file;
86 * Retrieves the global basename.
90 public function getGlobalBasename()
92 return $this->globalBasename
;
96 * Gets the element of schema meta data for the class from the mapping file.
97 * This will lazily load the mapping file if it is not loaded yet.
99 * @param string $className
101 * @return array The element of schema meta data.
103 * @throws MappingException
105 public function getElement($className)
107 if ($this->classCache
=== null) {
111 if (isset($this->classCache
[$className])) {
112 return $this->classCache
[$className];
115 $result = $this->loadMappingFile($this->locator
->findMappingFile($className));
116 if (!isset($result[$className])) {
117 throw MappingException
::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator
->getFileExtension());
120 $this->classCache
[$className] = $result[$className];
122 return $result[$className];
128 public function isTransient($className)
130 if ($this->classCache
=== null) {
134 if (isset($this->classCache
[$className])) {
138 return !$this->locator
->fileExists($className);
144 public function getAllClassNames()
146 if ($this->classCache
=== null) {
150 if (! $this->classCache
) {
151 return (array) $this->locator
->getAllClassNames($this->globalBasename
);
155 array_keys($this->classCache
),
156 (array) $this->locator
->getAllClassNames($this->globalBasename
)
161 * Loads a mapping file with the given name and returns a map
162 * from class/entity names to their corresponding file driver elements.
164 * @param string $file The mapping file to load.
168 abstract protected function loadMappingFile($file);
171 * Initializes the class cache from all the global files.
173 * Using this feature adds a substantial performance hit to file drivers as
174 * more metadata has to be loaded into memory than might actually be
175 * necessary. This may not be relevant to scenarios where caching of
176 * metadata is in place, however hits very hard in scenarios where no
181 protected function initialize()
183 $this->classCache
= [];
184 if (null !== $this->globalBasename
) {
185 foreach ($this->locator
->getPaths() as $path) {
186 $file = $path.'/'.$this->globalBasename
.$this->locator
->getFileExtension();
187 if (is_file($file)) {
188 $this->classCache
= array_merge(
190 $this->loadMappingFile($file)
198 * Retrieves the locator used to discover mapping files by className.
200 * @return FileLocator
202 public function getLocator()
204 return $this->locator
;
208 * Sets the locator used to discover mapping files by className.
210 * @param FileLocator $locator
212 public function setLocator(FileLocator
$locator)
214 $this->locator
= $locator;