Failed Conditions
Pull Request — 1.3.x (#71)
by Grégoire
02:19
created

AnnotationDriver::addExcludePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Doctrine\Persistence\Mapping\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Doctrine\Persistence\Mapping\MappingException;
7
use FilesystemIterator;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use RecursiveRegexIterator;
11
use ReflectionClass;
12
use RegexIterator;
13
use function array_merge;
14
use function array_unique;
15
use function class_exists;
16
use function get_class;
17
use function get_declared_classes;
18
use function in_array;
19
use function is_dir;
20
use function preg_match;
21
use function preg_quote;
22
use function realpath;
23
use function str_replace;
24
use function strpos;
25
26
/**
27
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
28
 */
29
abstract class AnnotationDriver implements MappingDriver
30
{
31
    /**
32
     * The annotation reader.
33
     *
34
     * @var Reader
35
     */
36
    protected $reader;
37
38
    /**
39
     * The paths where to look for mapping files.
40
     *
41
     * @var string[]
42
     */
43
    protected $paths = [];
44
45
    /**
46
     * The paths excluded from path where to look for mapping files.
47
     *
48
     * @var string[]
49
     */
50
    protected $excludePaths = [];
51
52
    /**
53
     * The file extension of mapping documents.
54
     *
55
     * @var string
56
     */
57
    protected $fileExtension = '.php';
58
59
    /**
60
     * Cache for AnnotationDriver#getAllClassNames().
61
     *
62
     * @var string[]|null
63
     */
64
    protected $classNames;
65
66
    /**
67
     * Name of the entity annotations as keys.
68
     *
69
     * @var string[]
70
     */
71
    protected $entityAnnotationClasses = [];
72
73
    /**
74
     * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
75
     * docblock annotations.
76
     *
77
     * @param Reader               $reader The AnnotationReader to use, duck-typed.
78
     * @param string|string[]|null $paths  One or multiple paths where mapping classes can be found.
79
     */
80 1
    public function __construct($reader, $paths = null)
81
    {
82 1
        $this->reader = $reader;
83 1
        if (! $paths) {
84
            return;
85
        }
86
87 1
        $this->addPaths((array) $paths);
88 1
    }
89
90
    /**
91
     * Appends lookup paths to metadata driver.
92
     *
93
     * @param string[] $paths
94
     *
95
     * @return void
96
     */
97 1
    public function addPaths(array $paths)
98
    {
99 1
        $this->paths = array_unique(array_merge($this->paths, $paths));
100 1
    }
101
102
    /**
103
     * Retrieves the defined metadata lookup paths.
104
     *
105
     * @return string[]
106
     */
107
    public function getPaths()
108
    {
109
        return $this->paths;
110
    }
111
112
    /**
113
     * Append exclude lookup paths to metadata driver.
114
     *
115
     * @param string[] $paths
116
     */
117
    public function addExcludePaths(array $paths)
118
    {
119
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
120
    }
121
122
    /**
123
     * Retrieve the defined metadata lookup exclude paths.
124
     *
125
     * @return string[]
126
     */
127
    public function getExcludePaths()
128
    {
129
        return $this->excludePaths;
130
    }
131
132
    /**
133
     * Retrieve the current annotation reader
134
     *
135
     * @return Reader
136
     */
137
    public function getReader()
138
    {
139
        return $this->reader;
140
    }
141
142
    /**
143
     * Gets the file extension used to look for mapping files under.
144
     *
145
     * @return string
146
     */
147
    public function getFileExtension()
148
    {
149
        return $this->fileExtension;
150
    }
151
152
    /**
153
     * Sets the file extension used to look for mapping files under.
154
     *
155
     * @param string $fileExtension The file extension to set.
156
     *
157
     * @return void
158
     */
159
    public function setFileExtension($fileExtension)
160
    {
161
        $this->fileExtension = $fileExtension;
162
    }
163
164
    /**
165
     * Returns whether the class with the specified name is transient. Only non-transient
166
     * classes, that is entities and mapped superclasses, should have their metadata loaded.
167
     *
168
     * A class is non-transient if it is annotated with an annotation
169
     * from the {@see AnnotationDriver::entityAnnotationClasses}.
170
     *
171
     * @param string $className
172
     *
173
     * @return bool
174
     */
175 1
    public function isTransient($className)
176
    {
177 1
        $classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));
178
179 1
        foreach ($classAnnotations as $annot) {
180 1
            if (isset($this->entityAnnotationClasses[get_class($annot)])) {
181 1
                return false;
182
            }
183
        }
184
185 1
        return true;
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191 1
    public function getAllClassNames()
192
    {
193 1
        if ($this->classNames !== null) {
194
            return $this->classNames;
195
        }
196
197 1
        if (! $this->paths) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->paths of type string[] 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...
198
            throw MappingException::pathRequired();
199
        }
200
201 1
        $classes       = [];
202 1
        $includedFiles = [];
203
204 1
        foreach ($this->paths as $path) {
205 1
            if (! is_dir($path)) {
206
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
207
            }
208
209 1
            $iterator = new RegexIterator(
210 1
                new RecursiveIteratorIterator(
211 1
                    new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
212 1
                    RecursiveIteratorIterator::LEAVES_ONLY
213
                ),
214 1
                '/^.+' . preg_quote($this->fileExtension) . '$/i',
215 1
                RecursiveRegexIterator::GET_MATCH
216
            );
217
218 1
            foreach ($iterator as $file) {
219 1
                $sourceFile = $file[0];
220
221 1
                if (! preg_match('(^phar:)i', $sourceFile)) {
222 1
                    $sourceFile = realpath($sourceFile);
223
                }
224
225 1
                foreach ($this->excludePaths as $excludePath) {
226
                    $exclude = str_replace('\\', '/', realpath($excludePath));
227
                    $current = str_replace('\\', '/', $sourceFile);
228
229
                    if (strpos($current, $exclude) !== false) {
230
                        continue 2;
231
                    }
232
                }
233
234 1
                require_once $sourceFile;
235
236 1
                $includedFiles[] = $sourceFile;
237
            }
238
        }
239
240 1
        $declared = get_declared_classes();
241
242 1
        foreach ($declared as $className) {
243 1
            $rc         = new ReflectionClass($className);
244 1
            $sourceFile = $rc->getFileName();
245 1
            if (! in_array($sourceFile, $includedFiles) || $this->isTransient($className)) {
246 1
                continue;
247
            }
248
249 1
            $classes[] = $className;
250
        }
251
252 1
        $this->classNames = $classes;
253
254 1
        return $classes;
255
    }
256
}
257
258
class_exists(\Doctrine\Persistence\Mapping\Driver\AnnotationDriver::class);
259