Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Add excluded_prefixes node in extension configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
qboot committed Jun 11, 2018
1 parent 92f352e commit 217e010
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -14,6 +14,7 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('redirection_io');
$excludedPrefixes = ['/_wdt', '/_profiler', '/_error'];

$rootNode
->children()
Expand All @@ -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()
;

Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/RedirectionIOExtension.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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'])
;
}
}
31 changes: 29 additions & 2 deletions EventListener/RequestResponseListener.php
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);

Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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;
}
}
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
# ...
```
1 change: 1 addition & 0 deletions Resources/config/services.xml
Expand Up @@ -6,6 +6,7 @@
<services>
<service id="RedirectionIO\Client\ProxySymfony\EventListener\RequestResponseListener" class="RedirectionIO\Client\ProxySymfony\EventListener\RequestResponseListener">
<argument type="service" id="RedirectionIO\Client\Sdk\Client" />
<argument /> <!-- excludedUrls, set by the extension -->
<tag name="kernel.event_listener" event="kernel.request" priority="1000" />
<tag name="kernel.event_listener" event="kernel.terminate" />
</service>
Expand Down

0 comments on commit 217e010

Please sign in to comment.