diff --git a/docs/paginator_configuration.md b/docs/paginator_configuration.md index 362dfe3..53c21dd 100644 --- a/docs/paginator_configuration.md +++ b/docs/paginator_configuration.md @@ -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) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d45040f..5a57af9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -49,6 +49,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->integerNode('page_limit') ->defaultNull() ->end() + ->booleanNode('convert_exception') + ->defaultFalse() + ->end() ->end() ; diff --git a/src/DependencyInjection/KnpPaginatorExtension.php b/src/DependencyInjection/KnpPaginatorExtension.php index 3e465e3..fe4fb76 100644 --- a/src/DependencyInjection/KnpPaginatorExtension.php +++ b/src/DependencyInjection/KnpPaginatorExtension.php @@ -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; @@ -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); + } } } diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php new file mode 100644 index 0000000..603bbad --- /dev/null +++ b/src/EventListener/ExceptionListener.php @@ -0,0 +1,21 @@ +getThrowable(); + if ($exception instanceof OutOfRangeException) { + $event->setThrowable(new NotFoundHttpException('Not Found.', $exception)); + } + } +} diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 635ae42..ea765d0 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -55,6 +55,7 @@ public function testDefaultConfig(): void ], 'page_range' => 5, 'page_limit' => null, + 'convert_exception' => false, ], $config); } @@ -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]);