source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Mapping/Driver/SimplifiedYamlDriver.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 5.6 KB
Line 
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
20namespace Doctrine\ORM\Mapping\Driver;
21
22use Doctrine\ORM\Mapping\MappingException;
23
24/**
25 * YamlDriver 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 */
31class SimplifiedYamlDriver extends YamlDriver
32{
33    protected $_prefixes = array();
34    protected $_globalBasename;
35    protected $_classCache;
36    protected $_fileExtension = '.orm.yml';
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 addNamespacePrefix($prefix, $path)
60    {
61        $this->_prefixes[$path] = $prefix;
62    }
63
64    public function getNamespacePrefixes()
65    {
66        return $this->_prefixes;
67    }
68
69    public function isTransient($className)
70    {
71        if (null === $this->_classCache) {
72            $this->initialize();
73        }
74
75        // The mapping is defined in the global mapping file
76        if (isset($this->_classCache[$className])) {
77            return false;
78        }
79
80        try {
81            $this->_findMappingFile($className);
82
83            return false;
84        } catch (MappingException $e) {
85            return true;
86        }
87    }
88
89    public function getAllClassNames()
90    {
91        if (null === $this->_classCache) {
92            $this->initialize();
93        }
94
95        $classes = array();
96
97        if ($this->_paths) {
98            foreach ((array) $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                    $fileName = $file->getBasename($this->_fileExtension);
110
111                    if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
112                        continue;
113                    }
114
115                    // NOTE: All files found here means classes are not transient!
116                    if (isset($this->_prefixes[$path])) {
117                        $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
118                    } else {
119                        $classes[] = str_replace('.', '\\', $fileName);
120                    }
121                }
122            }
123        }
124
125        return array_merge($classes, array_keys($this->_classCache));
126    }
127
128    public function getElement($className)
129    {
130        if (null === $this->_classCache) {
131            $this->initialize();
132        }
133
134        if (!isset($this->_classCache[$className])) {
135            $this->_classCache[$className] = parent::getElement($className);
136        }
137
138        return $this->_classCache[$className];
139    }
140
141    protected function initialize()
142    {
143        $this->_classCache = array();
144        if (null !== $this->_globalBasename) {
145            foreach ($this->_paths as $path) {
146                if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
147                    $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
148                }
149            }
150        }
151    }
152
153    protected function _findMappingFile($className)
154    {
155        $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
156        foreach ($this->_paths as $path) {
157            if (!isset($this->_prefixes[$path])) {
158                if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
159                    return $path.DIRECTORY_SEPARATOR.$defaultFileName;
160                }
161
162                continue;
163            }
164
165            $prefix = $this->_prefixes[$path];
166
167            if (0 !== strpos($className, $prefix.'\\')) {
168                continue;
169            }
170
171            $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
172            if (is_file($filename)) {
173                return $filename;
174            }
175
176            throw MappingException::mappingFileNotFound($className, $filename);
177        }
178
179        throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
180    }
181}
Note: See TracBrowser for help on using the repository browser.