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 = array();
43 * A map of mapping directory path to namespace prefix used to expand class shortnames.
47 protected $prefixes = array();
50 * File extension that is searched for.
54 protected $fileExtension;
59 * @param array $prefixes
60 * @param string|null $fileExtension
62 public function __construct(array $prefixes, $fileExtension = null)
64 $this->addNamespacePrefixes($prefixes);
65 $this->fileExtension = $fileExtension;
69 * Add Namespace Prefixes
71 * @param array $prefixes
73 public function addNamespacePrefixes(array $prefixes)
75 $this->prefixes = array_merge($this->prefixes, $prefixes);
76 $this->paths = array_merge($this->paths, array_keys($prefixes));
80 * Get Namespace Prefixes
84 public function getNamespacePrefixes()
86 return $this->prefixes;
92 public function getPaths()
100 public function getFileExtension()
102 return $this->fileExtension;
106 * Set the file extension used to look for mapping files under
108 * @param string $fileExtension The file extension to set
111 public function setFileExtension($fileExtension)
113 $this->fileExtension = $fileExtension;
119 public function fileExists($className)
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)) {
132 $prefix = $this->prefixes[$path];
134 if (0 !== strpos($className, $prefix.'\\')) {
138 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
139 return is_file($filename);
148 public function getAllClassNames($globalBasename = null)
153 foreach ((array) $this->paths as $path) {
154 if (!is_dir($path)) {
155 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
158 $iterator = new \RecursiveIteratorIterator(
159 new \RecursiveDirectoryIterator($path),
160 \RecursiveIteratorIterator::LEAVES_ONLY
163 foreach ($iterator as $file) {
164 $fileName = $file->getBasename($this->fileExtension);
166 if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
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);
174 $classes[] = str_replace('.', '\\', $fileName);
186 public function findMappingFile($className)
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;
198 $prefix = $this->prefixes[$path];
200 if (0 !== strpos($className, $prefix.'\\')) {
204 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
205 if (is_file($filename)) {
209 throw MappingException::mappingFileNotFound($className, $filename);
212 throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);