diff --git a/DependencyInjection/MonologConfigExtension.php b/DependencyInjection/MonologConfigExtension.php index 42576bf..b4f8fab 100644 --- a/DependencyInjection/MonologConfigExtension.php +++ b/DependencyInjection/MonologConfigExtension.php @@ -40,8 +40,11 @@ public function prepend(ContainerBuilder $container) $this->monologConfigs = $this->mergeConfiguration($this->monologConfigs, new FileImporter($config['sources']['files'])); } - $this->addConfig('config', $container); - $this->addConfig($container->getParameter("kernel.environment"), $container); + foreach ($this->monologConfigs as $definition => $arrayValues) { + $this->addDefinitionConfigs($definition, $container); + } + + $this->addParameters($container->getParameter("kernel.environment"), $container); } /** @@ -55,23 +58,60 @@ private function mergeConfiguration($monologConfigs, ImporterInterface $importer } /** - * @param $env + * @param $definition + * @param ContainerBuilder $container + */ + private function addDefinitionConfigs($definition, ContainerBuilder $container) + { + if ($container->hasExtension($definition) || $definition === 'app') { + foreach ($this->monologConfigs[$definition] as $environment => $environmentConfig) { + if (in_array($environment, ['config', $container->getParameter("kernel.environment")])) { + $this->addHandlers($environmentConfig, $container); + } + } + } + } + + /** + * @param $environmentConfig * @param ContainerBuilder $container */ - private function addConfig($env, ContainerBuilder $container) + private function addHandlers($environmentConfig, ContainerBuilder $container) { - if (isset($this->monologConfigs['monolog']) && isset($this->monologConfigs['monolog'][$env])) { - foreach ($this->monologConfigs['monolog'][$env]['handlers'] as $name => $values) { + if (isset($environmentConfig['handlers'])) { + foreach ($environmentConfig['handlers'] as $name => $values) { $container->prependExtensionConfig('monolog', [ 'handlers' => [$name => $values] ]); } } + } - if (isset($this->monologConfigs['parameters']) && isset($this->monologConfigs['parameters'][$env])) { - foreach ($this->monologConfigs['parameters'][$env] as $name => $value) { - $container->setParameter($name, $value); + /** + * @param $currentEnvironment + * @param ContainerBuilder $container + */ + private function addParameters($currentEnvironment, ContainerBuilder $container) + { + if (!$parameters = $this->getConfigParameters()) { + return; + } + + foreach ($parameters as $environment => $arrayParameters) { + if (in_array($environment, ['config', $currentEnvironment])) { + foreach ($arrayParameters as $name => $value) { + $container->setParameter($name, $value); + } } } + + } + + /** + * @return mixed|null + */ + private function getConfigParameters() + { + return isset($this->monologConfigs['parameters']) ? $this->monologConfigs['parameters'] : null ; } } \ No newline at end of file diff --git a/Resources/config/monolog_config.yml b/Resources/config/monolog_config.yml index 7e094b7..c84a01e 100644 --- a/Resources/config/monolog_config.yml +++ b/Resources/config/monolog_config.yml @@ -1,4 +1,4 @@ -monolog: +app: config: handlers: mail: