Failed Conditions
Pull Request — master (#1)
by Jonathan
10:52
created

DefaultFileLocator::getAllClassNames()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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