~ubuntu-branches/ubuntu/utopic/php-doctrine-common/utopic

« back to all changes in this revision

Viewing changes to lib/Doctrine/Common/Persistence/Mapping/Driver/StaticPHPDriver.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-06-15 11:26:00 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140615112600-sg4mgpwq9sfg4mre
Tags: 2.4.2-2
* Upload to unstable
* No tests if DEB_BUILD_OPTIONS contains nocheck

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 MIT license. 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\ClassMetadata;
 
23
use Doctrine\Common\Persistence\Mapping\MappingException;
 
24
 
 
25
/**
 
26
 * The StaticPHPDriver calls a static loadMetadata() method on your entity
 
27
 * classes where you can manually populate the ClassMetadata instance.
 
28
 *
 
29
 * @link   www.doctrine-project.org
 
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 StaticPHPDriver implements MappingDriver
 
37
{
 
38
    /**
 
39
     * Paths of entity directories.
 
40
     *
 
41
     * @var array
 
42
     */
 
43
    private $paths = array();
 
44
 
 
45
    /**
 
46
     * Map of all class names.
 
47
     *
 
48
     * @var array
 
49
     */
 
50
    private $classNames;
 
51
 
 
52
    /**
 
53
     * Constructor.
 
54
     *
 
55
     * @param array|string $paths
 
56
     */
 
57
    public function __construct($paths)
 
58
    {
 
59
        $this->addPaths((array) $paths);
 
60
    }
 
61
 
 
62
    /**
 
63
     * Adds paths.
 
64
     *
 
65
     * @param array $paths
 
66
     *
 
67
     * @return void
 
68
     */
 
69
    public function addPaths(array $paths)
 
70
    {
 
71
        $this->paths = array_unique(array_merge($this->paths, $paths));
 
72
    }
 
73
 
 
74
    /**
 
75
     * {@inheritdoc}
 
76
     */
 
77
    public function loadMetadataForClass($className, ClassMetadata $metadata)
 
78
    {
 
79
        $className::loadMetadata($metadata);
 
80
    }
 
81
 
 
82
    /**
 
83
     * {@inheritDoc}
 
84
     * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
 
85
     */
 
86
    public function getAllClassNames()
 
87
    {
 
88
        if ($this->classNames !== null) {
 
89
            return $this->classNames;
 
90
        }
 
91
 
 
92
        if (!$this->paths) {
 
93
            throw MappingException::pathRequired();
 
94
        }
 
95
 
 
96
        $classes = array();
 
97
        $includedFiles = array();
 
98
 
 
99
        foreach ($this->paths as $path) {
 
100
            if (!is_dir($path)) {
 
101
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
 
102
            }
 
103
 
 
104
            $iterator = new \RecursiveIteratorIterator(
 
105
                new \RecursiveDirectoryIterator($path),
 
106
                \RecursiveIteratorIterator::LEAVES_ONLY
 
107
            );
 
108
 
 
109
            foreach ($iterator as $file) {
 
110
                if ($file->getBasename('.php') == $file->getBasename()) {
 
111
                    continue;
 
112
                }
 
113
 
 
114
                $sourceFile = realpath($file->getPathName());
 
115
                require_once $sourceFile;
 
116
                $includedFiles[] = $sourceFile;
 
117
            }
 
118
        }
 
119
 
 
120
        $declared = get_declared_classes();
 
121
 
 
122
        foreach ($declared as $className) {
 
123
            $rc = new \ReflectionClass($className);
 
124
            $sourceFile = $rc->getFileName();
 
125
            if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
 
126
                $classes[] = $className;
 
127
            }
 
128
        }
 
129
 
 
130
        $this->classNames = $classes;
 
131
 
 
132
        return $classes;
 
133
    }
 
134
 
 
135
    /**
 
136
     * {@inheritdoc}
 
137
     */
 
138
    public function isTransient($className)
 
139
    {
 
140
        return ! method_exists($className, 'loadMetadata');
 
141
    }
 
142
}