AbstractClassMetadataFactory::getCacheDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Persistence\Mapping;
6
7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
9
use Doctrine\Persistence\Proxy;
10
use ReflectionException;
11
use function array_reverse;
12
use function array_unshift;
13
use function explode;
14
use function strpos;
15
use function strrpos;
16
use function substr;
17
18
/**
19
 * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
20
 * metadata mapping informations of a class which describes how a class should be mapped
21
 * to a relational database.
22
 *
23
 * This class was abstracted from the ORM ClassMetadataFactory.
24
 */
25
abstract class AbstractClassMetadataFactory implements ClassMetadataFactory
26
{
27
    /**
28
     * Salt used by specific Object Manager implementation.
29
     *
30
     * @var string
31
     */
32
    protected $cacheSalt = '$CLASSMETADATA';
33
34
    /** @var Cache|null */
35
    private $cacheDriver;
36
37
    /** @var array<string, ClassMetadata> */
38
    private $loadedMetadata = [];
39
40
    /** @var bool */
41
    protected $initialized = false;
42
43
    /** @var ReflectionService|null */
44
    private $reflectionService = null;
45
46
    /**
47
     * Sets the cache driver used by the factory to cache ClassMetadata instances.
48
     *
49
     * @return void
50
     */
51 4
    public function setCacheDriver(?Cache $cacheDriver = null)
52
    {
53 4
        $this->cacheDriver = $cacheDriver;
54 4
    }
55
56
    /**
57
     * Gets the cache driver used by the factory to cache ClassMetadata instances.
58
     *
59
     * @return Cache|null
60
     */
61 1
    public function getCacheDriver()
62
    {
63 1
        return $this->cacheDriver;
64
    }
65
66
    /**
67
     * Returns an array of all the loaded metadata currently in memory.
68
     *
69
     * @return ClassMetadata[]
70
     */
71
    public function getLoadedMetadata()
72
    {
73
        return $this->loadedMetadata;
74
    }
75
76
    /**
77
     * Forces the factory to load the metadata of all classes known to the underlying
78
     * mapping driver.
79
     *
80
     * @return array<int, ClassMetadata> The ClassMetadata instances of all mapped classes.
81
     */
82
    public function getAllMetadata()
83
    {
84
        if (! $this->initialized) {
85
            $this->initialize();
86
        }
87
88
        $driver   = $this->getDriver();
89
        $metadata = [];
90
        foreach ($driver->getAllClassNames() as $className) {
91
            $metadata[] = $this->getMetadataFor($className);
92
        }
93
94
        return $metadata;
95
    }
96
97
    /**
98
     * Lazy initialization of this stuff, especially the metadata driver,
99
     * since these are not needed at all when a metadata cache is active.
100
     *
101
     * @return void
102
     */
103
    abstract protected function initialize();
104
105
    /**
106
     * Gets the fully qualified class-name from the namespace alias.
107
     *
108
     * @return string
109
     */
110
    abstract protected function getFqcnFromAlias(
111
        string $namespaceAlias,
112
        string $simpleClassName
113
    );
114
115
    /**
116
     * Returns the mapping driver implementation.
117
     *
118
     * @return MappingDriver
119
     */
120
    abstract protected function getDriver();
121
122
    /**
123
     * Wakes up reflection after ClassMetadata gets unserialized from cache.
124
     *
125
     * @return void
126
     */
127
    abstract protected function wakeupReflection(
128
        ClassMetadata $class,
129
        ReflectionService $reflService
130
    );
131
132
    /**
133
     * Initializes Reflection after ClassMetadata was constructed.
134
     *
135
     * @return void
136
     */
137
    abstract protected function initializeReflection(
138
        ClassMetadata $class,
139
        ReflectionService $reflService
140
    );
141
142
    /**
143
     * Checks whether the class metadata is an entity.
144
     *
145
     * This method should return false for mapped superclasses or embedded classes.
146
     *
147
     * @return bool
148
     */
149
    abstract protected function isEntity(ClassMetadata $class);
150
151
    /**
152
     * Gets the class metadata descriptor for a class.
153
     *
154
     * @param string $className The name of the class.
155
     *
156
     * @return ClassMetadata
157
     *
158
     * @throws ReflectionException
159
     * @throws MappingException
160
     */
161 10
    public function getMetadataFor(string $className)
162
    {
163 10
        if (isset($this->loadedMetadata[$className])) {
164
            return $this->loadedMetadata[$className];
165
        }
166
167
        // Check for namespace alias
168 10
        if (strpos($className, ':') !== false) {
169 2
            [$namespaceAlias, $simpleClassName] = explode(':', $className, 2);
170
171 2
            $realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
172
        } else {
173 8
            $realClassName = $this->getRealClass($className);
174
        }
175
176 10
        if (isset($this->loadedMetadata[$realClassName])) {
177
            // We do not have the alias name in the map, include it
178
            return $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
179
        }
180
181 10
        $loadingException = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $loadingException is dead and can be removed.
Loading history...
182
183
        try {
184 10
            if ($this->cacheDriver !== null) {
185 3
                $cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt);
186
187 3
                if ($cached instanceof ClassMetadata) {
188 1
                    $this->loadedMetadata[$realClassName] = $cached;
189
190 1
                    $this->wakeupReflection($cached, $this->getReflectionService());
191
                } else {
192 3
                    foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
193 1
                        $this->cacheDriver->save(
194 1
                            $loadedClassName . $this->cacheSalt,
195 1
                            $this->loadedMetadata[$loadedClassName]
196
                        );
197
                    }
198
                }
199
            } else {
200 9
                $this->loadMetadata($realClassName);
201
            }
202 5
        } catch (MappingException $loadingException) {
203 5
            $fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $fallbackMetadataResponse is correct as $this->onNotFoundMetadata($realClassName) targeting Doctrine\Persistence\Map...y::onNotFoundMetadata() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
204
205 5
            if ($fallbackMetadataResponse === null) {
0 ignored issues
show
introduced by
The condition $fallbackMetadataResponse === null is always true.
Loading history...
206 3
                throw $loadingException;
207
            }
208
209 2
            $this->loadedMetadata[$realClassName] = $fallbackMetadataResponse;
210
        }
