AbstractAnnotationDriver::addExcludePaths()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Metadata\Driver;
12
13
use Cubiche\Core\Metadata\Exception\MappingException;
14
use Cubiche\Tests\Generator\ClassUtils;
15
use Doctrine\Common\Annotations\Reader;
16
17
/**
18
 * AbstractAnnotationDriver class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
abstract class AbstractAnnotationDriver implements DriverInterface
23
{
24
    /**
25
     * @var Reader
26
     */
27
    protected $reader;
28
29
    /**
30
     * The paths where to look for mapping files.
31
     *
32
     * @var array
33
     */
34
    protected $paths = [];
35
36
    /**
37
     * The paths excluded from path where to look for mapping files.
38
     *
39
     * @var array
40
     */
41
    protected $excludePaths = [];
42
43
    /**
44
     * The file extension of mapping documents.
45
     *
46
     * @var string
47
     */
48
    protected $fileExtension = '.php';
49
50
    /**
51
     * @var array|null
52
     */
53
    protected $classNames;
54
55
    /**
56
     * AbstractAnnotationDriver constructor.
57
     *
58
     * @param Reader $reader
59
     * @param array  $paths
60
     */
61
    public function __construct(Reader $reader, array $paths = array())
62
    {
63
        $this->reader = $reader;
64
        $this->addPaths($paths);
65
    }
66
67
    /**
68
     * @param array $paths
69
     */
70
    public function addPaths(array $paths)
71
    {
72
        $this->paths = array_unique(array_merge($this->paths, $paths));
73
    }
74
75
    /**
76
     * @param array $paths
77
     */
78
    public function addExcludePaths(array $paths)
79
    {
80
        $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function excludePaths()
87
    {
88
        return $this->excludePaths;
89
    }
90
91
    /**
92
     * @param string $fileExtension
93
     */
94
    public function setFileExtension($fileExtension)
95
    {
96
        $this->fileExtension = $fileExtension;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getAllClassNames()
103
    {
104
        if ($this->classNames !== null) {
105
            return $this->classNames;
106
        }
107
108
        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...
109
            throw MappingException::pathRequired();
110
        }
111
112
        $includedFiles = [];
113
        foreach ($this->paths as $path) {
114
            if (!is_dir($path)) {
115
                throw MappingException::invalidDirectory($path);
116
            }
117
118
            $iterator = new \RegexIterator(
119
                new \RecursiveIteratorIterator(
120
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
121
                    \RecursiveIteratorIterator::LEAVES_ONLY
122
                ),
123
                '/^.+'.preg_quote($this->fileExtension).'$/i',
124
                \RecursiveRegexIterator::GET_MATCH
125
            );
126
127
            foreach ($iterator as $file) {
128
                $sourceFile = $file[0];
129
130
                if (!preg_match('(^phar:)i', $sourceFile)) {
131
                    $sourceFile = realpath($sourceFile);
132
                }
133
134
                foreach ($this->excludePaths as $excludePath) {
135
                    $exclude = str_replace('\\', '/', realpath($excludePath));
136
                    $current = str_replace('\\', '/', $sourceFile);
137
138
                    if (strpos($current, $exclude) !== false) {
139
                        continue 2;
140
                    }
141
                }
142
143
                require_once $sourceFile;
144
                $includedFiles[] = $sourceFile;
145
            }
146
        }
147
148
        $this->classNames = [];
149
        foreach ($includedFiles as $includedFile) {
150
            $this->classNames = array_merge($this->classNames, ClassUtils::getClassesInFile($includedFile));
151
        }
152
153
        return $this->classNames;
154
    }
155
}
156