Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / StaticPHPDriver.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\ClassMetadata;
23 use Doctrine\Common\Persistence\Mapping\MappingException;
24
25 /**
26  * The StaticPHPDriver calls a static loadMetadata() method on your entity
27  * classes where you can manually populate the ClassMetadata instance.
28  *
29  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
30  * @link        www.doctrine-project.org
31  * @since       2.2
32  * @author      Benjamin Eberlei <kontakt@beberlei.de>
33  * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
34  * @author      Jonathan H. Wage <jonwage@gmail.com>
35  * @author      Roman Borschel <roman@code-factory.org>
36  */
37 class StaticPHPDriver implements MappingDriver
38 {
39     /**
40      * Paths of entity directories.
41      *
42      * @var array
43      */
44     private $paths = array();
45
46     /**
47      * Map of all class names.
48      *
49      * @var array
50      */
51     private $classNames;
52
53     /**
54      * Constructor
55      *
56      * @param array|string $paths
57      */
58     public function __construct($paths)
59     {
60         $this->addPaths((array) $paths);
61     }
62
63     /**
64      * Add paths
65      *
66      * @param array $paths
67      */
68     public function addPaths(array $paths)
69     {
70         $this->paths = array_unique(array_merge($this->paths, $paths));
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     public function loadMetadataForClass($className, ClassMetadata $metadata)
77     {
78         $className::loadMetadata($metadata);
79     }
80
81     /**
82      * {@inheritDoc}
83      * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
84      */
85     public function getAllClassNames()
86     {
87         if ($this->classNames !== null) {
88             return $this->classNames;
89         }
90
91         if (!$this->paths) {
92             throw MappingException::pathRequired();
93         }
94
95         $classes = array();
96         $includedFiles = array();
97
98         foreach ($this->paths as $path) {
99             if (!is_dir($path)) {
100                 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
101             }
102
103             $iterator = new \RecursiveIteratorIterator(
104                 new \RecursiveDirectoryIterator($path),
105                 \RecursiveIteratorIterator::LEAVES_ONLY
106             );
107
108             foreach ($iterator as $file) {
109                 if ($file->getBasename('.php') == $file->getBasename()) {
110                     continue;
111                 }
112
113                 $sourceFile = realpath($file->getPathName());
114                 require_once $sourceFile;
115                 $includedFiles[] = $sourceFile;
116             }
117         }
118
119         $declared = get_declared_classes();
120
121         foreach ($declared as $className) {
122             $rc = new \ReflectionClass($className);
123             $sourceFile = $rc->getFileName();
124             if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
125                 $classes[] = $className;
126             }
127         }
128
129         $this->classNames = $classes;
130
131         return $classes;
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     public function isTransient($className)
138     {
139         return ! method_exists($className, 'loadMetadata');
140     }
141 }