Version 1
[yaffs-website] / 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 = [];
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 = [];
48
49     /**
50      * File extension that is searched for.
51      *
52      * @var string|null
53      */
54     protected $fileExtension;
55
56     /**
57      * Represents PHP namespace delimiters when looking for files
58      *
59      * @var string
60      */
61     private $nsSeparator;
62
63     /**
64      * Constructor.
65      *
66      * @param array       $prefixes
67      * @param string|null $fileExtension
68      * @param string      $nsSeparator String which would be used when converting FQCN to filename and vice versa. Should not be empty
69      */
70     public function __construct(array $prefixes, $fileExtension = null, $nsSeparator = '.')
71     {
72         $this->addNamespacePrefixes($prefixes);
73         $this->fileExtension = $fileExtension;
74
75         if (empty($nsSeparator)) {
76             throw new \InvalidArgumentException('Namespace separator should not be empty');
77         }
78
79         $this->nsSeparator = (string) $nsSeparator;
80     }
81
82     /**
83      * Adds Namespace Prefixes.
84      *
85      * @param array $prefixes
86      *
87      * @return void
88      */
89     public function addNamespacePrefixes(array $prefixes)
90     {
91         $this->prefixes = array_merge($this->prefixes, $prefixes);
92         $this->paths = array_merge($this->paths, array_keys($prefixes));
93     }
94
95     /**
96      * Gets Namespace Prefixes.
97      *
98      * @return array
99      */
100     public function getNamespacePrefixes()
101     {
102         return $this->prefixes;
103     }
104
105     /**
106      * {@inheritDoc}
107      */
108     public function getPaths()
109     {
110         return $this->paths;
111     }
112
113     /**
114      * {@inheritDoc}
115      */
116     public function getFileExtension()
117     {
118         return $this->fileExtension;
119     }
120
121     /**
122      * Sets the file extension used to look for mapping files under.
123      *
124      * @param string $fileExtension The file extension to set.
125      *
126      * @return void
127      */
128     public function setFileExtension($fileExtension)
129     {
130         $this->fileExtension = $fileExtension;
131     }
132
133     /**
134      * {@inheritDoc}
135      */
136     public function fileExists($className)
137     {
138         $defaultFileName = str_replace('\\', $this->nsSeparator, $className).$this->fileExtension;
139         foreach ($this->paths as $path) {
140             if (!isset($this->prefixes[$path])) {
141                 // global namespace class
142                 if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
143                     return true;
144                 }
145
146                 continue;
147             }
148
149             $prefix = $this->prefixes[$path];
150
151             if (0 !== strpos($className, $prefix.'\\')) {
152                 continue;
153             }
154
155             $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator).$this->fileExtension;
156             if (is_file($filename)) {
157                 return true;
158             }
159         }
160
161         return false;
162     }
163
164     /**
165      * {@inheritDoc}
166      */
167     public function getAllClassNames($globalBasename = null)
168     {
169         $classes = [];
170
171         if ($this->paths) {
172             foreach ((array) $this->paths as $path) {
173                 if (!is_dir($path)) {
174                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
175                 }
176
177                 $iterator = new \RecursiveIteratorIterator(
178                     new \RecursiveDirectoryIterator($path),
179                     \RecursiveIteratorIterator::LEAVES_ONLY
180                 );
181
182                 foreach ($iterator as $file) {
183                     $fileName = $file->getBasename($this->fileExtension);
184
185                     if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
186                         continue;
187                     }
188
189                     // NOTE: All files found here means classes are not transient!
190                     if (isset($this->prefixes[$path])) {
191
192                         // Calculate namespace suffix for given prefix as a relative path from basepath to file path
193                         $nsSuffix = strtr(
194                             substr(realpath($file->getPath()), strlen(realpath($path))),
195                             $this->nsSeparator,
196                             '\\'
197                         );
198
199                         $classes[] = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' .str_replace($this->nsSeparator, '\\', $fileName);
200                     } else {
201                         $classes[] = str_replace($this->nsSeparator, '\\', $fileName);
202                     }
203                 }
204             }
205         }
206
207         return $classes;
208     }
209
210     /**
211      * {@inheritDoc}
212      */
213     public function findMappingFile($className)
214     {
215         $defaultFileName = str_replace('\\', $this->nsSeparator, $className).$this->fileExtension;
216         foreach ($this->paths as $path) {
217             if (!isset($this->prefixes[$path])) {
218                 if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
219                     return $path.DIRECTORY_SEPARATOR.$defaultFileName;
220                 }
221
222                 continue;
223             }
224
225             $prefix = $this->prefixes[$path];
226
227             if (0 !== strpos($className, $prefix.'\\')) {
228                 continue;
229             }
230
231             $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator ).$this->fileExtension;
232             if (is_file($filename)) {
233                 return $filename;
234             }
235         }
236
237         throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
238     }
239 }