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\ORM\Mapping\Driver; |
---|
21 | |
---|
22 | use Doctrine\ORM\Mapping\MappingException; |
---|
23 | |
---|
24 | /** |
---|
25 | * XmlDriver that additionally looks for mapping information in a global file. |
---|
26 | * |
---|
27 | * @author Fabien Potencier <fabien@symfony.com> |
---|
28 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
29 | * @license MIT |
---|
30 | */ |
---|
31 | class SimplifiedXmlDriver extends XmlDriver |
---|
32 | { |
---|
33 | protected $_prefixes = array(); |
---|
34 | protected $_globalBasename; |
---|
35 | protected $_classCache; |
---|
36 | protected $_fileExtension = '.orm.xml'; |
---|
37 | |
---|
38 | public function __construct($prefixes) |
---|
39 | { |
---|
40 | $this->addNamespacePrefixes($prefixes); |
---|
41 | } |
---|
42 | |
---|
43 | public function setGlobalBasename($file) |
---|
44 | { |
---|
45 | $this->_globalBasename = $file; |
---|
46 | } |
---|
47 | |
---|
48 | public function getGlobalBasename() |
---|
49 | { |
---|
50 | return $this->_globalBasename; |
---|
51 | } |
---|
52 | |
---|
53 | public function addNamespacePrefixes($prefixes) |
---|
54 | { |
---|
55 | $this->_prefixes = array_merge($this->_prefixes, $prefixes); |
---|
56 | $this->addPaths(array_flip($prefixes)); |
---|
57 | } |
---|
58 | |
---|
59 | public function getNamespacePrefixes() |
---|
60 | { |
---|
61 | return $this->_prefixes; |
---|
62 | } |
---|
63 | |
---|
64 | public function isTransient($className) |
---|
65 | { |
---|
66 | if (null === $this->_classCache) { |
---|
67 | $this->initialize(); |
---|
68 | } |
---|
69 | |
---|
70 | // The mapping is defined in the global mapping file |
---|
71 | if (isset($this->_classCache[$className])) { |
---|
72 | return false; |
---|
73 | } |
---|
74 | |
---|
75 | try { |
---|
76 | $this->_findMappingFile($className); |
---|
77 | |
---|
78 | return false; |
---|
79 | } catch (MappingException $e) { |
---|
80 | return true; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | public function getAllClassNames() |
---|
85 | { |
---|
86 | if (null === $this->_classCache) { |
---|
87 | $this->initialize(); |
---|
88 | } |
---|
89 | |
---|
90 | $classes = array(); |
---|
91 | |
---|
92 | if ($this->_paths) { |
---|
93 | foreach ((array) $this->_paths as $path) { |
---|
94 | if (!is_dir($path)) { |
---|
95 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
---|
96 | } |
---|
97 | |
---|
98 | $iterator = new \RecursiveIteratorIterator( |
---|
99 | new \RecursiveDirectoryIterator($path), |
---|
100 | \RecursiveIteratorIterator::LEAVES_ONLY |
---|
101 | ); |
---|
102 | |
---|
103 | foreach ($iterator as $file) { |
---|
104 | $fileName = $file->getBasename($this->_fileExtension); |
---|
105 | |
---|
106 | if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) { |
---|
107 | continue; |
---|
108 | } |
---|
109 | |
---|
110 | // NOTE: All files found here means classes are not transient! |
---|
111 | if (isset($this->_prefixes[$path])) { |
---|
112 | $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName); |
---|
113 | } else { |
---|
114 | $classes[] = str_replace('.', '\\', $fileName); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | return array_merge($classes, array_keys($this->_classCache)); |
---|
121 | } |
---|
122 | |
---|
123 | public function getElement($className) |
---|
124 | { |
---|
125 | if (null === $this->_classCache) { |
---|
126 | $this->initialize(); |
---|
127 | } |
---|
128 | |
---|
129 | if (!isset($this->_classCache[$className])) { |
---|
130 | $this->_classCache[$className] = parent::getElement($className); |
---|
131 | } |
---|
132 | |
---|
133 | return $this->_classCache[$className]; |
---|
134 | } |
---|
135 | |
---|
136 | protected function initialize() |
---|
137 | { |
---|
138 | $this->_classCache = array(); |
---|
139 | if (null !== $this->_globalBasename) { |
---|
140 | foreach ($this->_paths as $path) { |
---|
141 | if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) { |
---|
142 | $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file)); |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | protected function _findMappingFile($className) |
---|
149 | { |
---|
150 | $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension; |
---|
151 | foreach ($this->_paths as $path) { |
---|
152 | if (!isset($this->_prefixes[$path])) { |
---|
153 | if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) { |
---|
154 | return $path.DIRECTORY_SEPARATOR.$defaultFileName; |
---|
155 | } |
---|
156 | |
---|
157 | continue; |
---|
158 | } |
---|
159 | |
---|
160 | $prefix = $this->_prefixes[$path]; |
---|
161 | |
---|
162 | if (0 !== strpos($className, $prefix.'\\')) { |
---|
163 | continue; |
---|
164 | } |
---|
165 | |
---|
166 | $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension; |
---|
167 | if (is_file($filename)) { |
---|
168 | return $filename; |
---|
169 | } |
---|
170 | |
---|
171 | throw MappingException::mappingFileNotFound($className, $filename); |
---|
172 | } |
---|
173 | |
---|
174 | throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension); |
---|
175 | } |
---|
176 | } |
---|