3939 */
4040class TranslationDebugCommand extends Command
4141{
42+ const DISPLAY_MESSAGE_MISSING = 'missing ' ;
43+ const DISPLAY_MESSAGE_UNUSED = 'unused ' ;
44+ const DISPLAY_MESSAGE_FALLBACK = 'fallback ' ;
4245 const MESSAGE_MISSING = 0 ;
4346 const MESSAGE_UNUSED = 1 ;
4447 const MESSAGE_EQUALS_FALLBACK = 2 ;
@@ -85,6 +88,7 @@ protected function configure()
8588 new InputOption ('only-missing ' , null , InputOption::VALUE_NONE , 'Displays only missing messages ' ),
8689 new InputOption ('only-unused ' , null , InputOption::VALUE_NONE , 'Displays only unused messages ' ),
8790 new InputOption ('all ' , null , InputOption::VALUE_NONE , 'Load messages from all registered bundles ' ),
91+ new InputOption ('strict ' , null , InputOption::VALUE_OPTIONAL , 'Returns a non-zero exit code upon failure ' , false ),
8892 ])
8993 ->setDescription ('Displays translation messages information ' )
9094 ->setHelp (<<<'EOF'
@@ -130,6 +134,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
130134
131135 $ locale = $ input ->getArgument ('locale ' );
132136 $ domain = $ input ->getOption ('domain ' );
137+
138+ $ strictOption = $ input ->getOption ('strict ' );
139+ $ strictnessLevel = $ this ->getStrictnessLevel ($ strictOption );
140+ $ exitCode = 0 ;
141+
133142 /** @var KernelInterface $kernel */
134143 $ kernel = $ this ->getApplication ()->getKernel ();
135144 $ rootDir = $ kernel ->getContainer ()->getParameter ('kernel.root_dir ' );
@@ -244,7 +253,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
244253
245254 $ io ->getErrorStyle ()->warning ($ outputMessage );
246255
247- return ;
256+ if (null !== $ strictnessLevel ) {
257+ $ exitCode = 1 ;
258+ }
259+
260+ return $ exitCode ;
248261 }
249262
250263 // Load the fallback catalogues
@@ -265,9 +278,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
265278 if ($ extractedCatalogue ->defines ($ messageId , $ domain )) {
266279 if (!$ currentCatalogue ->defines ($ messageId , $ domain )) {
267280 $ states [] = self ::MESSAGE_MISSING ;
281+
282+ if (null !== $ strictnessLevel && $ strictnessLevel <= self ::MESSAGE_MISSING ) {
283+ $ exitCode = $ exitCode | 1 ;
284+ }
268285 }
269286 } elseif ($ currentCatalogue ->defines ($ messageId , $ domain )) {
270287 $ states [] = self ::MESSAGE_UNUSED ;
288+
289+ if (null !== $ strictnessLevel && $ strictnessLevel <= self ::MESSAGE_UNUSED ) {
290+ $ exitCode = $ exitCode | 1 ;
291+ }
271292 }
272293
273294 if (!\in_array (self ::MESSAGE_UNUSED , $ states ) && true === $ input ->getOption ('only-unused ' )
@@ -279,6 +300,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
279300 if ($ fallbackCatalogue ->defines ($ messageId , $ domain ) && $ value === $ fallbackCatalogue ->get ($ messageId , $ domain )) {
280301 $ states [] = self ::MESSAGE_EQUALS_FALLBACK ;
281302
303+ if (null !== $ strictnessLevel && $ strictnessLevel <= self ::MESSAGE_EQUALS_FALLBACK ) {
304+ $ exitCode = $ exitCode | 1 ;
305+ }
306+
282307 break ;
283308 }
284309 }
@@ -293,20 +318,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
293318 }
294319
295320 $ io ->table ($ headers , $ rows );
321+
322+ return $ exitCode ;
296323 }
297324
298325 private function formatState ($ state ): string
299326 {
300327 if (self ::MESSAGE_MISSING === $ state ) {
301- return '<error> missing </error> ' ;
328+ return '<error> ' . self :: DISPLAY_MESSAGE_MISSING . ' </error> ' ;
302329 }
303330
304331 if (self ::MESSAGE_UNUSED === $ state ) {
305- return '<comment> unused </comment> ' ;
332+ return '<comment> ' . self :: DISPLAY_MESSAGE_UNUSED . ' </comment> ' ;
306333 }
307334
308335 if (self ::MESSAGE_EQUALS_FALLBACK === $ state ) {
309- return '<info> fallback </info> ' ;
336+ return '<info> ' . self :: DISPLAY_MESSAGE_FALLBACK . ' </info> ' ;
310337 }
311338
312339 return $ state ;
@@ -390,4 +417,25 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
390417
391418 return $ fallbackCatalogues ;
392419 }
420+
421+ /**
422+ * @param string|false $strictArg
423+ *
424+ * @return int|false|null strictness level, using MESSAGE_* values
425+ */
426+ private function getStrictnessLevel ($ strictArg )
427+ {
428+ switch ($ strictArg ) {
429+ case self ::DISPLAY_MESSAGE_FALLBACK :
430+ return self ::MESSAGE_EQUALS_FALLBACK ;
431+ case self ::DISPLAY_MESSAGE_UNUSED :
432+ return self ::MESSAGE_UNUSED ;
433+ case self ::DISPLAY_MESSAGE_MISSING :
434+ return self ::MESSAGE_MISSING ;
435+ case null :
436+ return null ;
437+ default :
438+ return false ;
439+ }
440+ }
393441}
0 commit comments