Failed Conditions
Pull Request — master (#1)
by Jonathan
10:52
created

StaticPHPDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
namespace Doctrine\Common\Persistence\Mapping\Driver;
3
4
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
5
use Doctrine\Common\Persistence\Mapping\MappingException;
6
7
/**
8
 * The StaticPHPDriver calls a static loadMetadata() method on your entity
9
 * classes where you can manually populate the ClassMetadata instance.
10
 *
11
 * @link   www.doctrine-project.org
12
 * @since  2.2
13
 * @author Benjamin Eberlei <[email protected]>
14
 * @author Guilherme Blanco <[email protected]>
15
 * @author Jonathan H. Wage <[email protected]>
16
 * @author Roman Borschel <[email protected]>
17
 */
18
class StaticPHPDriver implements MappingDriver
19
{
20
    /**
21
     * Paths of entity directories.
22
     *
23
     * @var array
24
     */
25
    private $paths = [];
26
27
    /**
28
     * Map of all class names.
29
     *
30
     * @var array
31
     */
32
    private $classNames;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param array|string $paths
38
     */
39
    public function __construct($paths)
40
    {
41
        $this->addPaths((array) $paths);
42
    }
43
44
    /**
45
     * Adds paths.
46
     *
47
     * @param array $paths
48
     *
49
     * @return void
50
     */
51
    public function addPaths(array $paths)
52
    {
53
        $this->paths = array_unique(array_merge($this->paths, $paths));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function loadMetadataForClass($className, ClassMetadata $metadata)
60
    {
61
        $className::loadMetadata($metadata);
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
67
     */
68
    public function getAllClassNames()
69
    {
70
        if ($this->classNames !== null) {
71
            return $this->classNames;
72
        }
73
74
        if ( ! $this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
            throw MappingException::pathRequired();
76
        }
77
78
        $classes       = [];
79
        $includedFiles = [];
80
81
        foreach ($this->paths as $path) {
82
            if ( ! is_dir($path)) {
83
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
84
            }
85
86
            $iterator = new \RecursiveIteratorIterator(
87
                new \RecursiveDirectoryIterator($path),
88
                \RecursiveIteratorIterator::LEAVES_ONLY
89
            );
90
91
            foreach ($iterator as $file) {
92
                if ($file->getBasename('.php') == $file->getBasename()) {
93
                    continue;
94
                }
95
96
                $sourceFile = realpath($file->getPathName());
97
                require_once $sourceFile;
98
                $includedFiles[] = $sourceFile;
99
            }
100
        }
101
102
        $declared = get_declared_classes();
103
104
        foreach ($declared as $className) {
105
            $rc         = new \ReflectionClass($className);
106
            $sourceFile = $rc->getFileName();
107
            if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
108
                $classes[] = $className;
109
            }
110
        }
111
112
        $this->classNames = $classes;
113
114
        return $classes;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function isTransient($className)
121
    {
122
        return ! method_exists($className, 'loadMetadata');
123
    }
124
}
125