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 * 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.
29 * @author Fabien Potencier <fabien@symfony.com>
30 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 class SymfonyFileLocator
implements FileLocator
36 * The paths where to look for mapping files.
40 protected $paths = [];
43 * A map of mapping directory path to namespace prefix used to expand class shortnames.
47 protected $prefixes = [];
50 * File extension that is searched for.
54 protected $fileExtension;
57 * Represents PHP namespace delimiters when looking for files
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
70 public function __construct(array $prefixes, $fileExtension = null, $nsSeparator = '.')
72 $this->addNamespacePrefixes($prefixes);
73 $this->fileExtension
= $fileExtension;
75 if (empty($nsSeparator)) {
76 throw new \
InvalidArgumentException('Namespace separator should not be empty');
79 $this->nsSeparator
= (string) $nsSeparator;
83 * Adds Namespace Prefixes.
85 * @param array $prefixes
89 public function addNamespacePrefixes(array $prefixes)
91 $this->prefixes
= array_merge($this->prefixes
, $prefixes);
92 $this->paths
= array_merge($this->paths
, array_keys($prefixes));
96 * Gets Namespace Prefixes.
100 public function getNamespacePrefixes()
102 return $this->prefixes
;
108 public function getPaths()
116 public function getFileExtension()
118 return $this->fileExtension
;
122 * Sets the file extension used to look for mapping files under.
124 * @param string $fileExtension The file extension to set.
128 public function setFileExtension($fileExtension)
130 $this->fileExtension
= $fileExtension;
136 public function fileExists($className)
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)) {
149 $prefix = $this->prefixes
[$path];
151 if (0 !== strpos($className, $prefix.'\\')) {
155 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+
1), '\\', $this->nsSeparator
).$this->fileExtension
;
156 if (is_file($filename)) {
167 public function getAllClassNames($globalBasename = null)
172 foreach ((array) $this->paths
as $path) {
173 if (!is_dir($path)) {
174 throw MappingException
::fileMappingDriversRequireConfiguredDirectoryPath($path);
177 $iterator = new \
RecursiveIteratorIterator(
178 new \
RecursiveDirectoryIterator($path),
179 \RecursiveIteratorIterator
::LEAVES_ONLY
182 foreach ($iterator as $file) {
183 $fileName = $file->getBasename($this->fileExtension
);
185 if ($fileName == $file->getBasename() ||
$fileName == $globalBasename) {
189 // NOTE: All files found here means classes are not transient!
190 if (isset($this->prefixes
[$path])) {
192 // Calculate namespace suffix for given prefix as a relative path from basepath to file path
194 substr(realpath($file->getPath()), strlen(realpath($path))),
199 $classes[] = $this->prefixes
[$path] . str_replace(DIRECTORY_SEPARATOR
, '\\', $nsSuffix) . '\\' .str_replace($this->nsSeparator
, '\\', $fileName);
201 $classes[] = str_replace($this->nsSeparator
, '\\', $fileName);
213 public function findMappingFile($className)
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;
225 $prefix = $this->prefixes
[$path];
227 if (0 !== strpos($className, $prefix.'\\')) {
231 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+
1), '\\', $this->nsSeparator
).$this->fileExtension
;
232 if (is_file($filename)) {
237 throw MappingException
::mappingFileNotFound($className, substr($className, strrpos($className, '\\') +
1).$this->fileExtension
);