diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 58b22fb..f374232 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -14,6 +14,7 @@ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('redirection_io'); + $excludedPrefixes = ['/_wdt', '/_profiler', '/_error']; $rootNode ->children() @@ -33,6 +34,15 @@ public function getConfigTreeBuilder() ->info('Throw exception if something wrong happens') ->defaultValue('%kernel.debug%') ->end() + ->arrayNode('excluded_prefixes') + ->info('Exclude a set of prefixes from processing') + ->prototype('scalar')->end() + ->validate() + ->always() + ->then(function ($v) use ($excludedPrefixes) { return array_unique(array_merge($excludedPrefixes, $v)); }) + ->end() + ->defaultValue($excludedPrefixes) + ->end() ->end() ; diff --git a/DependencyInjection/RedirectionIOExtension.php b/DependencyInjection/RedirectionIOExtension.php index 726eb77..b205f5a 100644 --- a/DependencyInjection/RedirectionIOExtension.php +++ b/DependencyInjection/RedirectionIOExtension.php @@ -2,6 +2,7 @@ namespace RedirectionIO\Client\ProxySymfony\DependencyInjection; +use RedirectionIO\Client\ProxySymfony\EventListener\RequestResponseListener; use RedirectionIO\Client\Sdk\Client; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -30,5 +31,10 @@ public function load(array $configs, ContainerBuilder $container) ->replaceArgument(0, $connections) ->replaceArgument(2, $config['debug']) ; + + $container + ->getDefinition(RequestResponseListener::class) + ->replaceArgument(1, $config['excluded_prefixes']) + ; } } diff --git a/EventListener/RequestResponseListener.php b/EventListener/RequestResponseListener.php index 01c3f72..17611f9 100644 --- a/EventListener/RequestResponseListener.php +++ b/EventListener/RequestResponseListener.php @@ -2,6 +2,7 @@ namespace RedirectionIO\Client\ProxySymfony\EventListener; +use RedirectionIO\Client\Sdk\Exception\ExceptionInterface; use RedirectionIO\Client\Sdk\Client; use RedirectionIO\Client\Sdk\HttpMessage\Request; use RedirectionIO\Client\Sdk\HttpMessage\Response; @@ -14,10 +15,12 @@ class RequestResponseListener { private $client; + private $excludedPrefixes; - public function __construct(Client $client) + public function __construct(Client $client, array $excludedPrefixes = []) { $this->client = $client; + $this->excludedPrefixes = $excludedPrefixes; } public function onKernelRequest(GetResponseEvent $event) @@ -27,6 +30,11 @@ public function onKernelRequest(GetResponseEvent $event) } $request = $event->getRequest(); + + if ($this->isExcludedPrefix($request->getPathInfo())) { + return; + } + $response = $this->client->findRedirect($this->createSdkRequest($request)); $request->attributes->set('redirectionio_response', $response); @@ -41,6 +49,10 @@ public function onKernelRequest(GetResponseEvent $event) public function onKernelTerminate(PostResponseEvent $event) { + if ($this->isExcludedPrefix($event->getRequest()->getPathInfo())) { + return; + } + $response = $event->getRequest()->attributes->get('redirectionio_response'); if (!$response) { @@ -49,7 +61,11 @@ public function onKernelTerminate(PostResponseEvent $event) $request = $this->createSdkRequest($event->getRequest()); - $this->client->log($request, $response); + try { + $this->client->log($request, $response); + } catch (ExceptionInterface $exception) { + // do nothing + } } private function createSdkRequest(SymfonyRequest $symfonyRequest) @@ -62,4 +78,15 @@ private function createSdkRequest(SymfonyRequest $symfonyRequest) $symfonyRequest->getScheme() ); } + + private function isExcludedPrefix($url): bool + { + foreach ($this->excludedPrefixes as $excludedPrefix) { + if (0 === strpos($url, $excludedPrefix)) { + return true; + } + } + + return false; + } } diff --git a/README.md b/README.md index 80514c1..547829e 100644 --- a/README.md +++ b/README.md @@ -34,4 +34,7 @@ redirection_io: connections: agent_tcp: tcp://127.0.0.1:20301 agent_unix: unix:///var/run/redirectionio_agent.sock + excluded_prefixes: + - /admin + # ... ``` diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 6775388..a0a171f 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,6 +6,7 @@ +