DefaultFileLocator::getAllClassNames()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0796

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
ccs 15
cts 17
cp 0.8824
rs 8.8333
cc 7
nc 6
nop 1
crap 7.0796
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Persistence\Mapping\Driver;
6
7
use Doctrine\Persistence\Mapping\MappingException;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use const DIRECTORY_SEPARATOR;
11
use function array_merge;
12
use function array_unique;
13
use function assert;
14
use function is_dir;
15
use function is_file;
16
use function is_string;
17
use function str_replace;
18
19
/**
20
 * Locates the file that contains the metadata information for a given class name.
21
 *
22
 * This behavior is independent of the actual content of the file. It just detects
23
 * the file which is responsible for the given class name.
24
 */
25
class DefaultFileLocator implements FileLocator
26
{
27
    /**
28
     * The paths where to look for mapping files.
29
     *
30
     * @var array<int, string>
31
     */
32
    protected $paths = [];
33
34
    /**
35
     * The file extension of mapping documents.
36
     *
37
     * @var string|null
38
     */
39
    protected $fileExtension;
40
41
    /**
42
     * Initializes a new FileDriver that looks in the given path(s) for mapping
43
     * documents and operates in the specified operating mode.
44
     *
45
     * @param string|array<int, string> $paths         One or multiple paths where mapping documents
46
     *                                                 can be found.
47
     * @param string|null               $fileExtension The file extension of mapping documents,
48
     *                                                 usually prefixed with a dot.
49
     */
50 11
    public function __construct($paths, ?string $fileExtension = null)
51
    {
52 11
        $this->addPaths((array) $paths);
53 11
        $this->fileExtension = $fileExtension;
54 11
    }
55
56
    /**
57
     * Appends lookup paths to metadata driver.
58
     *
59
     * @param array<int, string> $paths
60
     *
61
     * @return void
62
     */
63 11
    public function addPaths(array $paths)
64
    {
65 11
        $this->paths = array_unique(array_merge($this->paths, $paths));
66 11
    }
67
68
    /**
69
     * Retrieves the defined metadata lookup paths.
70
     *
71
     * @return array<int, string>
72
     */
73 3
    public function getPaths()
74
    {
75 3
        return $this->paths;
76
    }
77
78
    /**
79
     * Gets the file extension used to look for mapping files under.
80
     *
81
     * @return string|null
82
     */
83 2
    public function getFileExtension()
84
    {
85 2
        return $this->fileExtension;
86
    }
87
88
    /**
89
     * Sets the file extension used to look for mapping files under.
90
     *
91
     * @param string|null $fileExtension The file extension to set.
92
     *
93
     * @return void
94
     */
95 1
    public function setFileExtension(?string $fileExtension)
96
    {
97 1
        $this->fileExtension = $fileExtension;
98 1
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103 3
    public function findMappingFile(string $className)
104
    {
105 3
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
106
107
        // Check whether file exists
108 3
        foreach ($this->paths as $path) {
109 3
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
110 2
                return $path . DIRECTORY_SEPARATOR . $fileName;
111
            }
112
        }
113
114 1
        throw MappingException::mappingFileNotFound($className, $fileName);
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 2
    public function getAllClassNames(string $globalBasename)
121
    {
122 2
        if ($this->paths === []) {
123
            return [];
124
        }
125
126 2
        $classes = [];
127
128 2
        foreach ($this->paths as $path) {
129 2
            if (! is_dir($path)) {
130
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
131
            }
132
133 2
            $iterator = new RecursiveIteratorIterator(
134 2
                new RecursiveDirectoryIterator($path),
135 2
                RecursiveIteratorIterator::LEAVES_ONLY
136
            );
137
138 2
            foreach ($iterator as $file) {
139 2
                $fileName = $file->getBasename($this->fileExtension);
140
141 2
                if ($fileName === $file->getBasename() || $fileName === $globalBasename) {
142 2
                    continue;
143
                }
144
145
                // NOTE: All files found here means classes are not transient!
146
147 1
                assert(is_string($fileName));
148 1
                $classes[] = str_replace('.', '\\', $fileName);
149
            }
150
        }
151
152 2
        return $classes;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 2
    public function fileExists(string $className)
159
    {
160 2
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
161
162
        // Check whether file exists
163 2
        foreach ($this->paths as $path) {
164 2
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
165 2
                return true;
166
            }
167
        }
168
169 2
        return false;
170
    }
171
}
172