Skip to content

Commit

Permalink
Making the listener configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Mar 28, 2024
1 parent 0714470 commit 190e01c
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/vendor/
/tools/
/bin/
/tmp/
/.phpunit.cache/
composer.lock
5 changes: 4 additions & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="grumphp" version="^2.5.0" installed="2.5.0" location="./tools/grumphp" copy="false"/>
<phar name="grumphp" version="^2.5.0" installed="2.5.0" location="./bin/grumphp" copy="false"/>
<phar name="phpmd" version="^2.15.0" installed="2.15.0" location="./bin/phpmd" copy="false"/>
<phar name="phpcs" version="^3.7.1" installed="3.7.1" location="./bin/phpcs" copy="false"/>
<phar name="phpstan" version="^1.9.2" installed="1.9.2" location="./bin/phpstan" copy="false"/>
</phive>
3 changes: 3 additions & 0 deletions config/correlation_id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
correlation_id:
response_header_name: 'X-Correlation-ID'
request_header_name: 'X-Correlation-ID'
45 changes: 45 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
grumphp:
ascii:
failed: resources/grumphp-grumpy.txt
succeeded: resources/grumphp-happy.txt
fixer:
enabled: true
fix_by_default: true
tasks:
phpstan:
autoload_file: ~
configuration: phpstan.neon
level: null
force_patterns: []
ignore_patterns: []
triggered_by: ['php']
memory_limit: "-1"
use_grumphp_paths: true
phpmd:
whitelist_patterns: []
exclude: []
report_format: text
ruleset: ['cleancode', 'codesize', 'naming', 'design', 'unusedcode']
triggered_by: ['php']
phpcs:
standard: []
severity: ~
error_severity: ~
warning_severity: ~
tab_width: ~
report: full
report_width: ~
whitelist_patterns: []
encoding: ~
ignore_patterns: []
sniffs: []
triggered_by: [php]
exclude: []
show_sniffs_error_path: true
phpunit:
config_file: phpunit.xml.dist
testsuite: ~
group: []
exclude_group: []
always_execute: false
order: null
Empty file added resources/grumphp-grumpy.txt
Empty file.
2 changes: 2 additions & 0 deletions resources/grumphp-happy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

All good!
31 changes: 31 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Phauthentic\CorrelationIdBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('correlation_id');

$treeBuilder->getRootNode()

Check failure on line 16 in src/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method Symfony\Component\Config\Definition\Builder\NodeDefinition::children().
->children()
->booleanNode('passthrough')
->defaultValue(false)
->end()
->scalarNode('response_header_name')
->defaultValue('X-Correlation-ID')
->end()
->scalarNode('request_header_name')
->defaultValue('X-Correlation-ID')
->end()
->end();

return $treeBuilder;
}
}
13 changes: 12 additions & 1 deletion src/DependencyInjection/CorrelationIdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ class CorrelationIdExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$yamlLoader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$yamlLoader = new Loader\YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config')
);

$yamlLoader->load('services.yaml');

$configuration = new Configuration();

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

$container->setParameter('correlation_id.request_header_name', $config['response_header_name']);
$container->setParameter('correlation_id.response_header_name', $config['response_header_name']);
}
}
52 changes: 46 additions & 6 deletions src/EventSubscriber/CorrelationIdSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Phauthentic\CorrelationIdBundle\EventSubscriber;

use InvalidArgumentException;
use Exception;
use Phauthentic\Infrastructure\Utils\CorrelationID;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -12,25 +14,63 @@

class CorrelationIdSubscriber implements EventSubscriberInterface
{
private string $header = 'X-Correlation-ID';
private string $requestHeaderName = 'X-Correlation-ID';
private string $responseHeaderName = 'X-Correlation-ID';
private bool $passthrough = false;

/**
* @param array<string, mixed> $config
* @return void
*/
public function __construct(array $config = [])
{
if (isset($config['request_header_name'])) {
$this->requestHeaderName = (string)$config['request_header_name'];
}

if (isset($config['response_header_name'])) {
$this->responseHeaderName = (string)$config['response_header_name'];
}

if (isset($config['response_header_name'])) {
$this->passthrough = (bool)$config['passthrough'];
}
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @param RequestEvent $event
* @return void
*/
public function onKernelRequest(RequestEvent $event): void
{
$event->getRequest()->attributes->set($this->header, CorrelationID::toString());
$event->getRequest()->attributes->set($this->requestHeaderName, CorrelationID::toString());
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @param ResponseEvent $event
* @return void
* @throws InvalidArgumentException
* @throws Exception
*/
public function onKernelResponse(ResponseEvent $event): void
{
if ($event->getRequest()->headers->has($this->header)) {
if (
$this->passthrough
&& $event->getRequest()->headers->has($this->requestHeaderName)
) {
$event->getResponse()->headers->set(
$this->header,
$event->getRequest()->headers->get($this->header)
$this->responseHeaderName,
$event->getRequest()->headers->get($this->requestHeaderName)
);

return;
}

$event->getResponse()->headers->set($this->header, CorrelationID::toString());
$event->getResponse()->headers->set($this->responseHeaderName, CorrelationID::toString());
}

public static function getSubscribedEvents(): array
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/correlation_id.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
services:
Phauthentic\CorrelationIdBundle\EventSubscriber\CorrelationIdSubscriber:
arguments:
$config: '%correlation_id%'
tags:
- { name: 'kernel.event_subscriber' }
6 changes: 6 additions & 0 deletions tests/EventSubscriber/CorrelationIdSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class CorrelationIdSubscriberTest extends TestCase
{
private const CORRELATION_ID = 'd8d089ec-72c8-44c1-a0bf-1906e5fc3524';

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function setUp(): void
{
$reflection = new ReflectionClass(CorrelationID::class);
Expand Down Expand Up @@ -61,6 +64,9 @@ public function testOnKernelResponse(): void
$this->assertNotEmpty($response->headers->get('X-Correlation-ID'));
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function testGetSubscribedEvents(): void
{
$subscribedEvents = CorrelationIdSubscriber::getSubscribedEvents();
Expand Down

0 comments on commit 190e01c

Please sign in to comment.