-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Event\Command; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use ReflectionClass; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\VarDumper\VarDumper as SymfonyVarDumper; | ||
use Yiisoft\Config\ConfigInterface; | ||
use Yiisoft\VarDumper\VarDumper; | ||
|
||
#[AsCommand( | ||
name: 'debug:events', | ||
description: 'Show information about events and listeners', | ||
)] | ||
final class DebugEventsCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly ContainerInterface $container, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('id', InputArgument::IS_ARRAY, 'Service ID') | ||
->addOption('groups', null, InputOption::VALUE_NONE, 'Show groups') | ||
->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'Show group'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$config = $this->container->get(ConfigInterface::class); | ||
|
||
$io = new SymfonyStyle($input, $output); | ||
|
||
if ($input->hasOption('groups') && $input->getOption('groups')) { | ||
$build = $this->getConfigBuild($config); | ||
$groups = array_keys($build); | ||
sort($groups); | ||
|
||
$io->table(['Groups'], array_map(static fn ($group) => [$group], $groups)); | ||
|
||
return self::SUCCESS; | ||
} | ||
if ($input->hasOption('group') && !empty($group = $input->getOption('group'))) { | ||
$data = $config->get($group); | ||
Check failure on line 57 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedMethodCall
Check failure on line 57 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedMethodCall
|
||
ksort($data); | ||
Check failure on line 58 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 58 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
|
||
$table = new Table($output); | ||
|
||
foreach ($data as $event => $listeners) { | ||
$io->title($event); | ||
Check failure on line 62 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 62 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
|
||
foreach ($listeners as $listener) { | ||
if (is_callable($listener) && !is_array($listener)) { | ||
SymfonyVarDumper::dump($this->export($listener)); | ||
} else { | ||
SymfonyVarDumper::dump($listener); | ||
} | ||
} | ||
$table->render(); | ||
$io->newLine(); | ||
} | ||
return self::SUCCESS; | ||
} | ||
|
||
$data = []; | ||
if ($config->has('events')) { | ||
Check failure on line 77 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedMethodCall
Check failure on line 77 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedMethodCall
|
||
$data = array_merge($data, $config->get('events')); | ||
Check failure on line 78 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 78 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedMethodCall
Check failure on line 78 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
Check failure on line 78 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedMethodCall
|
||
} | ||
if ($config->has('events-console')) { | ||
Check failure on line 80 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedMethodCall
Check failure on line 80 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedMethodCall
|
||
$data = array_merge($data, $config->get('events-console')); | ||
Check failure on line 81 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 81 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedMethodCall
Check failure on line 81 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
Check failure on line 81 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedMethodCall
|
||
} | ||
$rows = []; | ||
foreach ($data as $event => $listeners) { | ||
$rows[] = [ | ||
$event, | ||
is_countable($listeners) ? count($listeners) : 0, | ||
implode( | ||
"\n", | ||
array_map(function (mixed $listener) { | ||
if (is_array($listener)) { | ||
return sprintf( | ||
'%s::%s', | ||
$listener[0], | ||
Check failure on line 94 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 94 in src/Command/DebugEventsCommand.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
|
||
$listener[1] | ||
); | ||
} | ||
return $this->export($listener); | ||
}, $listeners) | ||
), | ||
]; | ||
} | ||
$table = new Table($output); | ||
$table | ||
->setHeaders(['Event', 'Count', 'Listeners']) | ||
->setRows($rows); | ||
$table->render(); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function getConfigBuild(mixed $config): array | ||
{ | ||
$reflection = new ReflectionClass($config); | ||
$buildReflection = $reflection->getProperty('build'); | ||
$buildReflection->setAccessible(true); | ||
return $buildReflection->getValue($config); | ||
} | ||
|
||
protected function export(mixed $value): string | ||
{ | ||
return VarDumper::create($value)->asString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Yiisoft\Yii\Event\Tests\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Yiisoft\Config\Config; | ||
use Yiisoft\Config\ConfigInterface; | ||
use Yiisoft\Config\ConfigPaths; | ||
use Yiisoft\Di\Container; | ||
use Yiisoft\Di\ContainerConfig; | ||
use Yiisoft\Yii\Event\Command\DebugEventsCommand; | ||
|
||
final class DebugEventsCommandTest extends TestCase | ||
{ | ||
public function testCommand(): void | ||
{ | ||
$container = $this->createContainer(); | ||
$command = new DebugEventsCommand($container); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([]); | ||
|
||
$commandTester->assertCommandIsSuccessful(); | ||
$output = $commandTester->getDisplay(); | ||
$this->assertStringContainsString('Listeners', $output); | ||
} | ||
|
||
private function createContainer(): ContainerInterface | ||
{ | ||
$config = ContainerConfig::create() | ||
->withDefinitions([ | ||
LoggerInterface::class => NullLogger::class, | ||
ConfigInterface::class => [ | ||
'class' => Config::class, | ||
'__construct()' => [ | ||
new ConfigPaths(__DIR__ . '/config'), | ||
], | ||
], | ||
]); | ||
return new Container($config); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
'/'=>[ | ||
'params' => [ | ||
|
||
] | ||
], | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
'/' => [ | ||
'params' => [ | ||
'yiitest/yii-debug' => [ | ||
'param1.php', | ||
], | ||
], | ||
], | ||
]; |