source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Common/Persistence/Mapping/Driver/FileDriver.php @ 345

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

collaborator page

File size: 5.7 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\Common\Persistence\Mapping\Driver;
21
22use Doctrine\Common\Persistence\Mapping\MappingException;
23
24/**
25 * Base driver for file-based metadata drivers.
26 *
27 * A file driver operates in a mode where it loads the mapping files of individual
28 * classes on demand. This requires the user to adhere to the convention of 1 mapping
29 * file per class and the file names of the mapping files must correspond to the full
30 * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
31 *
32 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
33 * @link        www.doctrine-project.com
34 * @since       2.2
35 * @author      Benjamin Eberlei <kontakt@beberlei.de>
36 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
37 * @author      Jonathan H. Wage <jonwage@gmail.com>
38 * @author      Roman Borschel <roman@code-factory.org>
39 */
40abstract class FileDriver implements MappingDriver
41{
42    /**
43     * @var FileLocator
44     */
45    protected $locator;
46
47    /**
48     * @var array
49     */
50    protected $classCache;
51
52    /**
53     * @var string
54     */
55    protected $globalBasename;
56
57    /**
58     * Initializes a new FileDriver that looks in the given path(s) for mapping
59     * documents and operates in the specified operating mode.
60     *
61     * @param string|array|FileLocator $paths A FileLocator or one/multiple paths where mapping documents can be found.
62     * @param string $fileExtension
63     */
64    public function __construct($locator, $fileExtension = null)
65    {
66        if ($locator instanceof FileLocator) {
67            $this->locator = $locator;
68        } else {
69            $this->locator = new DefaultFileLocator((array)$locator, $fileExtension);
70        }
71    }
72
73    public function setGlobalBasename($file)
74    {
75        $this->globalBasename = $file;
76    }
77
78    public function getGlobalBasename()
79    {
80        return $this->globalBasename;
81    }
82
83    /**
84     * Get the element of schema meta data for the class from the mapping file.
85     * This will lazily load the mapping file if it is not loaded yet
86     *
87     * @return array $element  The element of schema meta data
88     */
89    public function getElement($className)
90    {
91        if ($this->classCache === null) {
92            $this->initialize();
93        }
94
95        if (isset($this->classCache[$className])) {
96            return $this->classCache[$className];
97        }
98
99        $result = $this->loadMappingFile($this->locator->findMappingFile($className));
100
101        return $result[$className];
102    }
103
104    /**
105     * Whether the class with the specified name should have its metadata loaded.
106     * This is only the case if it is either mapped as an Entity or a
107     * MappedSuperclass.
108     *
109     * @param string $className
110     * @return boolean
111     */
112    public function isTransient($className)
113    {
114        if ($this->classCache === null) {
115            $this->initialize();
116        }
117
118        if (isset($this->classCache[$className])) {
119            return false;
120        }
121
122        return !$this->locator->fileExists($className);
123    }
124
125    /**
126     * Gets the names of all mapped classes known to this driver.
127     *
128     * @return array The names of all mapped classes known to this driver.
129     */
130    public function getAllClassNames()
131    {
132        if ($this->classCache === null) {
133            $this->initialize();
134        }
135
136        $classNames = (array)$this->locator->getAllClassNames($this->globalBasename);
137        if ($this->classCache) {
138            $classNames = array_merge(array_keys($this->classCache), $classNames);
139        }
140        return $classNames;
141    }
142
143    /**
144     * Loads a mapping file with the given name and returns a map
145     * from class/entity names to their corresponding file driver elements.
146     *
147     * @param string $file The mapping file to load.
148     * @return array
149     */
150    abstract protected function loadMappingFile($file);
151
152    /**
153     * Initialize the class cache from all the global files.
154     *
155     * Using this feature adds a substantial performance hit to file drivers as
156     * more metadata has to be loaded into memory than might actually be
157     * necessary. This may not be relevant to scenarios where caching of
158     * metadata is in place, however hits very hard in scenarios where no
159     * caching is used.
160     *
161     * @return void
162     */
163    protected function initialize()
164    {
165        $this->classCache = array();
166        if (null !== $this->globalBasename) {
167            foreach ($this->locator->getPaths() as $path) {
168                $file = $path.'/'.$this->globalBasename.$this->locator->getFileExtension();
169                if (is_file($file)) {
170                    $this->classCache = array_merge(
171                        $this->classCache,
172                        $this->loadMappingFile($file)
173                    );
174                }
175            }
176        }
177    }
178}
Note: See TracBrowser for help on using the repository browser.