Skip to content

Commit 9e301dc

Browse files
Simperfitnoemi-salaun
authored andcommitted
[FrameworkBundle] add --deprecations on debug:container command
1 parent acad376 commit 9e301dc

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* The `TemplateController` now accepts context argument
1414
* Deprecated *not* setting the "framework.router.utf8" configuration option as it will default to `true` in Symfony 6.0
1515
* Added tag `routing.expression_language_function` to define functions available in route conditions
16+
* Added `debug:container --deprecations` command to see compile-time deprecations.
1617

1718
5.0.0
1819
-----

Command/ContainerDebugCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ protected function configure()
6262
new InputOption('env-vars', null, InputOption::VALUE_NONE, 'Displays environment variables used in the container'),
6363
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
6464
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
65+
new InputOption('deprecations', null, InputOption::VALUE_NONE, 'To output the deprecations generated when compiling and warming the cache'),
6566
])
6667
->setDescription('Displays current services for an application')
6768
->setHelp(<<<'EOF'
6869
The <info>%command.name%</info> command displays all configured <comment>public</comment> services:
6970
7071
<info>php %command.full_name%</info>
72+
73+
To see deprecations generated during container compilation and cache warmup, use the <info>--deprecations</info> flag:
74+
75+
<info>php %command.full_name% --deprecations</info>
7176
7277
To get specific information about a service, specify its name:
7378
@@ -149,6 +154,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
149154
} elseif ($name = $input->getArgument('name')) {
150155
$name = $this->findProperServiceName($input, $errorIo, $object, $name, $input->getOption('show-hidden'));
151156
$options = ['id' => $name];
157+
} elseif ($input->getOption('deprecations')) {
158+
$options = ['deprecations' => true];
152159
} else {
153160
$options = [];
154161
}

Console/Descriptor/Descriptor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public function describe(OutputInterface $output, $object, array $options = [])
6464
case $object instanceof ContainerBuilder && isset($options['parameter']):
6565
$this->describeContainerParameter($object->resolveEnvPlaceholders($object->getParameter($options['parameter'])), $options);
6666
break;
67+
case $object instanceof ContainerBuilder && isset($options['deprecations']):
68+
$this->describeContainerDeprecations($object, $options);
69+
break;
6770
case $object instanceof ContainerBuilder:
6871
$this->describeContainerServices($object, $options);
6972
break;
@@ -120,6 +123,8 @@ abstract protected function describeContainerService($service, array $options =
120123
*/
121124
abstract protected function describeContainerServices(ContainerBuilder $builder, array $options = []);
122125

126+
abstract protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []);
127+
123128
abstract protected function describeContainerDefinition(Definition $definition, array $options = []);
124129

125130
abstract protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $builder = null);

Console/Descriptor/JsonDescriptor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
154154
throw new LogicException('Using the JSON format to debug environment variables is not supported.');
155155
}
156156

157+
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
158+
{
159+
throw new LogicException('Using the JSON format to print the deprecations is not supported.');
160+
}
161+
157162
private function writeData(array $data, array $options)
158163
{
159164
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;

Console/Descriptor/MarkdownDescriptor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ protected function describeContainerService($service, array $options = [], Conta
104104
}
105105
}
106106

107+
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
108+
{
109+
throw new LogicException('Using the Markdown format to print the deprecations is not supported.');
110+
}
111+
107112
protected function describeContainerServices(ContainerBuilder $builder, array $options = [])
108113
{
109114
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];

Console/Descriptor/TextDescriptor.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,32 @@ protected function describeContainerDefinition(Definition $definition, array $op
353353
$options['output']->table($tableHeaders, $tableRows);
354354
}
355355

