Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
created

FileDriver   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 161
rs 10
c 0
b 0
f 0
ccs 44
cts 50
cp 0.88

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllClassNames() 0 13 3
A initialize() 0 17 4
A getElement() 0 19 4
A setLocator() 0 3 1
A setGlobalBasename() 0 3 1
A isTransient() 0 11 3
A getLocator() 0 3 1
A __construct() 0 6 2
A getGlobalBasename() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Common\Persistence\Mapping\Driver;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Common\Persistence\Mapping\MappingException;
9
use function array_keys;
10
use function array_merge;
11
use function is_file;
12
use function str_replace;
13
14
/**
15
 * Base driver for file-based metadata drivers.
16
 *
17
 * A file driver operates in a mode where it loads the mapping files of individual
18
 * classes on demand. This requires the user to adhere to the convention of 1 mapping
19
 * file per class and the file names of the mapping files must correspond to the full
20
 * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
21
 */
22
abstract class FileDriver implements MappingDriver
23
{
24
    /** @var FileLocator */
25
    protected $locator;
26
27
    /** @var string[][]|null */
28
    protected $classCache;
29
30
    /** @var string|null */
31
    protected $globalBasename;
32
33
    /**
34
     * Initializes a new FileDriver that looks in the given path(s) for mapping
35
     * documents and operates in the specified operating mode.
36
     *
37
     * @param string|string[]|FileLocator $locator A FileLocator or one/multiple paths
38
     *                                          where mapping documents can be found.
39
     */
40 11
    public function __construct($locator, ?string $fileExtension = null)
41
    {
42 11
        if ($locator instanceof FileLocator) {
43 8
            $this->locator = $locator;
44
        } else {
45 3
            $this->locator = new DefaultFileLocator((array) $locator, $fileExtension);
46
        }
47 11
    }
48
49
    /**
50
     * Sets the global basename.
51
     */
52 5
    public function setGlobalBasename(string $file) : void
53
    {
54 5
        $this->globalBasename = $file;
55 5
    }
56
57
    /**
58
     * Retrieves the global basename.
59
     */
60 1
    public function getGlobalBasename() : ?string
61
    {
62 1
        return $this->globalBasename;
63
    }
64
65
    /**
66
     * Gets the element of schema meta data for the class from the mapping file.
67
     * This will lazily load the mapping file if it is not loaded yet.
68
     *
69
     * @return string[] The element of schema meta data.
70
     *
71
     * @throws MappingException
72
     */
73 3
    public function getElement(string $className) : array
74
    {
75 3
        if ($this->classCache === null) {
76 3
            $this->initialize();
77
        }
78
79 3
        if (isset($this->classCache[$className])) {
80 2
            return $this->classCache[$className];
81
        }
82
83 2
        $result = $this->loadMappingFile($this->locator->findMappingFile($className));
84
85 2
        if (! isset($result[$className])) {
86
            throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension());
87
        }
88
89 2
        $this->classCache[$className] = $result[$className];
90
91 2
        return $result[$className];
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 3
    public function isTransient(string $className) : bool
98
    {
99 3
        if ($this->classCache === null) {
100 3
            $this->initialize();
101
        }
102
103 3
        if (isset($this->classCache[$className])) {
104 1
            return false;
105
        }
106
107 3
        return ! $this->locator->fileExists($className);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 3
    public function getAllClassNames() : array
114
    {
115 3
        if ($this->classCache === null) {
116 3
            $this->initialize();
117
        }
118
119 3
        if (! $this->classCache) {
120 1
            return (array) $this->locator->getAllClassNames($this->globalBasename);
121
        }
122
123 2
        return array_merge(
124 2
            array_keys($this->classCache),
125 2
            (array) $this->locator->getAllClassNames($this->globalBasename)
126
        );
127
    }
128
129
    /**
130
     * Loads a mapping file with the given name and returns a map
131
     * from class/entity names to their corresponding file driver elements.
132
     *
133
     * @param string $file The mapping file to load.
134
     *
135
     * @return ClassMetadata[]
136
     */
137
    abstract protected function loadMappingFile(string $file) : array;
138
139
    /**
140
     * Initializes the class cache from all the global files.
141
     *
142
     * Using this feature adds a substantial performance hit to file drivers as
143
     * more metadata has to be loaded into memory than might actually be
144
     * necessary. This may not be relevant to scenarios where caching of
145
     * metadata is in place, however hits very hard in scenarios where no
146
     * caching is used.
147
     */
148 9
    protected function initialize() : void
149
    {
150 9
        $this->classCache = [];
151
152 9
        if ($this->globalBasename === null) {
153 5
            return;
154
        }
155
156 4
        foreach ($this->locator->getPaths() as $path) {
157 4
            $file = $path . '/' . $this->globalBasename . $this->locator->getFileExtension();
158 4
            if (! is_file($file)) {
159
                continue;
160
            }
161
162 4
            $this->classCache = array_merge(
163 4
                $this->classCache,
164 4
                $this->loadMappingFile($file)
165
            );
166
        }
167 4
    }
168
169
    /**
170
     * Retrieves the locator used to discover mapping files by className.
171
     */
172
    public function getLocator() : FileLocator
173
    {
174
        return $this->locator;
175
    }
176
177
    /**
178
     * Sets the locator used to discover mapping files by className.
179
     */
180
    public function setLocator(FileLocator $locator) : void
181
    {
182
        $this->locator = $locator;
183
    }
184
}
185