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 LGPL. 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 | * @var array |
---|
40 | */ |
---|
41 | private $drivers = array(); |
---|
42 | |
---|
43 | /** |
---|
44 | * Add a nested driver. |
---|
45 | * |
---|
46 | * @param Driver $nestedDriver |
---|
47 | * @param string $namespace |
---|
48 | */ |
---|
49 | public function addDriver(MappingDriver $nestedDriver, $namespace) |
---|
50 | { |
---|
51 | $this->drivers[$namespace] = $nestedDriver; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Get the array of nested drivers. |
---|
56 | * |
---|
57 | * @return array $drivers |
---|
58 | */ |
---|
59 | public function getDrivers() |
---|
60 | { |
---|
61 | return $this->drivers; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Loads the metadata for the specified class into the provided container. |
---|
66 | * |
---|
67 | * @param string $className |
---|
68 | * @param ClassMetadataInfo $metadata |
---|
69 | */ |
---|
70 | public function loadMetadataForClass($className, ClassMetadata $metadata) |
---|
71 | { |
---|
72 | foreach ($this->drivers as $namespace => $driver) { |
---|
73 | if (strpos($className, $namespace) === 0) { |
---|
74 | $driver->loadMetadataForClass($className, $metadata); |
---|
75 | return; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers)); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Gets the names of all mapped classes known to this driver. |
---|
84 | * |
---|
85 | * @return array The names of all mapped classes known to this driver. |
---|
86 | */ |
---|
87 | public function getAllClassNames() |
---|
88 | { |
---|
89 | $classNames = array(); |
---|
90 | $driverClasses = array(); |
---|
91 | foreach ($this->drivers AS $namespace => $driver) { |
---|
92 | $oid = spl_object_hash($driver); |
---|
93 | if (!isset($driverClasses[$oid])) { |
---|
94 | $driverClasses[$oid] = $driver->getAllClassNames(); |
---|
95 | } |
---|
96 | |
---|
97 | foreach ($driverClasses[$oid] AS $className) { |
---|
98 | if (strpos($className, $namespace) === 0) { |
---|
99 | $classNames[$className] = true; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | return array_keys($classNames); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Whether the class with the specified name should have its metadata loaded. |
---|
108 | * |
---|
109 | * This is only the case for non-transient classes either mapped as an Entity or MappedSuperclass. |
---|
110 | * |
---|
111 | * @param string $className |
---|
112 | * @return boolean |
---|
113 | */ |
---|
114 | public function isTransient($className) |
---|
115 | { |
---|
116 | foreach ($this->drivers AS $namespace => $driver) { |
---|
117 | if (strpos($className, $namespace) === 0) { |
---|
118 | return $driver->isTransient($className); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | // class isTransient, i.e. not an entity or mapped superclass |
---|
123 | return true; |
---|
124 | } |
---|
125 | } |
---|