Skip to content

Commit

Permalink
Merge eccf34a into 3f1e16b
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszhanc committed Jan 1, 2018
2 parents 3f1e16b + eccf34a commit 3cd7a8a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -307,6 +307,10 @@ If the command fails, it will throw a [League\Tactician\Bundle\Middleware\Invali

This middleware is bundled in Tactician, please refer to [the official documentation](http://tactician.thephpleague.com/plugins/locking-middleware/) for details.

### Logger Middleware (tactician.middleware.logger)

This middleware is bundled in Tactician, please refer to [the official documentation](http://tactician.thephpleague.com/plugins/logger/) for details.

### Security Middleware (tactician.middleware.security)

The security middleware will perform authorization on handling all commands. By default an AccessDenied exception will be thrown if the user is not authorized.
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -42,7 +42,8 @@
"symfony/console": "For debugging command-to-handler routing using the tactician:debug console command",
"symfony/validator": "For command validator middleware",
"symfony/security": "For command security middleware",
"league/tactician-doctrine": "For doctrine transaction middleware"
"league/tactician-doctrine": "For doctrine transaction middleware",
"league/tactician-logger": "For logger middleware"
},
"autoload" : {
"psr-4" : {
Expand All @@ -66,6 +67,7 @@
"symfony/security": "^2.8|^3.3|^4.0",
"symfony/validator": "^2.8|^3.3|^4.0",
"league/tactician-doctrine": "^1.1",
"league/tactician-logger": "^0.10.0",
"symfony/framework-bundle": "^2.8.15|^3.3|^4.0",
"symfony/security-bundle": "^2.8|^3.3|^4.0",
"matthiasnoback/symfony-config-test": "^3.0",
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -68,6 +68,10 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()
->end()
->scalarNode('logger_formatter')
->defaultValue('tactician.logger.class_properties_formatter')
->cannotBeEmpty()
->end()
->end()
->validate()
->ifTrue(function ($config) {
Expand Down
42 changes: 42 additions & 0 deletions src/DependencyInjection/TacticianExtension.php
Expand Up @@ -3,6 +3,9 @@
namespace League\Tactician\Bundle\DependencyInjection;

use League\Tactician\Bundle\Security\Voter\HandleCommandVoter;
use League\Tactician\Logger\Formatter\ClassNameFormatter;
use League\Tactician\Logger\Formatter\ClassPropertiesFormatter;
use League\Tactician\Logger\LoggerMiddleware;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -24,6 +27,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$loader->load('services.yml');
$container->setParameter('tactician.merged_config', $mergedConfig);
$this->configureSecurity($mergedConfig, $container);
$this->configureLogger($mergedConfig, $container);
}

public function getAlias()
Expand Down Expand Up @@ -66,4 +70,42 @@ private function configureCommandSecurityVoter(array $mergedConfig, ContainerBui
$container->setDefinition('tactician.middleware.security_voter', $definition);
}
}

/**
* Configure the logger middleware.
*
* @param array $mergedConfig
* @param ContainerBuilder $container
*/
private function configureLogger(array $mergedConfig, ContainerBuilder $container)
{
if (!class_exists(LoggerMiddleware::class)) {
return;
}

$this->configureLoggerFormatters($container);

$loggerMiddleware = new Definition(LoggerMiddleware::class, [
new Reference($mergedConfig['logger_formatter']),
new Reference('logger')
]);
$loggerMiddleware->setPublic(false);
$loggerMiddleware->addTag('monolog.logger', ['channel' => 'command_bus']);

$container->setDefinition('tactician.middleware.logger', $loggerMiddleware);
}


private function configureLoggerFormatters(ContainerBuilder $container)
{
$container->setDefinition(
'tactician.logger.class_properties_formatter',
new Definition(ClassPropertiesFormatter::class)
)->setPublic(false);

$container->setDefinition(
'tactician.logger.class_name_formatter',
new Definition(ClassNameFormatter::class)
)->setPublic(false);
}
}
26 changes: 26 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -36,6 +36,7 @@ public function testDefaultConfiguration()
'default_bus' => 'default',
'method_inflector' => 'tactician.handler.method_name_inflector.handle',
'security' => [],
'logger_formatter' => 'tactician.logger.class_properties_formatter'
]
);
}
Expand Down Expand Up @@ -239,4 +240,29 @@ public function testSecurityConfiguration()
]
]);
}

public function testCustomLoggerFormatterCanBeSet()
{
$this->assertConfigurationIsValid(
[
'tactician' => [
'logger_formatter' => 'some.formatter.service',
'commandbus' => [
'default' => [
'middleware' => [
'my_middleware.custom.stuff',
'my_middleware.custom.other_stuff',
],
],
'second' => [
'middleware' => [
'my_middleware.custom.stuff',
'my_middleware.custom.other_stuff',
]
]
]
]
]
);
}
}
10 changes: 10 additions & 0 deletions tests/DependencyInjection/TacticianExtensionTest.php
Expand Up @@ -4,6 +4,7 @@

use League\Tactician\Bundle\DependencyInjection\TacticianExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

class TacticianExtensionTest extends AbstractExtensionTestCase
Expand Down Expand Up @@ -64,4 +65,13 @@ public function testVoterIsNotLoadedWithoutSecurityMiddleware()

$this->assertContainerBuilderNotHasService('tactician.middleware.security_voter');
}

public function testLoggerMiddlewareIsCreated()
{
$this->load();

$this->assertContainerBuilderHasService('tactician.middleware.logger');
$this->assertContainerBuilderHasService('tactician.logger.class_properties_formatter');
$this->assertContainerBuilderHasService('tactician.logger.class_name_formatter');
}
}

0 comments on commit 3cd7a8a

Please sign in to comment.