From 6b960832aaca9dd3a4811ab1ba5f5ba306688f27 Mon Sep 17 00:00:00 2001 From: Valerio Barbera Date: Sun, 29 Dec 2024 11:17:55 +0100 Subject: [PATCH] support wildcard for commands --- src/DependencyInjection/InspectorExtension.php | 2 +- src/Filters.php | 18 ++++++++++++++++++ src/Listeners/ConsoleEventsSubscriber.php | 9 ++++++++- src/Listeners/KernelEventsSubscriber.php | 15 ++------------- 4 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 src/Filters.php diff --git a/src/DependencyInjection/InspectorExtension.php b/src/DependencyInjection/InspectorExtension.php index 26c6ef4..7a8573a 100644 --- a/src/DependencyInjection/InspectorExtension.php +++ b/src/DependencyInjection/InspectorExtension.php @@ -22,7 +22,7 @@ class InspectorExtension extends Extension /** * Current version of the bundle. */ - const VERSION = '1.3.7'; + const VERSION = '1.3.8'; /** * Loads a specific configuration. diff --git a/src/Filters.php b/src/Filters.php new file mode 100644 index 0000000..d3a27d4 --- /dev/null +++ b/src/Filters.php @@ -0,0 +1,18 @@ +getName(), $this->ignoredCommands); + foreach ($this->ignoredCommands as $pattern) { + if (Filters::matchWithWildcard($pattern, $command->getName())) { + return true; + } + } + + return false; } } diff --git a/src/Listeners/KernelEventsSubscriber.php b/src/Listeners/KernelEventsSubscriber.php index 208e28c..26e2e4e 100644 --- a/src/Listeners/KernelEventsSubscriber.php +++ b/src/Listeners/KernelEventsSubscriber.php @@ -3,6 +3,7 @@ namespace Inspector\Symfony\Bundle\Listeners; use Inspector\Inspector; +use Inspector\Symfony\Bundle\Filters; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; use Symfony\Component\HttpKernel\Event\ControllerEvent; @@ -320,7 +321,7 @@ public function onKernelView(ViewEvent $event): void protected function isRequestEligibleForInspection($path): bool { foreach ($this->ignoredRoutes as $pattern) { - if ($this->matchUrlWithWildcard($pattern, $path)) { + if (Filters::matchWithWildcard($pattern, $path)) { return false; } } @@ -328,18 +329,6 @@ protected function isRequestEligibleForInspection($path): bool return true; } - public function matchUrlWithWildcard(string $pattern, string $url): bool - { - // Escape special regex characters in the pattern, except for '*'. - $escapedPattern = preg_quote($pattern, '/'); - - // Replace '*' in the pattern with '.*' for regex matching. - $regex = '/^' . str_replace('\*', '.*', $escapedPattern) . '$/'; - - // Perform regex match. - return (bool)preg_match($regex, $url); - } - private function controllerLabel(KernelEvent $event): ?string { $controllerLabel = $event->getRequest()->attributes->get('_controller');