211
212 7
        if ($className !== $realClassName) {
213
            // We do not have the alias name in the map, include it
214 1
            $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName];
215
        }
216
217 7
        return $this->loadedMetadata[$className];
218
    }
219
220
    /**
221
     * Checks whether the factory has the metadata for a class loaded already.
222
     *
223
     * @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise.
224
     */
225 3
    public function hasMetadataFor(string $className)
226
    {
227 3
        return isset($this->loadedMetadata[$className]);
228
    }
229
230
    /**
231
     * Sets the metadata descriptor for a specific class.
232
     *
233
     * NOTE: This is only useful in very special cases, like when generating proxy classes.
234
     *
235
     * @return void
236
     */
237
    public function setMetadataFor(string $className, ClassMetadata $class)
238
    {
239
        $this->loadedMetadata[$className] = $class;
240
    }
241
242
    /**
243
     * Gets an array of parent classes for the given entity class.
244
     *
245
     * @return array<int, string>
246
     */
247 9
    protected function getParentClasses(string $name)
248
    {
249
        // Collect parent classes, ignoring transient (not-mapped) classes.
250 9
        $parentClasses = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $parentClasses is dead and can be removed.
Loading history...
251
252 9
        $parentClasses = $this->getReflectionService()
253 9
            ->getParentClasses($name);
254
255 4
        foreach (array_reverse($parentClasses) as $parentClass) {
256 3
            if ($this->getDriver()->isTransient($parentClass)) {
257
                continue;
258
            }
259
260 3
            $parentClasses[] = $parentClass;
261
        }
262
263 4
        return $parentClasses;
264
    }
265
266
    /**
267
     * Loads the metadata of the class in question and all it's ancestors whose metadata
268
     * is still not loaded.
269
     *
270
     * Important: The class $name does not necessarily exist at this point here.
271
     * Scenarios in a code-generation setup might have access to XML/YAML
272
     * Mapping files without the actual PHP code existing here. That is why the
273
     * {@see Doctrine\Persistence\Mapping\ReflectionService} interface
274
     * should be used for reflection.
275
     *
276
     * @param string $name The name of the class for which the metadata should get loaded.
277
     *
278
     * @return array<int, string>
279
     */