356+
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
357+
{
358+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
359+
if (!file_exists($containerDeprecationFilePath)) {
360+
$options['output']->warning('The deprecation file does not exist, please try warming the cache first.');
361+
362+
return;
363+
}
364+
365+
$logs = unserialize(file_get_contents($containerDeprecationFilePath));
366+
if (0 === \count($logs)) {
367+
$options['output']->success('There are no deprecations in the logs!');
368+
369+
return;
370+
}
371+
372+
$formattedLogs = [];
373+
$remainingCount = 0;
374+
foreach ($logs as $log) {
375+
$formattedLogs[] = sprintf("%sx: %s \n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
376+
$remainingCount += $log['count'];
377+
}
378+
$options['output']->title(sprintf('Remaining deprecations (%s)', $remainingCount));
379+
$options['output']->listing($formattedLogs);
380+
}
381+
356382
protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $builder = null)
357383
{
358384
if ($alias->isPublic()) {

Console/Descriptor/XmlDescriptor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
106106
throw new LogicException('Using the XML format to debug environment variables is not supported.');
107107
}
108108

109+
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
110+
{
111+
throw new LogicException('Using the XML format to print the deprecations is not supported.');
112+
}
113+
109114
private function writeDocument(\DOMDocument $dom)
110115
{
111116
$dom->formatOutput = true;

Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,88 @@ public function testDescribeEnvVar()
136136
$this->assertStringContainsString(file_get_contents(__DIR__.'/Fixtures/describe_env_vars.txt'), $tester->getDisplay(true));
137137
}
138138

139+
public function testGetDeprecation()
140+
{
141+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
142+
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
143+
touch($path);
144+
file_put_contents($path, serialize([[
145+
'type' => 16384,
146+
'message' => 'The "Symfony\Bundle\FrameworkBundle\Controller\Controller" class is deprecated since Symfony 4.2, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.',
147+
'file' => '/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php',
148+
'line' => 17,
149+
'trace' => [[
150+
'file' => '/home/hamza/projet/contrib/sf/src/Controller/DefaultController.php',
151+
'line' => 9,
152+
'function' => 'spl_autoload_call',
153+
]],
154+
'count' => 1,
155+
]]));
156+
$application = new Application(static::$kernel);
157+
$application->setAutoExit(false);
158+
159+
@unlink(static::$container->getParameter('debug.container.dump'));
160+
161+
$tester = new ApplicationTester($application);
162+
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
163+
164+
$this->assertSame(0, $tester->getStatusCode());
165+
$this->assertContains('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
166+
$this->assertContains('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
167+
}
168+
169+
public function testGetDeprecationNone()
170+
{
171+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
172+
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
173+
touch($path);
174+
file_put_contents($path, serialize([]));
175+
176+
$application = new Application(static::$kernel);
177+
$application->setAutoExit(false);
178+
179+
@unlink(static::$container->getParameter('debug.container.dump'));
180+
181+
$tester = new ApplicationTester($application);
182+
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
183+
184+
$this->assertSame(0, $tester->getStatusCode());
185+
$this->assertContains('[OK] There are no deprecations in the logs!', $tester->getDisplay());
186+
}
187+
188+
public function testGetDeprecationNoFile(): void
189+
{
190+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
191+
$path = sprintf('%s/%sDeprecations.log', static::$kernel->getContainer()->getParameter('kernel.cache_dir'), static::$kernel->getContainer()->getParameter('kernel.container_class'));
192+
@unlink($path);
193+
194+
$application = new Application(static::$kernel);
195+
$application->setAutoExit(false);
196+
197+
@unlink(static::$container->getParameter('debug.container.dump'));
198+
199+
$tester = new ApplicationTester($application);
200+
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
201+
202+
$this->assertSame(0, $tester->getStatusCode());
203+
$this->assertContains('[WARNING] The deprecation file does not exist', $tester->getDisplay());
204+
}
205+
206+
public function testGetDeprecationXml(): void
207+
{
208+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
209+
$application = new Application(static::$kernel);
210+
$application->setAutoExit(false);
211+
212+
@unlink(static::$container->getParameter('debug.container.dump'));
213+
214+
$tester = new ApplicationTester($application);
215+
$tester->run(['command' => 'debug:container', '--deprecations' => true, '--format' => 'xml']);
216+
217+
$this->assertSame(1, $tester->getStatusCode());
218+
$this->assertContains('Using the XML format to print the deprecations is not supported.', $tester->getDisplay());
219+
}
220+
139221
public function provideIgnoreBackslashWhenFindingService()
140222
{
141223
return [

0 commit comments

Comments
 (0)