Skip to content

Commit

Permalink
add new listener for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Aug 20, 2022
1 parent f72e1b5 commit cb4cd12
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/paginator_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ for recent versions of Symfony and `app/config.yml` for older versions. If you c
knp_paginator:
page_range: 5 # default page range used in pagination control
page_limit: 100 # page limit for pagination control; to disable set this field to ~ (null)
convert_exception: false # convert paginator exception (e.g. non-positive page and/or limit) into 404 error
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name; to disable sorting set this field to ~ (null)
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->integerNode('page_limit')
->defaultNull()
->end()
->booleanNode('convert_exception')
->defaultFalse()
->end()
->end()
;

Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/KnpPaginatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Knp\Bundle\PaginatorBundle\DependencyInjection;

use Knp\Bundle\PaginatorBundle\EventListener\ExceptionListener;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand Down Expand Up @@ -50,5 +52,11 @@ public function load(array $configs, ContainerBuilder $container): void
'pageOutOfRange' => $config['default_options']['page_out_of_range'],
'defaultLimit' => $config['default_options']['default_limit'],
]]);

if ($config['convert_exception']) {
$definition = new Definition(ExceptionListener::class);
$definition->addTag('kernel.event_listener', ['event' => 'kernel.exception']);
$container->setDefinition(ExceptionListener::class, $definition);
}
}
}
21 changes: 21 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Knp\Bundle\PaginatorBundle\EventListener;

use OutOfRangeException;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Intercept OutOfRangeException and throw http-related exceptions instead.
*/
final class ExceptionListener
{
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof OutOfRangeException) {
$event->setThrowable(new NotFoundHttpException('Not Found.', $exception));
}
}
}
2 changes: 2 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function testDefaultConfig(): void
],
'page_range' => 5,
'page_limit' => null,
'convert_exception' => false,
], $config);
}

Expand All @@ -80,6 +81,7 @@ public function testCustomConfig(): void
],
'page_range' => 15,
'page_limit' => 100,
'convert_exception' => true,
];
$config = $this->processor->processConfiguration($this->configuration, ['knp_paginator' => $expected]);

Expand Down

0 comments on commit cb4cd12

Please sign in to comment.