Completed
Pull Request — master (#50)
by Jonathan
04:13 queued 02:03
created

DefaultFileLocator::getFileExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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
        if (is_string($paths)) {
53 1
            $paths = [$paths];
54
        }
55
56 11
        $this->addPaths($paths);
57 11
        $this->fileExtension = $fileExtension;
58 11
    }
59
60
    /**
61
     * Appends lookup paths to metadata driver.
62
     *
63
     * @param array<int, string> $paths
64
     */
65 11
    public function addPaths(array $paths) : void
66
    {
67 11
        $this->paths = array_unique(array_merge($this->paths, $paths));
68 11
    }
69
70
    /**
71
     * Retrieves the defined metadata lookup paths.
72
     *
73
     * @return array<int, string>
74
     */
75 3
    public function getPaths() : array
76
    {
77 3
        return $this->paths;
78
    }
79
80
    /**
81
     * Gets the file extension used to look for mapping files under.
82
     */
83 2
    public function getFileExtension() : ?string
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 1
    public function setFileExtension(?string $fileExtension) : void
94
    {
95 1
        $this->fileExtension = $fileExtension;
96 1
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 3
    public function findMappingFile(string $className) : string
102
    {
103 3
        $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
104
105
        // Check whether file exists
106 3
        foreach ($this->paths as $path) {
107 3
            if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
108 2
                return $path . DIRECTORY_SEPARATOR . $fileName;
109
            }
110
        }
111
112 1
        throw MappingException::mappingFileNotFound($className, $fileName);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 2
    public function getAllClassNames(string $globalBasename) : array
119
    {
120 2
        if ($this->paths === []) {
121
            return [];
122
        }
123
124 2
        $classes = [];
125
126 2
        foreach ($this->paths as $path) {
127 2
            if (! is_dir($path)) {
128
                throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
129
            }
130
131 2
            $iterator = new RecursiveIteratorIterator(
132 2
                new RecursiveDirectoryIterator($path),
133 2
                RecursiveIteratorIterator::LEAVES_ONLY
134
            );
135
136 2
            foreach ($iterator as $file) {
137 2
                $fileName = $file->getBasename($this->fileExtension);
138
139 2
                if ($fileName === $file->getBasename() || $fileName === $globalBasename) {
140 2
                    continue;
141
                }
142
143
                // NOTE: All files found here means classes are not transient!
144
145 1
                $class = str_replace('.', '\\', $fileName);
146 1
                assert(is_string($class));
147
148 1
                $classes[] = $class;
149
            }
150
        }
151
152 2
        return $classes;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 2
    public function fileExists(string $className) : bool
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