Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / FileDriver.php
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 MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Persistence\Mapping\Driver;
21
22 use Doctrine\Common\Persistence\Mapping\MappingException;
23
24 /**
25  * Base driver for file-based metadata drivers.
26  *
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 '.'.
31  *
32  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
33  * @link        www.doctrine-project.com
34  * @since       2.2
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>
39  */
40 abstract class FileDriver implements MappingDriver
41 {
42     /**
43      * @var FileLocator
44      */
45     protected $locator;
46
47     /**
48      * @var array
49      */
50     protected $classCache;
51
52     /**
53      * @var string
54      */
55     protected $globalBasename;
56
57     /**
58      * Initializes a new FileDriver that looks in the given path(s) for mapping
59      * documents and operates in the specified operating mode.
60      *
61      * @param string|array|FileLocator $locator A FileLocator or one/multiple paths where mapping documents can be found.
62      * @param string $fileExtension
63      */
64     public function __construct($locator, $fileExtension = null)
65     {
66         if ($locator instanceof FileLocator) {
67             $this->locator = $locator;
68         } else {
69             $this->locator = new DefaultFileLocator((array)$locator, $fileExtension);
70         }
71     }
72
73     /**
74      * Set global basename
75      *
76      * @param string $file
77      */
78     public function setGlobalBasename($file)
79     {
80         $this->globalBasename = $file;
81     }
82
83     /**
84      * Retrieve global basename
85      *
86      * @return string
87      */
88     public function getGlobalBasename()
89     {
90         return $this->globalBasename;
91     }
92
93     /**
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
96      *
97      * @param string $className
98      *
99      * @throws MappingException
100      * @return array The element of schema meta data
101      */
102     public function getElement($className)
103     {
104         if ($this->classCache === null) {
105             $this->initialize();
106         }
107
108         if (isset($this->classCache[$className])) {
109             return $this->classCache[$className];
110         }
111
112         $result = $this->loadMappingFile($this->locator->findMappingFile($className));
113         if (!isset($result[$className])) {
114             throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension());
115         }
116
117         return $result[$className];
118     }
119
120     /**
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
123      * MappedSuperclass.
124      *
125      * @param string $className
126      * @return boolean
127      */
128     public function isTransient($className)
129     {
130         if ($this->classCache === null) {
131             $this->initialize();
132         }
133
134         if (isset($this->classCache[$className])) {
135             return false;
136         }
137
138         return !$this->locator->fileExists($className);
139     }
140
141     /**
142      * Gets the names of all mapped classes known to this driver.
143      *
144      * @return array The names of all mapped classes known to this driver.
145      */
146     public function getAllClassNames()
147     {
148         if ($this->classCache === null) {
149             $this->initialize();
150         }
151
152         $classNames = (array)$this->locator->getAllClassNames($this->globalBasename);
153         if ($this->classCache) {
154             $classNames = array_merge(array_keys($this->classCache), $classNames);
155         }
156         return $classNames;
157     }
158
159     /**
160      * Loads a mapping file with the given name and returns a map
161      * from class/entity names to their corresponding file driver elements.
162      *
163      * @param string $file The mapping file to load.
164      * @return array
165      */
166     abstract protected function loadMappingFile($file);
167
168     /**
169      * Initialize the class cache from all the global files.
170      *
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
175      * caching is used.
176      *
177      * @return void
178      */
179     protected function initialize()
180     {
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(
187                         $this->classCache,
188                         $this->loadMappingFile($file)
189                     );
190                 }
191             }
192         }
193     }
194
195     /**
196      * Retrieve the locator used to discover mapping files by className
197      *
198      * @return FileLocator
199      */
200     public function getLocator()
201     {
202         return $this->locator;
203     }
204
205     /**
206      * Set the locator used to discover mapping files by className
207      *
208      * @param FileLocator $locator
209      */
210     public function setLocator(FileLocator $locator)
211     {
212         $this->locator = $locator;
213     }
214 }