Skip to content

Commit

Permalink
support wildcard for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ilvalerione committed Dec 29, 2024
1 parent 96efe3d commit 6b96083
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/InspectorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/Filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Inspector\Symfony\Bundle;

class Filters
{
public static function matchWithWildcard(string $pattern, string $url)
{
// 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);
}
}
9 changes: 8 additions & 1 deletion src/Listeners/ConsoleEventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inspector\Symfony\Bundle\Listeners;

use Inspector\Inspector;
use Inspector\Symfony\Bundle\Filters;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand Down Expand Up @@ -134,6 +135,12 @@ public function onConsoleSignal(ConsoleSignalEvent $event): void

protected function isIgnored(Command $command): bool
{
return \in_array($command->getName(), $this->ignoredCommands);
foreach ($this->ignoredCommands as $pattern) {
if (Filters::matchWithWildcard($pattern, $command->getName())) {
return true;
}
}

return false;
}
}
15 changes: 2 additions & 13 deletions src/Listeners/KernelEventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -320,26 +321,14 @@ 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;
}
}

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');
Expand Down

0 comments on commit 6b96083

Please sign in to comment.