280 9
    protected function loadMetadata(string $name)
281
    {
282 9
        if (! $this->initialized) {
283 9
            $this->initialize();
284
        }
285
286 9
        $loaded = [];
287
288 9
        $parentClasses   = $this->getParentClasses($name);
289 4
        $parentClasses[] = $name;
290
291
        // Move down the hierarchy of parent classes, starting from the topmost class
292 4
        $parent          = null;
293 4
        $rootEntityFound = false;
294 4
        $visited         = [];
295 4
        $reflService     = $this->getReflectionService();
296
297 4
        foreach ($parentClasses as $className) {
298 4
            if (isset($this->loadedMetadata[$className])) {
299 3
                $parent = $this->loadedMetadata[$className];
300
301 3
                if ($this->isEntity($parent)) {
302 3
                    $rootEntityFound = true;
303
304 3
                    array_unshift($visited, $className);
305
                }
306
307 3
                continue;
308
            }
309
310 4
            $class = $this->newClassMetadataInstance($className);
311 4
            $this->initializeReflection($class, $reflService);
312
313 4
            $this->doLoadMetadata($class, $parent, $rootEntityFound, $visited);
314
315 4
            $this->loadedMetadata[$className] = $class;
316
317 4
            $parent = $class;
318
319 4
            if ($this->isEntity($class)) {
320 4
                $rootEntityFound = true;
321
322 4
                array_unshift($visited, $className);
323
            }
324
325 4
            $this->wakeupReflection($class, $reflService);
326
327 4
            $loaded[] = $className;
328
        }
329
330 4
        return $loaded;
331
    }
332
333
    /**
334
     * Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions
335
     *
336
     * Override this method to implement a fallback strategy for failed metadata loading
337
     *
338
     * @return ClassMetadata|null
339
     */
340
    protected function onNotFoundMetadata(string $className)
341
    {
342
        return null;
343
    }
344
345
    /**
346
     * Actually loads the metadata from the underlying metadata.
347
     *
348
     * @param array<int, string> $nonSuperclassParents All parent class names that are not marked as mapped superclasses.
349
     *
350
     * @return void
351
     */
352
    abstract protected function doLoadMetadata(
353
        ClassMetadata $class,
354
        ?ClassMetadata $parent,
355
        bool $rootEntityFound,
356
        array $nonSuperclassParents
357
    );
358
359
    /**
360
     * Creates a new ClassMetadata instance for the given class name.
361
     *
362
     * @return ClassMetadata
363
     */
364
    abstract protected function newClassMetadataInstance(string $className);
365
366
    /**
367
     * {@inheritDoc}
368
     */
369
    public function isTransient(string $class)
370
    {
371
        if (! $this->initialized) {
372
            $this->initialize();
373
        }
374
375
        // Check for namespace alias
376
        if (strpos($class, ':') !== false) {
377
            [$namespaceAlias, $simpleClassName] = explode(':', $class, 2);
378
379
            $class = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName);
380
        }
381
382
        return $this->getDriver()->isTransient($class);
383
    }
384
385
    /**
386
     * Sets the reflectionService.
387
     *
388
     * @return void
389
     */
390
    public function setReflectionService(ReflectionService $reflectionService)
391
    {
392
        $this->reflectionService = $reflectionService;
393
    }
394
395
    /**
396
     * Gets the reflection service associated with this metadata factory.
397
     *
398
     * @return ReflectionService
399
     */
400 10
    public function getReflectionService()
401
    {
402 10
        if ($this->reflectionService === null) {
403 10
            $this->reflectionService = new RuntimeReflectionService();
404
        }
405
406 10
        return $this->reflectionService;
407
    }
408
409
    /**
410
     * Gets the real class name of a class name that could be a proxy.
411
     */
412 8
    private function getRealClass(string $class) : string
413
    {
414 8
        $pos = strrpos($class, '\\' . Proxy::MARKER . '\\');
415
416 8
        if ($pos === false) {
417 8
            return $class;
418
        }
419
420
        return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
421
    }
422
}
423