Skip to content

[FrameworkBundle] add --deprecations on debug:container command #32584

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

Closed
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
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CHANGELOG
* Added new `error_controller` configuration to handle system exceptions
* Added sort option for `translation:update` command.
* [BC Break] The `framework.messenger.routing.senders` config key is not deep merged anymore.
* Added `debug:container --deprecations` command to see compile-time deprecations

4.3.0
-----
Expand All @@ -42,8 +43,8 @@ CHANGELOG
options if you're using Symfony's serializer.
* [BC Break] Removed the `framework.messenger.routing.send_and_handle` configuration.
Instead of setting it to true, configure a `SyncTransport` and route messages to it.
* Added information about deprecated aliases in `debug:autowiring`
* Added php ini session options `sid_length` and `sid_bits_per_character`
* Added information about deprecated aliases in `debug:autowiring`
* Added php ini session options `sid_length` and `sid_bits_per_character`
to the `session` section of the configuration
* Added support for Translator paths, Twig paths in translation commands.
* Added support for PHP files with translations in translation commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ protected function configure()
new InputOption('env-vars', null, InputOption::VALUE_NONE, 'Displays environment variables used in the container'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
new InputOption('deprecations', null, InputOption::VALUE_NONE, 'To output the deprecations generated when compiling and warming the cache'),
])
->setDescription('Displays current services for an application')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command displays all configured <comment>public</comment> services:

<info>php %command.full_name%</info>

To see deprecations generated during container compilation and cache warmup, use the <info>--deprecations</info> flag:

<info>php %command.full_name% --deprecations</info>

To get specific information about a service, specify its name:

Expand Down Expand Up @@ -149,6 +154,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
} elseif ($name = $input->getArgument('name')) {
$name = $this->findProperServiceName($input, $errorIo, $object, $name, $input->getOption('show-hidden'));
$options = ['id' => $name];
} elseif ($input->getOption('deprecations')) {
$options = ['deprecations' => true];
Copy link
Member

Choose a reason for hiding this comment

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

validateInput() needs to be updated to forbid combining this option with others.

} else {
$options = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function describe(OutputInterface $output, $object, array $options = [])
case $object instanceof ContainerBuilder && isset($options['parameter']):
$this->describeContainerParameter($object->resolveEnvPlaceholders($object->getParameter($options['parameter'])), $options);
break;
case $object instanceof ContainerBuilder && isset($options['deprecations']):
$this->describeContainerDeprecations($object, $options);
break;
case $object instanceof ContainerBuilder:
$this->describeContainerServices($object, $options);
break;
Expand Down Expand Up @@ -132,6 +135,11 @@ abstract protected function describeContainerService($service, array $options =
*/
abstract protected function describeContainerServices(ContainerBuilder $builder, array $options = []);

/**
* Describes container deprecations.
*/
Copy link
Member

Choose a reason for hiding this comment

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

can be removed

abstract protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []);

/**
* Describes a service definition.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
throw new LogicException('Using the JSON format to debug environment variables is not supported.');
}

/**
* {@inheritdoc}
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the JSON format to print the deprecations is not supported.');
}

/**
* Writes data as json.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ protected function describeContainerService($service, array $options = [], Conta
}
}

/**
* {@inheritdoc}
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the Markdown format to print the deprecations is not supported.');
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,35 @@ protected function describeContainerDefinition(Definition $definition, array $op
$options['output']->table($tableHeaders, $tableRows);
}

/**
* {@inheritdoc}
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
if (!file_exists($containerDeprecationFilePath)) {
$options['output']->warning('The deprecation file does not exist, please try warming the cache first.');

return;
}

$logs = unserialize(file_get_contents($containerDeprecationFilePath));
if (0 === \count($logs)) {
$options['output']->success('There are no deprecations in the logs!');

return;
}

$formattedLogs = [];
$remainingCount = 0;
foreach ($logs as $log) {
$formattedLogs[] = sprintf("%sx: %s \n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
Copy link
Member

Choose a reason for hiding this comment

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

we should use <href=%s>%s:%d</> here, to generate IDE links (which means using the FileLinkFormatter service to compute it)

$remainingCount += $log['count'];
}
$options['output']->title(sprintf('Remaining deprecations (%s)', $remainingCount));
$options['output']->listing($formattedLogs);
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
throw new LogicException('Using the XML format to debug environment variables is not supported.');
}

/**
* {@inheritdoc}
*/
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
{
throw new LogicException('Using the XML format to print the deprecations is not supported.');
}

/**
* Writes DOM document.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,88 @@ public function testDescribeEnvVar()
$this->assertStringContainsString(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
}

public function testGetDeprecation()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
touch($path);
file_put_contents($path, serialize([[
'type' => 16384,
'message' => 'The "Symfony\Bundle\FrameworkBundle\Controller\Controller" class is deprecated since Symfony 4.2, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.',
'file' => '/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php',
'line' => 17,
'trace' => [[
'file' => '/home/hamza/projet/contrib/sf/src/Controller/DefaultController.php',
'line' => 9,
'function' => 'spl_autoload_call',
]],
'count' => 1,
]]));
$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
$this->assertContains('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
}

public function testGetDeprecationNone()
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
touch($path);
file_put_contents($path, serialize([]));

$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[OK] There are no deprecations in the logs!', $tester->getDisplay());
}

public function testGetDeprecationNoFile(): void
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
@unlink($path);

$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertContains('[WARNING] The deprecation file does not exist', $tester->getDisplay());
}

public function testGetDeprecationXml(): void
{
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
$application = new Application(static::$kernel);
$application->setAutoExit(false);

@unlink(static::$container->getParameter('debug.container.dump'));

$tester = new ApplicationTester($application);
$tester->run(['command' => 'debug:container', '--deprecations' => true, '--format' => 'xml']);

$this->assertSame(1, $tester->getStatusCode());
$this->assertContains('Using the XML format to print the deprecations is not supported.', $tester->getDisplay());
}

public function provideIgnoreBackslashWhenFindingService()
{
return [
Expand Down