-
Notifications
You must be signed in to change notification settings - Fork 34
/
DriverFactory.php
153 lines (129 loc) · 4.61 KB
/
DriverFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Roave\PsrContainerDoctrine;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Roave\PsrContainerDoctrine\Exception\InvalidArgumentException;
use Roave\PsrContainerDoctrine\Exception\OutOfBoundsException;
use function array_key_exists;
use function is_array;
use function is_subclass_of;
/**
* @method MappingDriver __invoke(ContainerInterface $container)
*/
final class DriverFactory extends AbstractFactory
{
private static bool $isAnnotationLoaderRegistered = false;
/**
* {@inheritdoc}
*/
protected function createWithConfig(ContainerInterface $container, string $configKey)
{
$config = $this->retrieveConfig($container, $configKey, 'driver');
if (! array_key_exists('class', $config)) {
throw OutOfBoundsException::forMissingConfigKey('class');
}
if (! is_array($config['paths'])) {
$config['paths'] = [$config['paths']];
}
if (
$config['class'] !== AttributeDriver::class
&& ! is_subclass_of($config['class'], AttributeDriver::class)
&& is_subclass_of($config['class'], AnnotationDriver::class)
) {
$this->registerAnnotationLoader();
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress UnsafeInstantiation
*/
$driver = new $config['class'](
$this->createCachedReader($container, $config, new AnnotationReader()),
$config['paths']
);
} elseif ($config['extension'] !== null && is_subclass_of($config['class'], FileDriver::class)) {
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress UnsafeInstantiation
*/
$driver = new $config['class']($config['paths'], $config['extension']);
}
if (! isset($driver)) {
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress UnsafeInstantiation
*/
$driver = new $config['class']($config['paths']);
}
if (array_key_exists('global_basename', $config) && $driver instanceof FileDriver) {
$driver->setGlobalBasename($config['global_basename']);
}
if ($driver instanceof MappingDriverChain) {
if ($config['default_driver'] !== null) {
$driver->setDefaultDriver($this->createWithConfig($container, $config['default_driver']));
}
foreach ($config['drivers'] as $namespace => $driverName) {
if ($driverName === null) {
continue;
}
$driver->addDriver($this->createWithConfig($container, $driverName), $namespace);
}
}
return $driver;
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfig(string $configKey): array
{
return [
'paths' => [],
'extension' => null,
'drivers' => [],
'default_driver' => null,
];
}
/**
* Registers the annotation loader
*/
private function registerAnnotationLoader(): void
{
if (self::$isAnnotationLoaderRegistered) {
return;
}
/** @psalm-suppress DeprecatedMethod */
AnnotationRegistry::registerLoader('class_exists');
self::$isAnnotationLoaderRegistered = true;
}
/**
* @param array<string, mixed> $config
*/
private function createCachedReader(ContainerInterface $container, array $config, Reader $reader): PsrCachedReader
{
$cache = $this->retrieveDependency(
$container,
$config['cache'],
'cache',
CacheFactory::class
);
if ($cache instanceof Cache) {
$cache = CacheAdapter::wrap($cache);
}
if ($cache instanceof CacheItemPoolInterface) {
return new PsrCachedReader($reader, $cache);
}
throw InvalidArgumentException::fromUnsupportedCache(
$cache
);
}
}