Skip to content
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

Add debug command #372

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ composer.phar
/phpunit.phar
/phpunit.xml
/.phpunit.cache
.phpunit.result.cache

# Static analysis
analysis.txt
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh #353: Add shortcut for tag reference #333 (@xepozz)
- Enh #356: Improve usage `NotFoundException` for cases with definitions (@vjik)
- Enh #364: Minor refactoring to improve performance of container (@samdark)
- New #372: Add `debug:container` console command (@samdark, @xepozz)
- Enh #375: Raise minimum PHP version to `^8.1` and refactor code (@vjik)

## 1.2.1 December 23, 2022
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,11 @@ $config = ContainerConfig::create()
$container = new Container($config);
```

## Configuration debugging

If you use the package with Yii3, `./yii debug:container` command is available.
It shows information about container.

## Documentation

- [Internals](docs/internals.md)
Expand Down
20 changes: 16 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.26",
"yiisoft/injector": "^1.0",
"yiisoft/test-support": "^3.0"
"yiisoft/test-support": "^3.0",
"yiisoft/config": "^1.3",
"yiisoft/var-dumper": "^1.7",
"symfony/console": "^5.4|^6.0|^7.0"
},
"suggest": {
"yiisoft/injector": "^1.0",
"phpbench/phpbench": "To run benchmarks."
"symfony/console": "For debug:container command",
"yiisoft/var-dumper": "For debug:container command"
},
"provide": {
"psr/container-implementation": "1.0.0"
Expand All @@ -66,6 +69,14 @@
"Yiisoft\\Di\\Tests\\": "tests"
}
},
"extra": {
"config-plugin-options": {
"source-directory": "config"
},
"config-plugin": {
"params": "params.php"
}
},
"scripts": {
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch"
Expand All @@ -77,7 +88,8 @@
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"yiisoft/config": false
}
}
}
18 changes: 18 additions & 0 deletions config/params.php
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,
],
],
];
190 changes: 190 additions & 0 deletions src/Command/DebugContainerCommand.php
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

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-name as an id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please elaborate?

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?

Copy link
Member

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

Copy link
Member Author

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.

{
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)) {

Check failure on line 55 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:55:42: MixedArgument: Argument 1 of array_key_exists cannot be mixed, expecting array-key (see https://psalm.dev/030)

Check failure on line 55 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:55:47: MixedArgument: Argument 2 of array_key_exists cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 55 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:55:42: MixedArgument: Argument 1 of array_key_exists cannot be mixed, expecting array-key (see https://psalm.dev/030)

Check failure on line 55 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:55:47: MixedArgument: Argument 2 of array_key_exists cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
$definition = $definitions[$id];

Check warning on line 56 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L51-L56

Added lines #L51 - L56 were not covered by tests
}
}
if ($definition === null) {
$io->error(
sprintf(
'Service "%s" not found.',
$id,

Check failure on line 63 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:63:29: MixedArgument: Argument 2 of sprintf cannot be mixed, expecting float|int|object{__tostring()}|string (see https://psalm.dev/030)

Check failure on line 63 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:63:29: MixedArgument: Argument 2 of sprintf cannot be mixed, expecting float|int|object{__tostring()}|string (see https://psalm.dev/030)
)

Check warning on line 64 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L59-L64

Added lines #L59 - L64 were not covered by tests
);
continue;

Check warning on line 66 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L66

Added line #L66 was not covered by tests
}
$io->title($id);

Check failure on line 68 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:68:28: MixedArgument: Argument 1 of Symfony\Component\Console\Style\SymfonyStyle::title cannot be mixed, expecting string (see https://psalm.dev/030)

Check failure on line 68 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:68:28: MixedArgument: Argument 1 of Symfony\Component\Console\Style\SymfonyStyle::title cannot be mixed, expecting string (see https://psalm.dev/030)

Check warning on line 68 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L68

Added line #L68 was not covered by tests

$normalizedDefinition = DefinitionNormalizer::normalize($definition, $id);
if ($normalizedDefinition instanceof ArrayDefinition) {
$definitionList = ['ID' => $id];
if (class_exists($normalizedDefinition->getClass())) {
$definitionList[] = ['Class' => $normalizedDefinition->getClass()];

Check warning on line 74 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L70-L74

Added lines #L70 - L74 were not covered by tests
}
if (!empty($normalizedDefinition->getConstructorArguments())) {
$definitionList[] = [
'Constructor' => $this->export(
$normalizedDefinition->getConstructorArguments()
),
];

Check warning on line 81 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L76-L81

Added lines #L76 - L81 were not covered by tests
}
if (!empty($normalizedDefinition->getMethodsAndProperties())) {
$definitionList[] = [
'Methods' => $this->export(
$normalizedDefinition->getMethodsAndProperties()
),
];

Check warning on line 88 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L83-L88

Added lines #L83 - L88 were not covered by tests
}
if (isset($definition['tags'])) {
$definitionList[] = ['Tags' => $this->export($definition['tags'])];

Check failure on line 91 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArrayAccess

src/Command/DebugContainerCommand.php:91:70: MixedArrayAccess: Cannot access array value on mixed variable $definition (see https://psalm.dev/051)

Check failure on line 91 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArrayAccess

src/Command/DebugContainerCommand.php:91:70: MixedArrayAccess: Cannot access array value on mixed variable $definition (see https://psalm.dev/051)

Check warning on line 91 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L90-L91

Added lines #L90 - L91 were not covered by tests
}

$io->definitionList(...$definitionList);

Check warning on line 94 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L94

Added line #L94 was not covered by tests

continue;

Check warning on line 96 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L96

Added line #L96 was not covered by tests
}
if ($normalizedDefinition instanceof CallableDefinition || $normalizedDefinition instanceof ValueDefinition) {
$io->text(
$this->export($definition)

Check warning on line 100 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L98-L100

Added lines #L98 - L100 were not covered by tests
);
continue;

Check warning on line 102 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L102

Added line #L102 was not covered by tests
}

$output->writeln([
$id,
VarDumper::create($normalizedDefinition)->asString(),
]);

Check warning on line 108 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L105-L108

Added lines #L105 - L108 were not covered by tests
}

return self::SUCCESS;

Check warning on line 111 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L111

Added line #L111 was not covered by tests
}

if ($input->hasOption('groups') && $input->getOption('groups')) {
$build = $this->getConfigBuild($config);
$groups = array_keys($build);
sort($groups);

Check warning on line 117 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L115-L117

Added lines #L115 - L117 were not covered by tests

$io->table(['Groups'], array_map(static fn ($group) => [$group], $groups));

Check warning on line 119 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L119

Added line #L119 was not covered by tests

return self::SUCCESS;

Check warning on line 121 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L121

Added line #L121 was not covered by tests
}
if ($input->hasOption('group') && !empty($group = $input->getOption('group'))) {
$data = $config->get($group);

Check failure on line 124 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:124:34: MixedArgument: Argument 1 of Yiisoft\Config\ConfigInterface::get cannot be non-empty-mixed, expecting string (see https://psalm.dev/030)

Check failure on line 124 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:124:34: MixedArgument: Argument 1 of Yiisoft\Config\ConfigInterface::get cannot be non-empty-mixed, expecting string (see https://psalm.dev/030)
ksort($data);

Check warning on line 125 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L124-L125

Added lines #L124 - L125 were not covered by tests

$rows = $this->getGroupServices($data);

Check warning on line 127 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L127

Added line #L127 was not covered by tests

$table = new Table($output);
$table
->setHeaderTitle($group)
->setHeaders(['Service', 'Definition'])
->setRows($rows);
$table->render();

Check warning on line 134 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L129-L134

Added lines #L129 - L134 were not covered by tests

return self::SUCCESS;

Check warning on line 136 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L136

Added line #L136 was not covered by tests
}

$build = $this->getConfigBuild($config);

foreach ($build as $group => $data) {
$rows = $this->getGroupServices($data);

Check failure on line 142 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:142:45: MixedArgument: Argument 1 of Yiisoft\Di\Command\DebugContainerCommand::getGroupServices cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 142 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:142:45: MixedArgument: Argument 1 of Yiisoft\Di\Command\DebugContainerCommand::getGroupServices cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

$table = new Table($output);
$table
->setHeaderTitle($group)

Check failure on line 146 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgumentTypeCoercion

src/Command/DebugContainerCommand.php:146:34: MixedArgumentTypeCoercion: Argument 1 of Symfony\Component\Console\Helper\Table::setHeaderTitle expects null|string, but parent type array-key provided (see https://psalm.dev/194)

Check failure on line 146 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgumentTypeCoercion

src/Command/DebugContainerCommand.php:146:34: MixedArgumentTypeCoercion: Argument 1 of Symfony\Component\Console\Helper\Table::setHeaderTitle expects null|string, but parent type array-key provided (see https://psalm.dev/194)
->setHeaders(['Group', 'Services'])
->setRows($rows);
$table->render();
}

return self::SUCCESS;
}

private function getConfigBuild(mixed $config): array

Check failure on line 155 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedInferredReturnType

src/Command/DebugContainerCommand.php:155:53: MixedInferredReturnType: Could not verify return type 'array<array-key, mixed>' for Yiisoft\Di\Command\DebugContainerCommand::getConfigBuild (see https://psalm.dev/047)

Check failure on line 155 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedInferredReturnType

src/Command/DebugContainerCommand.php:155:53: MixedInferredReturnType: Could not verify return type 'array<array-key, mixed>' for Yiisoft\Di\Command\DebugContainerCommand::getConfigBuild (see https://psalm.dev/047)
{
$reflection = new ReflectionClass($config);

Check failure on line 157 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:157:43: MixedArgument: Argument 1 of ReflectionClass::__construct cannot be mixed, expecting class-string|object|trait-string (see https://psalm.dev/030)

Check failure on line 157 in src/Command/DebugContainerCommand.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/Command/DebugContainerCommand.php:157:43: MixedArgument: Argument 1 of ReflectionClass::__construct cannot be mixed, expecting class-string|object|trait-string (see https://psalm.dev/030)
$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;

Check warning on line 169 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L167-L169

Added lines #L167 - L169 were not covered by tests
}
if (is_array($definition)) {
$class = $definition['class'] ?? $id;

Check warning on line 172 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L171-L172

Added lines #L171 - L172 were not covered by tests
}
if (is_object($definition)) {
$class = $definition::class;

Check warning on line 175 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L174-L175

Added lines #L174 - L175 were not covered by tests
}

$rows[] = [
$id,
$class,
];

Check warning on line 181 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L178-L181

Added lines #L178 - L181 were not covered by tests
}
return $rows;
}

protected function export(mixed $value): string

Check warning on line 186 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L186

Added line #L186 was not covered by tests
{
return VarDumper::create($value)->asString();

Check warning on line 188 in src/Command/DebugContainerCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/DebugContainerCommand.php#L188

Added line #L188 was not covered by tests
}
}
50 changes: 50 additions & 0 deletions tests/Unit/Command/DebugContainerCommandTest.php
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);
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Command/config/.merge-plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return [
'/'=>[
'params' => [

]
],
];
13 changes: 13 additions & 0 deletions tests/Unit/Command/config/param1.php
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',
],
],
],
];
Loading