composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / DefaultFileLocator.php
blob6ed7f6d0d59a92a8492092096668b0582fe3be54
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 * Locates the file that contains the metadata information for a given class name.
27 * This behavior is independent of the actual content of the file. It just detects
28 * the file which is responsible for the given class name.
30 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
33 class DefaultFileLocator implements FileLocator
35 /**
36 * The paths where to look for mapping files.
38 * @var array
40 protected $paths = [];
42 /**
43 * The file extension of mapping documents.
45 * @var string|null
47 protected $fileExtension;
49 /**
50 * Initializes a new FileDriver that looks in the given path(s) for mapping
51 * documents and operates in the specified operating mode.
53 * @param string|array $paths One or multiple paths where mapping documents can be found.
54 * @param string|null $fileExtension The file extension of mapping documents, usually prefixed with a dot.
56 public function __construct($paths, $fileExtension = null)
58 $this->addPaths((array) $paths);
59 $this->fileExtension = $fileExtension;
62 /**
63 * Appends lookup paths to metadata driver.
65 * @param array $paths
67 * @return void
69 public function addPaths(array $paths)
71 $this->paths = array_unique(array_merge($this->paths, $paths));
74 /**
75 * Retrieves the defined metadata lookup paths.
77 * @return array
79 public function getPaths()
81 return $this->paths;
84 /**
85 * Gets the file extension used to look for mapping files under.
87 * @return string|null
89 public function getFileExtension()
91 return $this->fileExtension;
94 /**
95 * Sets the file extension used to look for mapping files under.
97 * @param string|null $fileExtension The file extension to set.
99 * @return void
101 public function setFileExtension($fileExtension)
103 $this->fileExtension = $fileExtension;
107 * {@inheritDoc}
109 public function findMappingFile($className)
111 $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
113 // Check whether file exists
114 foreach ($this->paths as $path) {
115 if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
116 return $path . DIRECTORY_SEPARATOR . $fileName;
120 throw MappingException::mappingFileNotFound($className, $fileName);
124 * {@inheritDoc}
126 public function getAllClassNames($globalBasename)
128 $classes = [];
130 if ($this->paths) {
131 foreach ($this->paths as $path) {
132 if ( ! is_dir($path)) {
133 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
136 $iterator = new \RecursiveIteratorIterator(
137 new \RecursiveDirectoryIterator($path),
138 \RecursiveIteratorIterator::LEAVES_ONLY
141 foreach ($iterator as $file) {
142 $fileName = $file->getBasename($this->fileExtension);
144 if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
145 continue;
148 // NOTE: All files found here means classes are not transient!
149 $classes[] = str_replace('.', '\\', $fileName);
154 return $classes;
158 * {@inheritDoc}
160 public function fileExists($className)
162 $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
164 // Check whether file exists
165 foreach ((array) $this->paths as $path) {
166 if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
167 return true;
171 return false;