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\ClassMetadata;
23 use Doctrine\Common\Persistence\Mapping\MappingException;
26 * The StaticPHPDriver calls a static loadMetadata() method on your entity
27 * classes where you can manually populate the ClassMetadata instance.
29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 * @link www.doctrine-project.org
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
34 * @author Jonathan H. Wage <jonwage@gmail.com>
35 * @author Roman Borschel <roman@code-factory.org>
37 class StaticPHPDriver implements MappingDriver
40 * Paths of entity directories.
44 private $paths = array();
47 * Map of all class names.
56 * @param array|string $paths
58 public function __construct($paths)
60 $this->addPaths((array) $paths);
68 public function addPaths(array $paths)
70 $this->paths = array_unique(array_merge($this->paths, $paths));
76 public function loadMetadataForClass($className, ClassMetadata $metadata)
78 $className::loadMetadata($metadata);
83 * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
85 public function getAllClassNames()
87 if ($this->classNames !== null) {
88 return $this->classNames;
92 throw MappingException::pathRequired();
96 $includedFiles = array();
98 foreach ($this->paths as $path) {
100 throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
103 $iterator = new \RecursiveIteratorIterator(
104 new \RecursiveDirectoryIterator($path),
105 \RecursiveIteratorIterator::LEAVES_ONLY
108 foreach ($iterator as $file) {
109 if ($file->getBasename('.php') == $file->getBasename()) {
113 $sourceFile = realpath($file->getPathName());
114 require_once $sourceFile;
115 $includedFiles[] = $sourceFile;
119 $declared = get_declared_classes();
121 foreach ($declared as $className) {
122 $rc = new \ReflectionClass($className);
123 $sourceFile = $rc->getFileName();
124 if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
125 $classes[] = $className;
129 $this->classNames = $classes;
137 public function isTransient($className)
139 return ! method_exists($className, 'loadMetadata');