Completed
Pull Request — master (#50)
by Jonathan
04:15 queued 01:55
created

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