Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / Driver / MappingDriverChain.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\Driver\MappingDriver,
23     Doctrine\Common\Persistence\Mapping\ClassMetadata,
24     Doctrine\Common\Persistence\Mapping\MappingException;
25
26 /**
27  * The DriverChain allows you to add multiple other mapping drivers for
28  * certain namespaces
29  *
30  * @since  2.2
31  * @author Benjamin Eberlei <kontakt@beberlei.de>
32  * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
33  * @author Jonathan H. Wage <jonwage@gmail.com>
34  * @author Roman Borschel <roman@code-factory.org>
35  */
36 class MappingDriverChain implements MappingDriver
37 {
38     /**
39      * The default driver
40      *
41      * @var MappingDriver
42      */
43     private $defaultDriver;
44
45     /**
46      * @var array
47      */
48     private $drivers = array();
49
50     /**
51      * Get the default driver.
52      *
53      * @return MappingDriver|null
54      */
55     public function getDefaultDriver()
56     {
57         return $this->defaultDriver;
58     }
59
60     /**
61      * Set the default driver.
62      *
63      * @param MappingDriver $driver
64      */
65     public function setDefaultDriver(MappingDriver $driver)
66     {
67         $this->defaultDriver = $driver;
68     }
69
70     /**
71      * Add a nested driver.
72      *
73      * @param MappingDriver $nestedDriver
74      * @param string $namespace
75      */
76     public function addDriver(MappingDriver $nestedDriver, $namespace)
77     {
78         $this->drivers[$namespace] = $nestedDriver;
79     }
80
81     /**
82      * Get the array of nested drivers.
83      *
84      * @return array $drivers
85      */
86     public function getDrivers()
87     {
88         return $this->drivers;
89     }
90
91     /**
92      * Loads the metadata for the specified class into the provided container.
93      *
94      * @param string $className
95      * @param ClassMetadata $metadata
96      *
97      * @throws MappingException
98      */
99     public function loadMetadataForClass($className, ClassMetadata $metadata)
100     {
101         /* @var $driver MappingDriver */
102         foreach ($this->drivers as $namespace => $driver) {
103             if (strpos($className, $namespace) === 0) {
104                 $driver->loadMetadataForClass($className, $metadata);
105                 return;
106             }
107         }
108
109         if (null !== $this->defaultDriver) {
110             $this->defaultDriver->loadMetadataForClass($className, $metadata);
111             return;
112         }
113
114         throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
115     }
116
117     /**
118      * Gets the names of all mapped classes known to this driver.
119      *
120      * @return array The names of all mapped classes known to this driver.
121      */
122     public function getAllClassNames()
123     {
124         $classNames = array();
125         $driverClasses = array();
126
127         /* @var $driver MappingDriver */
128         foreach ($this->drivers AS $namespace => $driver) {
129             $oid = spl_object_hash($driver);
130
131             if (!isset($driverClasses[$oid])) {
132                 $driverClasses[$oid] = $driver->getAllClassNames();
133             }
134
135             foreach ($driverClasses[$oid] AS $className) {
136                 if (strpos($className, $namespace) === 0) {
137                     $classNames[$className] = true;
138                 }
139             }
140         }
141
142         return array_keys($classNames);
143     }
144
145     /**
146      * Whether the class with the specified name should have its metadata loaded.
147      *
148      * This is only the case for non-transient classes either mapped as an Entity or MappedSuperclass.
149      *
150      * @param string $className
151      * @return boolean
152      */
153     public function isTransient($className)
154     {
155         /* @var $driver MappingDriver */
156         foreach ($this->drivers AS $namespace => $driver) {
157             if (strpos($className, $namespace) === 0) {
158                 return $driver->isTransient($className);
159             }
160         }
161
162         if ($this->defaultDriver !== null) {
163             return $this->defaultDriver->isTransient($className);
164         }
165
166         return true;
167     }
168 }