Skip to content

Commit

Permalink
Simplify services configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy Leherpeur committed Feb 23, 2024
1 parent d4a755f commit a85cd14
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 156 deletions.
94 changes: 0 additions & 94 deletions src/DependencyInjection/ZddMessageCompilerPass.php

This file was deleted.

75 changes: 44 additions & 31 deletions src/ZddMessageBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
namespace Yousign\ZddMessageBundle;

use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface as MessengerSerializerInterface;
use Yousign\ZddMessageBundle\Command\GenerateZddMessageCommand;
use Yousign\ZddMessageBundle\Command\ListZddMessageCommand;
use Yousign\ZddMessageBundle\Command\ValidateZddMessageCommand;
use Yousign\ZddMessageBundle\Config\ZddMessageConfigInterface;
use Yousign\ZddMessageBundle\DependencyInjection\ZddMessageCompilerPass;
use Yousign\ZddMessageBundle\Listener\Symfony\MessengerListener;
use Yousign\ZddMessageBundle\Serializer\SerializerInterface;
use Yousign\ZddMessageBundle\Serializer\ZddMessageMessengerSerializer;

final class ZddMessageBundle extends AbstractBundle
Expand All @@ -22,8 +25,9 @@ public function configure(DefinitionConfigurator $definition): void
$definition
->rootNode()
->children()
->scalarNode('message_config_service')->defaultNull()->end()
->scalarNode('serialized_messages_dir')->defaultNull()->end()
->scalarNode('serializer')->defaultValue('Yousign\ZddMessageBundle\Serializer\ZddMessageMessengerSerializer')->end()
->scalarNode('serializer')->defaultValue(ZddMessageMessengerSerializer::class)->end()
->arrayNode('log_untracked_messages')
->children()
->arrayNode('messenger')
Expand All @@ -40,28 +44,44 @@ public function configure(DefinitionConfigurator $definition): void
}

/** @phpstan-ignore-next-line */
public function loadExtension(array $config, ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$containerBuilder->setDefinition(
ZddMessageMessengerSerializer::class,
new Definition(
ZddMessageMessengerSerializer::class,
[
new Reference(MessengerSerializerInterface::class),
]
)
);
$serviceConfigurator = $container
->services()
->defaults()
->autowire()
->autoconfigure()
;

$containerBuilder->setAlias(
'yousign.zdd.message.serializer',
new Alias($config['serializer'] ?? ZddMessageMessengerSerializer::class)
);
$messageConfigServiceId = $config['message_config_service'];
if (!$messageConfigServiceId) {
throw new \LogicException(sprintf('You should configure zdd_message.message_config_service with a service that implements %s', ZddMessageConfigInterface::class));
}

$serviceConfigurator->bind('$zddMessageConfig', service($messageConfigServiceId));
$serviceConfigurator->bind('$zddMessagePath', $config['serialized_messages_dir'] ?? $this->getDefaultPath($builder));

$containerBuilder->registerForAutoconfiguration(ZddMessageConfigInterface::class)->addTag('yousign.zdd.message.config');
$messengerEnable = $config['log_untracked_messages']['messenger']['enable'] ?? false;
if ($messengerEnable) {
$messengerLevel = $config['log_untracked_messages']['messenger']['level'] ?? 'warning';
$serviceConfigurator
->set(MessengerListener::class)
->autowire()
->tag('kernel.event_subscriber')
->args([
service('logger'),
service($messageConfigServiceId),
$messengerLevel,
])
;
}

$containerBuilder->setParameter('yousign.zdd.message.serialized_messages_dir', $config['serialized_messages_dir'] ?? $this->getDefaultPath($containerBuilder));
$containerBuilder->setParameter('yousign.zdd.message.log_untracked_messages.messenger.enable', $config['log_untracked_messages']['messenger']['enable'] ?? false);
$containerBuilder->setParameter('yousign.zdd.message.log_untracked_messages.messenger.level', $config['log_untracked_messages']['messenger']['level'] ?? 'warning');
$serviceConfigurator
->set(SerializerInterface::class, $config['serializer'])
->set(GenerateZddMessageCommand::class)
->set(ValidateZddMessageCommand::class)
->set(ListZddMessageCommand::class)
;
}

private function getDefaultPath(ContainerBuilder $containerBuilder): string
Expand All @@ -74,11 +94,4 @@ private function getDefaultPath(ContainerBuilder $containerBuilder): string

return $projectDir.'/var/zdd-message';
}

public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new ZddMessageCompilerPass());
}
}
18 changes: 0 additions & 18 deletions tests/Fixtures/App/Messages/Config/NullConfig.php

This file was deleted.

27 changes: 14 additions & 13 deletions tests/Fixtures/App/packages/config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
framework:
secret: ThisIsSecret
test: true
messenger:
transports:
sync: "in-memory://"

parameters:
custom_path_file: '%kernel.project_dir%/tmp/serialized_messages_directory'

services:
_defaults:
autowire: true
Expand All @@ -15,10 +9,17 @@ services:

Yousign\ZddMessageBundle\Tests\Fixtures\App\Messages\Config\MessageConfig: ~

zdd_message:
serialized_messages_dir: '%custom_path_file%'
log_untracked_messages:
messenger:
enable: true
level: warning
framework:
secret: ThisIsSecret
test: true
messenger:
transports:
sync: "in-memory://"

zdd_message:
message_config_service: Yousign\ZddMessageBundle\Tests\Fixtures\App\Messages\Config\MessageConfig
serialized_messages_dir: '%custom_path_file%'
log_untracked_messages:
messenger:
enable: true
level: warning

0 comments on commit a85cd14

Please sign in to comment.