Skip to content

Commit c27ad7a

Browse files
committed
[FrameworkBundle] add debug:deprecations:warmed command
1 parent 25f1804 commit c27ad7a

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`
1111
* Added support for configuring chained cache pools
1212
* Deprecated booting the kernel before running `WebTestCase::createClient()`
13+
* Added the `DebugDeprecationsWarmedCommand` to show the deprecation in CLI.
1314

1415
4.3.0
1516
-----
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
18+
19+
/**
20+
* Show the deprecations generated during the cache warmup.
21+
*
22+
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
23+
*
24+
* @final
25+
*/
26+
class DebugDeprecationsWarmedCommand extends Command
27+
{
28+
protected static $defaultName = 'debug:deprecations:warmed';
29+
30+
private $containerDeprecationFilePath;
31+
32+
public function __construct(string $containerDeprecationFilePath)
33+
{
34+
parent::__construct();
35+
36+
$this->containerDeprecationFilePath = $containerDeprecationFilePath;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setDescription('Show the deprecated calls generated during cache:warmup')
46+
->setHelp(<<<'EOF'
47+
The <info>%command.name%</info> shows depecrated calls generated during cache:warmup
48+
49+
<info>php %command.full_name%</info>
50+
EOF
51+
)
52+
;
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
protected function execute(InputInterface $input, OutputInterface $output)
59+
{
60+
$io = new SymfonyStyle($input, $output);
61+
if (!file_exists($this->containerDeprecationFilePath)) {
62+
$io->warning('The deprecation file does not exist, please try warming the cache before.');
63+
64+
return;
65+
}
66+
67+
$logs = unserialize(file_get_contents($this->containerDeprecationFilePath));
68+
if (0 === \count($logs)) {
69+
$io->success('There are no deprecations in the logs!');
70+
71+
return;
72+
}
73+
74+
$formattedLogs = [];
75+
$remainingCount = 0;
76+
foreach ($logs as $log) {
77+
$formattedLogs[] = sprintf("%sx: %s \n in %s:%s", $log['count'], $log['message'], $log['file'], $log['line']);
78+
$remainingCount += $log['count'];
79+
}
80+
$io->title(sprintf('Remaining warmup deprecations (%s)', $remainingCount));
81+
$io->listing($formattedLogs);
82+
}
83+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
<tag name="console.command" command="debug:event-dispatcher" />
8282
</service>
8383

84+
<service id="console.command.debug_deprecation_warmed" class="Symfony\Bundle\FrameworkBundle\Command\DebugDeprecationsWarmedCommand">
85+
<argument>%kernel.cache_dir%/%kernel.container_class%Deprecations.log</argument>
86+
<tag name="console.command" command="debug:deprecations:warmed" />
87+
</service>
88+
8489
<service id="console.command.messenger_consume_messages" class="Symfony\Component\Messenger\Command\ConsumeMessagesCommand">
8590
<argument type="service" id="messenger.routable_message_bus" />
8691
<argument type="service" id="messenger.receiver_locator" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\DebugDeprecationsWarmedCommand;
15+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
class DebugDeprecationsWarmedCommandTest extends TestCase
20+
{
21+
public function testGetDeprecation()
22+
{
23+
$path = tempnam(sys_get_temp_dir(), 'sf');
24+
touch($path);
25+
file_put_contents($path, 'a:2:{i:0;a:6:{s:4:"type";i:16384;s:7:"message";s:171:"The "Symfony\Bundle\FrameworkBundle\Controller\Controller" class is deprecated since Symfony 4.2, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.";s:4:"file";s:87:"/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php";s:4:"line";i:17;s:5:"trace";a:1:{i:0;a:3:{s:4:"file";s:66:"/home/hamza/projet/contrib/sf/src/Controller/DefaultController.php";s:4:"line";i:9;s:8:"function";s:17:"spl_autoload_call";}}s:5:"count";i:1;}i:1;a:6:{s:4:"type";i:16384;s:7:"message";s:188:"The "App\Controller\DefaultController" class extends "Symfony\Bundle\FrameworkBundle\Controller\Controller" that is deprecated since Symfony 4.2, use {@see AbstractController} instead.
26+
*.";s:4:"file";s:71:"/home/hamza/projet/contrib/sf/vendor/symfony/debug/DebugClassLoader.php";s:4:"line";i:191;s:5:"trace";a:1:{i:0;a:1:{s:8:"function";s:17:"spl_autoload_call";}}s:5:"count";i:1;}}');
27+
28+
/** @var CommandTester $tester */
29+
$tester = $this->getCommandTester($path);
30+
$tester->execute([]);
31+
32+
$this->assertSame(0, $tester->getStatusCode());
33+
$this->assertContains('Symfony\Bundle\FrameworkBundle\Controller\Controller', $tester->getDisplay());
34+
$this->assertContains('/home/hamza/projet/contrib/sf/vendor/symfony/framework-bundle/Controller/Controller.php', $tester->getDisplay());
35+
}
36+
37+
public function testGetDeprecationNone()
38+
{
39+
$path = tempnam(sys_get_temp_dir(), 'sf');
40+
touch($path);
41+
file_put_contents($path, 'a:0:{}');
42+
43+
/** @var CommandTester $tester */
44+
$tester = $this->getCommandTester($path);
45+
$tester->execute([]);
46+
47+
$this->assertSame(0, $tester->getStatusCode());
48+
$this->assertContains('[OK] There are no deprecations in the logs!', $tester->getDisplay());
49+
}
50+
51+
public function testGetDeprecationNoFile(): void
52+
{
53+
$tester = $this->getCommandTester('notexist');
54+
$tester->execute([]);
55+
56+
$this->assertSame(0, $tester->getStatusCode());
57+
$this->assertContains('[WARNING] The deprecation file does not exist', $tester->getDisplay());
58+
}
59+
60+
private function getCommandTester($path): CommandTester
61+
{
62+
$command = new DebugDeprecationsWarmedCommand($path);
63+
$application = new Application();
64+
$application->add($command);
65+
66+
return new CommandTester($application->get('debug:deprecations:warmed'));
67+
}
68+
}

0 commit comments

Comments
 (0)