From 587c292dbbe328773a63d0d5a9b9a58a65b142ab Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 13 Sep 2024 13:04:00 +0300 Subject: [PATCH] Move commands to corresponding packages --- README.md | 2 +- config/params.php | 4 - src/Command/DebugContainerCommand.php | 194 ------------------ src/Command/DebugEventsCommand.php | 128 ------------ .../Application/config/.merge-plan.php | 11 - tests/Support/Application/config/param1.php | 13 -- .../Command/DebugContainerCommandTest.php | 58 ------ tests/Unit/Command/DebugEventsCommandTest.php | 53 ----- 8 files changed, 1 insertion(+), 462 deletions(-) delete mode 100644 src/Command/DebugContainerCommand.php delete mode 100644 src/Command/DebugEventsCommand.php delete mode 100644 tests/Support/Application/config/.merge-plan.php delete mode 100644 tests/Support/Application/config/param1.php delete mode 100644 tests/Unit/Command/DebugContainerCommandTest.php delete mode 100644 tests/Unit/Command/DebugEventsCommandTest.php diff --git a/README.md b/README.md index fdcc1128a..f66d5f52e 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ composer require yiisoft/yii-debug --dev ``` > The debug extension also can be installed without the `--dev` flag if you want to collect data in production. -> Specify needed collectors only to reduce functions overriding and improve performance. +> Specify the necessary collectors only to reduce functions overriding and improve performance. ## General usage diff --git a/config/params.php b/config/params.php index 2c02981ac..408017916 100644 --- a/config/params.php +++ b/config/params.php @@ -24,8 +24,6 @@ use Yiisoft\Yii\Debug\Collector\VarDumperCollector; use Yiisoft\Yii\Debug\Collector\Web\RequestCollector; use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector; -use Yiisoft\Yii\Debug\Command\DebugContainerCommand; -use Yiisoft\Yii\Debug\Command\DebugEventsCommand; use Yiisoft\Yii\Debug\Command\DebugResetCommand; /** @@ -90,8 +88,6 @@ 'yiisoft/yii-console' => [ 'commands' => [ 'debug:reset' => DebugResetCommand::class, - 'debug:container' => DebugContainerCommand::class, - 'debug:events' => DebugEventsCommand::class, ], ], ]; diff --git a/src/Command/DebugContainerCommand.php b/src/Command/DebugContainerCommand.php deleted file mode 100644 index f3dcf38d9..000000000 --- a/src/Command/DebugContainerCommand.php +++ /dev/null @@ -1,194 +0,0 @@ -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 - { - $this->debugger->stop(); - $config = $this->container->get(ConfigInterface::class); - - $io = new SymfonyStyle($input, $output); - - if ($input->hasArgument('id') && !empty($ids = $input->getArgument('id'))) { - $build = $this->getConfigBuild($config); - foreach ($ids as $id) { - $definition = null; - foreach ($build as $definitions) { - if (array_key_exists($id, $definitions)) { - $definition = $definitions[$id]; - } - } - if ($definition === null) { - $io->error( - sprintf( - 'Service "%s" not found.', - $id, - ) - ); - continue; - } - $io->title($id); - - $normalizedDefinition = DefinitionNormalizer::normalize($definition, $id); - if ($normalizedDefinition instanceof ArrayDefinition) { - $definitionList = ['ID' => $id]; - if (class_exists($normalizedDefinition->getClass())) { - $definitionList[] = ['Class' => $normalizedDefinition->getClass()]; - } - if (!empty($normalizedDefinition->getConstructorArguments())) { - $definitionList[] = [ - 'Constructor' => $this->export( - $normalizedDefinition->getConstructorArguments() - ), - ]; - } - if (!empty($normalizedDefinition->getMethodsAndProperties())) { - $definitionList[] = [ - 'Methods' => $this->export( - $normalizedDefinition->getMethodsAndProperties() - ), - ]; - } - if (isset($definition['tags'])) { - $definitionList[] = ['Tags' => $this->export($definition['tags'])]; - } - - $io->definitionList(...$definitionList); - - continue; - } - if ($normalizedDefinition instanceof CallableDefinition || $normalizedDefinition instanceof ValueDefinition) { - $io->text( - $this->export($definition) - ); - continue; - } - - $output->writeln([ - $id, - VarDumper::create($normalizedDefinition)->asString(), - ]); - } - - return ExitCode::OK; - } - - 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 ExitCode::OK; - } - if ($input->hasOption('group') && !empty($group = $input->getOption('group'))) { - $data = $config->get($group); - ksort($data); - - $rows = $this->getGroupServices($data); - - $table = new Table($output); - $table - ->setHeaderTitle($group) - ->setHeaders(['Service', 'Definition']) - ->setRows($rows); - $table->render(); - - return ExitCode::OK; - } - - $build = $this->getConfigBuild($config); - - foreach ($build as $group => $data) { - $rows = $this->getGroupServices($data); - - $table = new Table($output); - $table - ->setHeaderTitle($group) - ->setHeaders(['Group', 'Services']) - ->setRows($rows); - $table->render(); - } - - return ExitCode::OK; - } - - private function getConfigBuild(mixed $config): array - { - $reflection = new ReflectionClass($config); - $buildReflection = $reflection->getProperty('build'); - $buildReflection->setAccessible(true); - return $buildReflection->getValue($config); - } - - protected function getGroupServices(array $data): array - { - $rows = []; - foreach ($data as $id => $definition) { - $class = ''; - if (is_string($definition)) { - $class = $definition; - } - if (is_array($definition)) { - $class = $definition['class'] ?? $id; - } - if (is_object($definition)) { - $class = $definition::class; - } - - $rows[] = [ - $id, - $class, - ]; - } - return $rows; - } - - protected function export(mixed $value): string - { - return VarDumper::create($value)->asString(); - } -} diff --git a/src/Command/DebugEventsCommand.php b/src/Command/DebugEventsCommand.php deleted file mode 100644 index c57e63e2c..000000000 --- a/src/Command/DebugEventsCommand.php +++ /dev/null @@ -1,128 +0,0 @@ -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 - { - $this->debugger->stop(); - $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 ExitCode::OK; - } - if ($input->hasOption('group') && !empty($group = $input->getOption('group'))) { - $data = $config->get($group); - ksort($data); - $table = new Table($output); - - foreach ($data as $event => $listeners) { - $io->title($event); - 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 ExitCode::OK; - } - - $data = []; - if ($config->has('events')) { - $data = array_merge($data, $config->get('events')); - } - if ($config->has('events-console')) { - $data = array_merge($data, $config->get('events-console')); - } - $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], - $listener[1] - ); - } - return $this->export($listener); - }, $listeners) - ), - ]; - } - $table = new Table($output); - $table - ->setHeaders(['Event', 'Count', 'Listeners']) - ->setRows($rows); - $table->render(); - - return ExitCode::OK; - } - - 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(); - } -} diff --git a/tests/Support/Application/config/.merge-plan.php b/tests/Support/Application/config/.merge-plan.php deleted file mode 100644 index f60be9931..000000000 --- a/tests/Support/Application/config/.merge-plan.php +++ /dev/null @@ -1,11 +0,0 @@ -[ - 'params' => [ - - ] - ], -]; diff --git a/tests/Support/Application/config/param1.php b/tests/Support/Application/config/param1.php deleted file mode 100644 index c9d956327..000000000 --- a/tests/Support/Application/config/param1.php +++ /dev/null @@ -1,13 +0,0 @@ - [ - 'params' => [ - 'yiitest/yii-debug' => [ - 'param1.php', - ], - ], - ], -]; diff --git a/tests/Unit/Command/DebugContainerCommandTest.php b/tests/Unit/Command/DebugContainerCommandTest.php deleted file mode 100644 index 07778828d..000000000 --- a/tests/Unit/Command/DebugContainerCommandTest.php +++ /dev/null @@ -1,58 +0,0 @@ -createContainer(); - $idGenerator = new DebuggerIdGenerator(); - $storage = $this->createMock(StorageInterface::class); - $storage->expects($this->never())->method('clear'); - $debugger = new Debugger($idGenerator, $storage, []); - - $config = $container->get(ConfigInterface::class); - // trigger config build - $config->get('params'); - - $command = new DebugContainerCommand($container, $debugger); - $commandTester = new CommandTester($command); - - $commandTester->execute([]); - - $this->assertEquals(0, $commandTester->getStatusCode()); - } - - private function createContainer(): ContainerInterface - { - $config = ContainerConfig::create() - ->withDefinitions([ - LoggerInterface::class => NullLogger::class, - ConfigInterface::class => [ - 'class' => Config::class, - '__construct()' => [ - new ConfigPaths(dirname(__DIR__, 2) . '/Support/Application/config'), - ], - ], - ]); - return new Container($config); - } -} diff --git a/tests/Unit/Command/DebugEventsCommandTest.php b/tests/Unit/Command/DebugEventsCommandTest.php deleted file mode 100644 index cce470244..000000000 --- a/tests/Unit/Command/DebugEventsCommandTest.php +++ /dev/null @@ -1,53 +0,0 @@ -createContainer(); - $idGenerator = new DebuggerIdGenerator(); - $storage = $this->createMock(StorageInterface::class); - $storage->expects($this->never())->method('clear'); - $debugger = new Debugger($idGenerator, $storage, []); - - $command = new DebugEventsCommand($container, $debugger); - - $commandTester = new CommandTester($command); - - $commandTester->execute([]); - } - - private function createContainer(): ContainerInterface - { - $config = ContainerConfig::create() - ->withDefinitions([ - LoggerInterface::class => NullLogger::class, - ConfigInterface::class => [ - 'class' => Config::class, - '__construct()' => [ - new ConfigPaths(dirname(__DIR__, 2) . '/Support/Application/config'), - ], - ], - ]); - return new Container($config); - } -}