-
-
Notifications
You must be signed in to change notification settings - Fork 45
Add debug command #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samdark
wants to merge
9
commits into
master
Choose a base branch
from
add-debug-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add debug command #372
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
624482f
Add debug command
samdark bd2dcea
Apply fixes from StyleCI
StyleCIBot e06790e
Add CHANGELOG
samdark 19dc9cf
Merge branch 'master' into add-debug-command
vjik 8e0d0fe
Make command dependencies optional
samdark f1d6923
Update symfony/console version
samdark da38a9f
Fix suggestion
samdark 79b8ef6
Fix suggestion
samdark c5eae16
Remove suggestions that are for development
samdark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Yiisoft\Di\Command\DebugContainerCommand; | ||
|
|
||
| return [ | ||
| 'yiisoft/yii-debug' => [ | ||
| 'ignoredCommands' => [ | ||
| 'debug:container', | ||
| ], | ||
| ], | ||
| 'yiisoft/yii-console' => [ | ||
| 'commands' => [ | ||
| 'debug:container' => DebugContainerCommand::class, | ||
| ], | ||
| ], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Di\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 Yiisoft\Config\ConfigInterface; | ||
| use Yiisoft\Definitions\ArrayDefinition; | ||
| use Yiisoft\Definitions\CallableDefinition; | ||
| use Yiisoft\Definitions\ValueDefinition; | ||
| use Yiisoft\Di\Helpers\DefinitionNormalizer; | ||
| use Yiisoft\VarDumper\VarDumper; | ||
|
|
||
| #[AsCommand( | ||
| name: 'debug:container', | ||
| description: 'Show information about container', | ||
| )] | ||
| final class DebugContainerCommand 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->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 self::SUCCESS; | ||
| } | ||
|
|
||
| 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); | ||
| ksort($data); | ||
|
|
||
| $rows = $this->getGroupServices($data); | ||
|
|
||
| $table = new Table($output); | ||
| $table | ||
| ->setHeaderTitle($group) | ||
| ->setHeaders(['Service', 'Definition']) | ||
| ->setRows($rows); | ||
| $table->render(); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| $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 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 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Di\Tests\Unit\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\Command\DebugContainerCommand; | ||
| use Yiisoft\Di\Container; | ||
| use Yiisoft\Di\ContainerConfig; | ||
|
|
||
| final class DebugContainerCommandTest extends TestCase | ||
| { | ||
| public function testCommand(): void | ||
| { | ||
| $container = $this->createContainer(); | ||
| $config = $container->get(ConfigInterface::class); | ||
| // trigger config build | ||
| $config->get('params'); | ||
|
|
||
| $command = new DebugContainerCommand($container); | ||
| $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(__DIR__ . '/config'), | ||
| ], | ||
| ], | ||
| ]); | ||
| return new Container($config); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| '/'=>[ | ||
| 'params' => [ | ||
|
|
||
| ] | ||
| ], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ], | ||
| ], | ||
| ], | ||
| ]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to debug tags. Or can I pass
tag@tag-nameas an id?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to see all services with given tag. Is it possible now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yiisoft/config must provide API to explore the config files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BoShurik no, it is not possible yet.