Completed
Pull Request — 1.3.x (#71)
by Grégoire
06:05
created

DefaultFileLocator::getAllClassNames()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

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