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