8000 Add show-deprecations option to lint:twig command · tigr1991/symfony@a676c7e · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit a676c7e

Browse files
committed
Add show-deprecations option to lint:twig command
1 parent 38b9a27 commit a676c7e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
`DebugCommand::__construct()` method, swap the variables position.
1010
* the `LintCommand` lints all the templates stored in all configured Twig paths if none argument is provided
1111
* deprecated accepting STDIN implicitly when using the `lint:twig` command, use `lint:twig -` (append a dash) instead to make it explicit.
12+
* added `--show-deprecations` option to the `lint:twig` command
1213

1314
4.3.0
1415
-----

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function configure()
5050
$this
5151
->setDescription('Lints a template and outputs encountered errors')
5252
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
53+
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors')
5354
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
5455
->setHelp(<<<'EOF'
5556
The <info>%command.name%</info> command lints a template and outputs to STDOUT
@@ -77,6 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7778
{
7879
$io = new SymfonyStyle($input, $output);
7980
$filenames = $input->getArgument('filename');
81+
$showDeprecations = $input->getOption('show-deprecations');
8082

8183
if (['-'] === $filenames) {
8284
return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]);
@@ -104,7 +106,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
104106
}
105107
}
106108

107-
$filesInfo = $this->getFilesInfo($filenames);
109+
if ($showDeprecations) {
110+
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
111+
if (E_USER_DEPRECATED === $level) {
112+
$templateLine = 0;
113+
if (preg_match('/ at line (\d+) /', $message, $matches)) {
114+
$templateLine = $matches[1];
115+
}
116+
117+
throw new Error($message, $templateLine);
118+
}
119+
120+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
121+
});
122+
}
123+
124+
try {
125+
$filesInfo = $this->getFilesInfo($filenames);
126+
} finally {
127+
if ($showDeprecations) {
128+
restore_error_handler();
129+
}
130+
}
108131

109132
return $this->display($input, $output, $io, $filesInfo);
110133
}

0 commit comments

Comments
 (0)
0