composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / SymfonyFileLocator.php
blob4588cfec8893cf63c2e8b728745e06624e257135
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.
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;
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.
29 * @author Fabien Potencier <fabien@symfony.com>
30 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 * @license MIT
33 class SymfonyFileLocator implements FileLocator
35 /**
36 * The paths where to look for mapping files.
38 * @var array
40 protected $paths = [];
42 /**
43 * A map of mapping directory path to namespace prefix used to expand class shortnames.
45 * @var array
47 protected $prefixes = [];
49 /**
50 * File extension that is searched for.
52 * @var string|null
54 protected $fileExtension;
56 /**
57 * Represents PHP namespace delimiters when looking for files
59 * @var string
61 private $nsSeparator;
63 /**
64 * Constructor.
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;
82 /**
83 * Adds Namespace Prefixes.
85 * @param array $prefixes
87 * @return void
89 public function addNamespacePrefixes(array $prefixes)
91 $this->prefixes = array_merge($this->prefixes, $prefixes);
92 $this->paths = array_merge($this->paths, array_keys($prefixes));
95 /**
96 * Gets Namespace Prefixes.
98 * @return array
100 public function getNamespacePrefixes()
102 return $this->prefixes;
106 * {@inheritDoc}
108 public function getPaths()
110 return $this->paths;
114 * {@inheritDoc}
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.
126 * @return void
128 public function setFileExtension($fileExtension)
130 $this->fileExtension = $fileExtension;
134 * {@inheritDoc}
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)) {
143 return true;
146 continue;
149 $prefix = $this->prefixes[$path];
151 if (0 !== strpos($className, $prefix.'\\')) {
152 continue;
155 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator).$this->fileExtension;
156 if (is_file($filename)) {
157 return true;
161 return false;
165 * {@inheritDoc}
167 public function getAllClassNames($globalBasename = null)
169 $classes = [];
171 if ($this->paths) {
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) {
186 continue;
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
193 $nsSuffix = strtr(
194 substr(realpath($file->getPath()), strlen(realpath($path))),
195 $this->nsSeparator,
196 '\\'
199 $classes[] = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' .str_replace($this->nsSeparator, '\\', $fileName);
200 } else {
201 $classes[] = str_replace($this->nsSeparator, '\\', $fileName);
207 return $classes;
211 * {@inheritDoc}
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;
222 continue;
225 $prefix = $this->prefixes[$path];
227 if (0 !== strpos($className, $prefix.'\\')) {
228 continue;
231 $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator ).$this->fileExtension;
232 if (is_file($filename)) {
233 return $filename;
237 throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);