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\Driver\MappingDriver,
23 Doctrine\Common\Persistence\Mapping\ClassMetadata,
24 Doctrine\Common\Persistence\Mapping\MappingException;
27 * The DriverChain allows you to add multiple other mapping drivers for
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>
36 class MappingDriverChain implements MappingDriver
43 private $defaultDriver;
48 private $drivers = array();
51 * Get the default driver.
53 * @return MappingDriver|null
55 public function getDefaultDriver()
57 return $this->defaultDriver;
61 * Set the default driver.
63 * @param MappingDriver $driver
65 public function setDefaultDriver(MappingDriver $driver)
67 $this->defaultDriver = $driver;
71 * Add a nested driver.
73 * @param MappingDriver $nestedDriver
74 * @param string $namespace
76 public function addDriver(MappingDriver $nestedDriver, $namespace)
78 $this->drivers[$namespace] = $nestedDriver;
82 * Get the array of nested drivers.
84 * @return array $drivers
86 public function getDrivers()
88 return $this->drivers;
92 * Loads the metadata for the specified class into the provided container.
94 * @param string $className
95 * @param ClassMetadata $metadata
97 * @throws MappingException
99 public function loadMetadataForClass($className, ClassMetadata $metadata)
101 /* @var $driver MappingDriver */
102 foreach ($this->drivers as $namespace => $driver) {
103 if (strpos($className, $namespace) === 0) {
104 $driver->loadMetadataForClass($className, $metadata);
109 if (null !== $this->defaultDriver) {
110 $this->defaultDriver->loadMetadataForClass($className, $metadata);
114 throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
118 * Gets the names of all mapped classes known to this driver.
120 * @return array The names of all mapped classes known to this driver.
122 public function getAllClassNames()
124 $classNames = array();
125 $driverClasses = array();
127 /* @var $driver MappingDriver */
128 foreach ($this->drivers AS $namespace => $driver) {
129 $oid = spl_object_hash($driver);
131 if (!isset($driverClasses[$oid])) {
132 $driverClasses[$oid] = $driver->getAllClassNames();
135 foreach ($driverClasses[$oid] AS $className) {
136 if (strpos($className, $namespace) === 0) {
137 $classNames[$className] = true;
142 return array_keys($classNames);
146 * Whether the class with the specified name should have its metadata loaded.
148 * This is only the case for non-transient classes either mapped as an Entity or MappedSuperclass.
150 * @param string $className
153 public function isTransient($className)
155 /* @var $driver MappingDriver */
156 foreach ($this->drivers AS $namespace => $driver) {
157 if (strpos($className, $namespace) === 0) {
158 return $driver->isTransient($className);
162 if ($this->defaultDriver !== null) {
163 return $this->defaultDriver->isTransient($className);