Skip to content

Commit c98322b

Browse files
noemi-salaunnicolas-grekas
authored andcommitted
[FrameworkBundle] add all formats support for debug:container --deprecations command
1 parent 9e301dc commit c98322b

20 files changed

+169
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +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.
16+
* Added `debug:container --deprecations` option to see compile-time deprecations.
1717

1818
5.0.0
1919
-----

Command/ContainerDebugCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ 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'),
65+
new InputOption('deprecations', null, InputOption::VALUE_NONE, 'Displays deprecations generated when compiling and warming up the container'),
6666
])
6767
->setDescription('Displays current services for an application')
6868
->setHelp(<<<'EOF'
6969
The <info>%command.name%</info> command displays all configured <comment>public</comment> services:
7070
7171
<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-
72+
73+
To see deprecations generated during container compilation and cache warmup, use the <info>--deprecations</info> option:
74+
7575
<info>php %command.full_name% --deprecations</info>
7676
7777
To get specific information about a service, specify its name:
@@ -187,7 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
187187
$errorIo->comment('To search for a specific tag, re-run this command with a search term. (e.g. <comment>debug:container --tag=form.type</comment>)');
188188
} elseif ($input->getOption('parameters')) {
189189
$errorIo->comment('To search for a specific parameter, re-run this command with a search term. (e.g. <comment>debug:container --parameter=kernel.debug</comment>)');
190-
} else {
190+
} elseif (!$input->getOption('deprecations')) {
191191
$errorIo->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
192192
}
193193
}

Console/Descriptor/Descriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ abstract protected function describeContainerService($service, array $options =
123123
*/
124124
abstract protected function describeContainerServices(ContainerBuilder $builder, array $options = []);
125125

126-
abstract protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []);
126+
abstract protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void;
127127

128128
abstract protected function describeContainerDefinition(Definition $definition, array $options = []);
129129

Console/Descriptor/JsonDescriptor.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;
1313

1414
use Symfony\Component\Console\Exception\LogicException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\DependencyInjection\Alias;
1617
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
1718
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
@@ -156,7 +157,26 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
156157

157158
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
158159
{
159-
throw new LogicException('Using the JSON format to print the deprecations is not supported.');
160+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
161+
if (!file_exists($containerDeprecationFilePath)) {
162+
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
163+
}
164+
165+
$logs = unserialize(file_get_contents($containerDeprecationFilePath));
166+
167+
$formattedLogs = [];
168+
$remainingCount = 0;
169+
foreach ($logs as $log) {
170+
$formattedLogs[] = [
171+
'message' => $log['message'],
172+
'file' => $log['file'],
173+
'line' => $log['line'],
174+
'count' => $log['count'],
175+
];
176+
$remainingCount += $log['count'];
177+
}
178+
179+
$this->writeData(['remainingCount' => $remainingCount, 'deprecations' => $formattedLogs], $options);
160180
}
161181

162182
private function writeData(array $data, array $options)

Console/Descriptor/MarkdownDescriptor.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;
1313

