1 | <?php |
||
21 | * |
||
22 | * @link http://www.doctrine-project.org/ |
||
23 | */ |
||
24 | class SQLLoggerCollectorFactory implements FactoryInterface |
||
25 | { |
||
26 | protected string $name; |
||
|
|||
27 | |||
28 | public function __construct(string $name) |
||
29 | { |
||
30 | $this->name = $name; |
||
31 | 5 | } |
|
32 | |||
33 | 5 | /** |
|
34 | 5 | * {@inheritDoc} |
|
35 | */ |
||
36 | public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
||
37 | { |
||
38 | $options = $this->getOptions($container); |
||
39 | 5 | assert($options instanceof SQLLoggerCollectorOptions); |
|
40 | |||
41 | // @todo always ask the serviceLocator instead? (add a factory?) |
||
42 | 5 | if ($options->getSqlLogger()) { |
|
43 | $debugStackLogger = $container->get($options->getSqlLogger()); |
||
44 | } else { |
||
45 | 5 | $debugStackLogger = new DebugStack(); |
|
46 | 2 | } |
|
47 | |||
48 | 3 | $configuration = $container->get($options->getConfiguration()); |
|
49 | assert($configuration instanceof Configuration); |
||
50 | |||
51 | if ($configuration->getSQLLogger() !== null) { |
||
52 | 5 | $logger = new LoggerChain(); |
|
53 | $logger->addLogger($debugStackLogger); |
||
54 | 5 | $logger->addLogger($configuration->getSQLLogger()); |
|
55 | 1 | $configuration->setSQLLogger($logger); |
|
56 | 1 | } else { |
|
57 | 1 | $configuration->setSQLLogger($debugStackLogger); |
|
58 | 1 | } |
|
59 | |||
60 | 4 | return new SQLLoggerCollector($debugStackLogger, 'doctrine.sql_logger_collector.' . $options->getName()); |
|
61 | } |
||
62 | |||
63 | 5 | /** |
|
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | public function createService(ServiceLocatorInterface $container) |
||
67 | { |
||
68 | return $this($container, SQLLoggerCollector::class); |
||
69 | 5 | } |
|
70 | |||
71 | 5 | /** |
|
72 | * @return mixed |
||
73 | * |
||
74 | * @throws RuntimeException |
||
75 | */ |
||
76 | protected function getOptions(ContainerInterface $serviceLocator) |
||
77 | { |
||
78 | $options = $serviceLocator->get('config'); |
||
79 | 5 | $options = $options['doctrine']; |
|
80 | $options = $options['sql_logger_collector'][$this->name] ?? null; |
||
81 | 5 | ||
82 | 5 | if ($options === null) { |
|
83 | 5 | throw new RuntimeException( |
|
84 | sprintf( |
||
85 | 5 | 'Configuration with name "%s" could not be found in "doctrine.sql_logger_collector".', |
|
86 | $this->name |
||
87 | ) |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | $optionsClass = $this->getOptionsClass(); |
||
92 | |||
93 | return new $optionsClass($options); |
||
94 | 5 | } |
|
95 | |||
96 | 5 | /** |
|
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | protected function getOptionsClass() |
||
100 | { |
||
101 | return SQLLoggerCollectorOptions::class; |
||
102 | 5 | } |
|
103 | } |
||
104 |