Rajout de doctrine/orm
[zf2.biz/galerie.git] / 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  * Locate the file that contains the metadata information for a given class name.
26  *
27  * This behavior is inpependent 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 = array();
41
42     /**
43      * The file extension of mapping documents.
44      *
45      * @var string
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
55      */
56     public function __construct($paths, $fileExtension = null)
57     {
58         $this->addPaths((array) $paths);
59         $this->fileExtension = $fileExtension;
60     }
61
62     /**
63      * Append lookup paths to metadata driver.
64      *
65      * @param array $paths
66      */
67     public function addPaths(array $paths)
68     {
69         $this->paths = array_unique(array_merge($this->paths, $paths));
70     }
71
72     /**
73      * Retrieve the defined metadata lookup paths.
74      *
75      * @return array
76      */
77     public function getPaths()
78     {
79         return $this->paths;
80     }
81
82     /**
83      * Get the file extension used to look for mapping files under
84      *
85      * @return string
86      */
87     public function getFileExtension()
88     {
89         return $this->fileExtension;
90     }
91
92     /**
93      * Set the file extension used to look for mapping files under
94      *
95      * @param string $fileExtension The file extension to set
96      * @return void
97      */
98     public function setFileExtension($fileExtension)
99     {
100         $this->fileExtension = $fileExtension;
101     }
102
103     /**
104      * {@inheritDoc}
105      */
106     public function findMappingFile($className)
107     {
108         $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
109
110         // Check whether file exists
111         foreach ($this->paths as $path) {
112             if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
113                 return $path . DIRECTORY_SEPARATOR . $fileName;
114             }
115         }
116
117         throw MappingException::mappingFileNotFound($className, $fileName);
118     }
119
120     /**
121      * {@inheritDoc}
122      */
123     public function getAllClassNames($globalBasename)
124     {
125         $classes = array();
126
127         if ($this->paths) {
128             foreach ($this->paths as $path) {
129                 if ( ! is_dir($path)) {
130                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
131                 }
132
133                 $iterator = new \RecursiveIteratorIterator(
134                     new \RecursiveDirectoryIterator($path),
135                     \RecursiveIteratorIterator::LEAVES_ONLY
136                 );
137
138                 foreach ($iterator as $file) {
139                     $fileName = $file->getBasename($this->fileExtension);
140
141                     if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
142                         continue;
143                     }
144
145                     // NOTE: All files found here means classes are not transient!
146                     $classes[] = str_replace('.', '\\', $fileName);
147                 }
148             }
149         }
150
151         return $classes;
152     }
153
154     /**
155      * {@inheritDoc}
156      */
157     public function fileExists($className)
158     {
159         $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
160
161         // Check whether file exists
162         foreach ((array) $this->paths as $path) {
163             if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
164                 return true;
165             }
166         }
167
168         return false;
169     }
170 }