1414
use Symfony\Component\Console\Exception\LogicException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\DependencyInjection\Alias;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -106,7 +107,29 @@ protected function describeContainerService($service, array $options = [], Conta
106107

107108
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
108109
{
109-
throw new LogicException('Using the Markdown format to print the deprecations is not supported.');
110+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
111+
if (!file_exists($containerDeprecationFilePath)) {
112+
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
113+
}
114+
115+
$logs = unserialize(file_get_contents($containerDeprecationFilePath));
116+
if (0 === \count($logs)) {
117+
$this->write("## There are no deprecations in the logs!\n");
118+
119+
return;
120+
}
121+
122+
$formattedLogs = [];
123+
$remainingCount = 0;
124+
foreach ($logs as $log) {
125+
$formattedLogs[] = sprintf("- %sx: \"%s\" in %s:%s\n", $log['count'], $log['message'], $log['file'], $log['line']);
126+
$remainingCount += $log['count'];
127+
}
128+
129+
$this->write(sprintf("## Remaining deprecations (%s)\n\n", $remainingCount));
130+
foreach ($formattedLogs as $formattedLog) {
131+
$this->write($formattedLog);
132+
}
110133
}
111134

112135
protected function describeContainerServices(ContainerBuilder $builder, array $options = [])

Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ protected function describeContainerDeprecations(ContainerBuilder $builder, arra
372372
$formattedLogs = [];
373373
$remainingCount = 0;
374374
foreach ($logs as $log) {
375-
$formattedLogs[] = sprintf("%sx: %s \n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
375+
$formattedLogs[] = sprintf("%sx: %s\n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
376376
$remainingCount += $log['count'];
377377
}
378378
$options['output']->title(sprintf('Remaining deprecations (%s)', $remainingCount));

Console/Descriptor/XmlDescriptor.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;
1313

1414
use Symfony\Component\Console\Exception\LogicException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\DependencyInjection\Alias;
1617
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1718
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
@@ -108,7 +109,30 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
108109

109110
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
110111
{
111-
throw new LogicException('Using the XML format to print the deprecations is not supported.');
112+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
113+
if (!file_exists($containerDeprecationFilePath)) {
114+
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
115+
}
116+
117+
$logs = unserialize(file_get_contents($containerDeprecationFilePath));
118+
119+
$dom = new \DOMDocument('1.0', 'UTF-8');
120+
$dom->appendChild($deprecationsXML = $dom->createElement('deprecations'));
121+
122+
$formattedLogs = [];
123+
$remainingCount = 0;
124+
foreach ($logs as $log) {
125+
$deprecationsXML->appendChild($deprecationXML = $dom->createElement('deprecation'));
126+
$deprecationXML->setAttribute('count', $log['count']);
127+
$deprecationXML->appendChild($dom->createElement('message', $log['message']));
128+
$deprecationXML->appendChild($dom->createElement('file', $log['file']));
129+
$deprecationXML->appendChild($dom->createElement('line', $log['line']));
130+
$remainingCount += $log['count'];
131+
}
132+
133+
$deprecationsXML->setAttribute('remainingCount', $remainingCount);
134+
135+
$this->writeDocument($dom);
112136
}
113137

114138
private function writeDocument(\DOMDocument $dom)

Tests/Console/Descriptor/AbstractDescriptorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ public function getClassDescriptionTestData()
211211
];
212212
}
213213

214+
/**
215+
* @dataProvider getDeprecationsTestData
216+
*/
217+
public function testGetDeprecations(ContainerBuilder $builder, $expectedDescription)
218+
{
219+
$this->assertDescription($expectedDescription, $builder, ['deprecations' => true]);
220+
}
221+
222+
public function getDeprecationsTestData()
223+
{
224+
return $this->getDescriptionTestData(ObjectsProvider::getContainerDeprecations());
225+
}
226+
214227
abstract protected function getDescriptor();
215228

216229
abstract protected function getFormat();

Tests/Console/Descriptor/ObjectsProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public static function getContainerParameter()
8888
];
8989
}
9090

91+
public static function getContainerDeprecations()
92+
{
93+
$builderWithDeprecations = new ContainerBuilder();
94+
$builderWithDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
95+
$builderWithDeprecations->setParameter('kernel.container_class', 'KernelContainerWith');
96+
97+
$builderWithoutDeprecations = new ContainerBuilder();
98+
$builderWithoutDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
99+
$builderWithoutDeprecations->setParameter('kernel.container_class', 'KernelContainerWithout');
100+
101+
return [
102+
'deprecations' => $builderWithDeprecations,
103+
'deprecations_empty' => $builderWithoutDeprecations,
104+
];
105+
}
106+
91107
public static function getContainerBuilders()
92108
{
93109
$builder1 = new ContainerBuilder();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a:2:{i:0;a:6:{s:4:"type";i:16384;s:7:"message";s:25:"Some deprecation message.";s:4:"file";s:22:"/path/to/some/file.php";s:4:"line";i:39;s:5:"trace";a:0:{}s:5:"count";i:3;}i:1;a:6:{s:4:"type";i:16384;s:7:"message";s:29:"An other deprecation message.";s:4:"file";s:26:"/path/to/an/other/file.php";s:4:"line";i:25;s:5:"trace";a:0:{}s:5:"count";i:2;}}

0 commit comments

Comments
 (0)