Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / SymfonyFileLocator.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  * The Symfony File Locator makes a simplifying assumptions compared
26  * to the DefaultFileLocator. By assuming paths only contain entities of a certain
27  * namespace the mapping files consists of the short classname only.
28  *
29  * @author Fabien Potencier <fabien@symfony.com>
30  * @author Benjamin Eberlei <kontakt@beberlei.de>
31  * @license MIT
32  */
33 class SymfonyFileLocator implements FileLocator
34 {
35     /**
36      * The paths where to look for mapping files.
37      *
38      * @var array
39      */
40     protected $paths = array();
41
42     /**
43      * A map of mapping directory path to namespace prefix used to expand class shortnames.
44      *
45      * @var array
46      */
47     protected $prefixes = array();
48
49     /**
50      * File extension that is searched for.
51      *
52      * @var string
53      */
54     protected $fileExtension;
55
56     /**
57      * Constructor
58      *
59      * @param array $prefixes
60      * @param string|null $fileExtension
61      */
62     public function __construct(array $prefixes, $fileExtension = null)
63     {
64         $this->addNamespacePrefixes($prefixes);
65         $this->fileExtension = $fileExtension;
66     }
67
68     /**
69      * Add Namespace Prefixes
70      *
71      * @param array $prefixes
72      */
73     public function addNamespacePrefixes(array $prefixes)
74     {
75         $this->prefixes = array_merge($this->prefixes, $prefixes);
76         $this->paths = array_merge($this->paths, array_keys($prefixes));
77     }
78
79     /**
80      * Get Namespace Prefixes
81      *
82      * @return array
83      */
84     public function getNamespacePrefixes()
85     {
86         return $this->prefixes;
87     }
88
89     /**
90      * {@inheritDoc}
91      */
92     public function getPaths()
93     {
94         return $this->paths;
95     }
96
97     /**
98      * {@inheritDoc}
99      */
100     public function getFileExtension()
101     {
102         return $this->fileExtension;
103     }
104
105     /**
106      * Set the file extension used to look for mapping files under
107      *
108      * @param string $fileExtension The file extension to set
109      * @return void
110      */
111     public function setFileExtension($fileExtension)
112     {
113         $this->fileExtension = $fileExtension;
114     }
115
116     /**
117      * {@inheritDoc}
118      */
119     public function fileExists($className)
120     {
121         $defaultFileName = str_replace('\\', '.', $className).$this->fileExtension;
122         foreach ($this->paths as $path) {
123             if (!isset($this->prefixes[$path])) {
124                 // global namespace class
125                 if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
126                     return true;
127                 }
128
129                 continue;
130             }
131
132             $prefix = $this->prefixes[$path];
133
134             if (0 !== strpos($className, $prefix.'\\')) {
135                 continue;
136             }
137
138             $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
139             return is_file($filename);
140         }
141
142         return false;
143     }
144
145     /**
146      * {@inheritDoc}
147      */
148     public function getAllClassNames($globalBasename = null)
149     {
150         $classes = array();
151
152         if ($this->paths) {
153             foreach ((array) $this->paths as $path) {
154                 if (!is_dir($path)) {
155                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
156                 }
157
158                 $iterator = new \RecursiveIteratorIterator(
159                     new \RecursiveDirectoryIterator($path),
160                     \RecursiveIteratorIterator::LEAVES_ONLY
161                 );
162
163                 foreach ($iterator as $file) {
164                     $fileName = $file->getBasename($this->fileExtension);
165
166                     if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
167                         continue;
168                     }
169
170                     // NOTE: All files found here means classes are not transient!
171                     if (isset($this->prefixes[$path])) {
172                         $classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
173                     } else {
174                         $classes[] = str_replace('.', '\\', $fileName);
175                     }
176                 }
177             }
178         }
179
180         return $classes;
181     }
182
183     /**
184      * {@inheritDoc}
185      */
186     public function findMappingFile($className)
187     {
188         $defaultFileName = str_replace('\\', '.', $className).$this->fileExtension;
189         foreach ($this->paths as $path) {
190             if (!isset($this->prefixes[$path])) {
191                 if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
192                     return $path.DIRECTORY_SEPARATOR.$defaultFileName;
193                 }
194
195                 continue;
196             }
197
198             $prefix = $this->prefixes[$path];
199
200             if (0 !== strpos($className, $prefix.'\\')) {
201                 continue;
202             }
203
204             $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
205             if (is_file($filename)) {
206                 return $filename;
207             }
208
209             throw MappingException::mappingFileNotFound($className, $filename);
210         }
211
212         throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
213     }
214 }