Skip to content

Commit

Permalink
Fixed configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kreemer committed Aug 9, 2023
1 parent 0ee9d9d commit a16b49f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 72 deletions.
2 changes: 1 addition & 1 deletion config/doctrine_health.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
Akondas\ActuatorBundle\Service\Health\Indicator\Doctrine:
SymSensor\ActuatorDoctrineBundle\Service\Health\Indicator\Doctrine:
tags: ['sym_sensor_actuator.health_indicator']
arguments:
- [ ]
4 changes: 3 additions & 1 deletion config/doctrine_info.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
services:
SymSensor\ActuatorBundle\Service\Info\Collector\Doctrine:
SymSensor\ActuatorDoctrineBundle\Service\Info\Collector\Doctrine:
tags: ['sym_sensor_actuator.info_collector']
arguments:
- [ ]
39 changes: 7 additions & 32 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,15 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();

$rootNode // @phpstan-ignore-line
->canBeDisabled()
->children()
->arrayNode('health')
->canBeDisabled()
->children()
->arrayNode('database')
->addDefaultsIfNotSet()
->canBeDisabled()
->arrayNode('connections')
->useAttributeAsKey('name')
->defaultValue(['default' => ['service' => 'doctrine.dbal.default_connection', 'check_sql' => 'SELECT 1']])
->arrayPrototype()
->children()
->arrayNode('connections')
->useAttributeAsKey('name')
->defaultValue(['default' => ['service' => 'doctrine.dbal.default_connection', 'check_sql' => 'SELECT 1']])
->arrayPrototype()
->children()
->scalarNode('service')->isRequired()->end()
->scalarNode('check_sql')->defaultValue('SELECT 1')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->arrayNode('info')
->addDefaultsIfNotSet()
->canBeDisabled()
->children()
->arrayNode('database')
->canBeDisabled()
->children()
->arrayNode('connections')
->useAttributeAsKey('name')
->defaultValue(['default' => 'doctrine.dbal.default_connection'])
->scalarPrototype()->end()
->end()
->end()
->scalarNode('service')->isRequired()->end()
->scalarNode('check_sql')->defaultValue('SELECT 1')->end()
->end()
->end()
->end()
Expand Down
47 changes: 12 additions & 35 deletions src/DependencyInjection/SymSensorActuatorDoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use SymSensor\ActuatorDoctrineBundle\Service\Health\Indicator as HealthIndicator;
use SymSensor\ActuatorDoctrineBundle\Service\Info\Collector as InfoCollector;

final class SymSensorActuatorDoctrineExtension extends Extension
{
Expand All @@ -29,42 +27,30 @@ final class SymSensorActuatorDoctrineExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yaml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$this->processHealthConfiguration($config['health'], $container);
$this->processInfoConfiguration($config['info'], $container);
$this->processHealthConfiguration($config, $container);
$this->processInfoConfiguration($config, $container);
}

/**
* @param mixed[] $config
*/
private function processHealthConfiguration(array $config, ContainerBuilder $container): void
{
$enabled = true;
if (!$this->isConfigEnabled($container, $config)) {
$enabled = false;
}
$container->setParameter('sym_sensor_actuator_doctrine.health.enabled', $enabled);

if (
$container->willBeAvailable('doctrine/doctrine-bundle', Connection::class, [])
&& isset($config['database'])
&& \is_array($config['database'])
&& $this->isConfigEnabled($container, $config['database'])
&& $this->isConfigEnabled($container, $config)
) {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('doctrine_health.yaml');

$databaseConfig = $config['builtin']['database'];
$definition = $container->getDefinition(HealthIndicator\Doctrine::class);
$definition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Health\Indicator\Doctrine::class);

if (\is_array($databaseConfig['connections'])) {
if (\is_array($config['connections'])) {
$constructorArgument = [];
foreach ($databaseConfig['connections'] as $name => $connection) {
foreach ($config['connections'] as $name => $connection) {
if (!\is_array($connection)) {
continue;
}
Expand All @@ -85,28 +71,19 @@ private function processHealthConfiguration(array $config, ContainerBuilder $con
*/
private function processInfoConfiguration(array $config, ContainerBuilder $container): void
{
$enabled = true;
if (!$this->isConfigEnabled($container, $config)) {
$enabled = false;
}
$container->setParameter('sym_sensor_actuator_doctrine.info.enabled', $enabled);

if (
$container->willBeAvailable('doctrine/doctrine-bundle', Connection::class, [])
&& isset($config['database'])
&& \is_array($config['database'])
&& $this->isConfigEnabled($container, $config['builtin']['database'])
&& $this->isConfigEnabled($container, $config)
) {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config/extensions'));
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('doctrine_info.yaml');

$databaseConfig = $config['database'];
if (isset($databaseConfig['connections']) && \is_array($databaseConfig['connections'])) {
if (isset($config['connections']) && \is_array($config['connections'])) {
$connectionReferences = [];
foreach ($databaseConfig['connections'] as $name => $connectionDefinition) {
$connectionReferences[$name] = new Reference($connectionDefinition);
foreach ($config['connections'] as $name => $connectionDefinition) {
$connectionReferences[$name] = new Reference($connectionDefinition['service']);
}
$definition = $container->getDefinition(InfoCollector\Doctrine::class);
$definition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Info\Collector\Doctrine::class);
$definition->replaceArgument(0, $connectionReferences);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Info/Collector/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function collect(): Info
}

$connectionInfo[$name] = [
'type' => $type,
'database' => $database,
'driver' => \get_class($connection->getDriver()),
'type' => $type,
'database' => $database,
'driver' => \get_class($connection->getDriver()),
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/SymSensorActuatorDoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace SymSensor\ActuatorDoctrineBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use SymSensor\ActuatorDoctrineBundle\DependencyInjection\SymSensorActuatorDoctrineExtension;

Expand Down

0 comments on commit a16b49f

Please sign in to comment.