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

DefaultFileLocator::findMappingFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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