Version 1
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / DefaultFileLocator.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  * Locates the file that contains the metadata information for a given class name.
26  *
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.
29  *
30  * @author Benjamin Eberlei <kontakt@beberlei.de>
31  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
32  */
33 class DefaultFileLocator implements FileLocator
34 {
35     /**
36      * The paths where to look for mapping files.
37      *
38      * @var array
39      */
40     protected $paths = [];
41
42     /**
43      * The file extension of mapping documents.
44      *
45      * @var string|null
46      */
47     protected $fileExtension;
48
49     /**
50      * Initializes a new FileDriver that looks in the given path(s) for mapping
51      * documents and operates in the specified operating mode.
52      *
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.
55      */
56     public function __construct($paths, $fileExtension = null)
57     {
58         $this->addPaths((array) $paths);
59         $this->fileExtension = $fileExtension;
60     }
61
62     /**
63      * Appends lookup paths to metadata driver.
64      *
65      * @param array $paths
66      *
67      * @return void
68      */
69     public function addPaths(array $paths)
70     {
71         $this->paths = array_unique(array_merge($this->paths, $paths));
72     }
73
74     /**
75      * Retrieves the defined metadata lookup paths.
76      *
77      * @return array
78      */
79     public function getPaths()
80     {
81         return $this->paths;
82     }
83
84     /**
85      * Gets the file extension used to look for mapping files under.
86      *
87      * @return string|null
88      */
89     public function getFileExtension()
90     {
91         return $this->fileExtension;
92     }
93
94     /**
95      * Sets the file extension used to look for mapping files under.
96      *
97      * @param string|null $fileExtension The file extension to set.
98      *
99      * @return void
100      */
101     public function setFileExtension($fileExtension)
102     {
103         $this->fileExtension = $fileExtension;
104     }
105
106     /**
107      * {@inheritDoc}
108      */
109     public function findMappingFile($className)
110     {
111         $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
112
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;
117             }
118         }
119
120         throw MappingException::mappingFileNotFound($className, $fileName);
121     }
122
123     /**
124      * {@inheritDoc}
125      */
126     public function getAllClassNames($globalBasename)
127     {
128         $classes = [];
129
130         if ($this->paths) {
131             foreach ($this->paths as $path) {
132                 if ( ! is_dir($path)) {
133                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
134                 }
135
136                 $iterator = new \RecursiveIteratorIterator(
137                     new \RecursiveDirectoryIterator($path),
138                     \RecursiveIteratorIterator::LEAVES_ONLY
139                 );
140
141                 foreach ($iterator as $file) {
142                     $fileName = $file->getBasename($this->fileExtension);
143
144                     if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
145                         continue;
146                     }
147
148                     // NOTE: All files found here means classes are not transient!
149                     $classes[] = str_replace('.', '\\', $fileName);
150                 }
151             }
152         }
153
154         return $classes;
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     public function fileExists($className)
161     {
162         $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
163
164         // Check whether file exists
165         foreach ((array) $this->paths as $path) {
166             if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
167                 return true;
168             }
169         }
170
171         return false;
172     }
173 }