1 | <?php |
||
20 | * @link http://www.doctrine-project.org/ |
||
21 | */ |
||
22 | class DBALConfigurationFactory implements FactoryInterface |
||
23 | { |
||
24 | protected string $name; |
||
|
|||
25 | |||
26 | public function __construct(string $name) |
||
27 | { |
||
28 | $this->name = $name; |
||
29 | } |
||
30 | 88 | ||
31 | /** |
||
32 | 88 | * {@inheritDoc} |
|
33 | 88 | * |
|
34 | * @return Configuration |
||
35 | */ |
||
36 | public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
||
37 | { |
||
38 | $config = new Configuration(); |
||
39 | $this->setupDBALConfiguration($container, $config); |
||
40 | |||
41 | return $config; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | * |
||
47 | * @return Configuration |
||
48 | */ |
||
49 | public function createService(ServiceLocatorInterface $container) |
||
50 | { |
||
51 | return $this($container, Configuration::class); |
||
52 | } |
||
53 | |||
54 | public function setupDBALConfiguration(ContainerInterface $container, Configuration $config) : void |
||
55 | { |
||
56 | $options = $this->getOptions($container); |
||
57 | $config->setResultCacheImpl($container->get($options->resultCache)); |
||
58 | |||
59 | $sqlLogger = $options->sqlLogger; |
||
60 | if (is_string($sqlLogger) && $container->has($sqlLogger)) { |
||
61 | 86 | $sqlLogger = $container->get($sqlLogger); |
|
62 | } |
||
63 | 86 | ||
64 | 86 | $config->setSQLLogger($sqlLogger); |
|
65 | |||
66 | 86 | foreach ($options->types as $name => $class) { |
|
67 | 86 | if (Type::hasType($name)) { |
|
68 | Type::overrideType($name, $class); |
||
69 | } else { |
||
70 | 86 | Type::addType($name, $class); |
|
71 | } |
||
72 | 86 | } |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return mixed |
||
77 | * |
||
78 | * @throws RuntimeException |
||
79 | 86 | */ |
|
80 | public function getOptions(ContainerInterface $serviceLocator) |
||
81 | { |
||
82 | $options = $serviceLocator->get('config'); |
||
83 | $options = $options['doctrine']; |
||
84 | $options = $options['configuration'][$this->name] ?? null; |
||
85 | |||
86 | 88 | if ($options === null) { |
|
87 | throw new RuntimeException( |
||
88 | 88 | sprintf( |
|
89 | 88 | 'Configuration with name "%s" could not be found in "doctrine.configuration".', |
|
90 | 88 | $this->name |
|
91 | ) |
||
92 | 88 | ); |
|
93 | } |
||
94 | |||
95 | $optionsClass = $this->getOptionsClass(); |
||
96 | |||
97 | return new $optionsClass($options); |
||
98 | } |
||
99 | |||
100 | protected function getOptionsClass() : string |
||
101 | 88 | { |
|
102 | return DoctrineORMModuleConfiguration::class; |
||
103 | 88 | } |
|
104 | } |
||
105 |