14
14
use Symfony \Component \Config \ConfigCache ;
15
15
use Symfony \Component \Config \FileLocator ;
16
16
use Symfony \Component \Console \Command \Command ;
17
+ use Symfony \Component \Console \Exception \RuntimeException ;
17
18
use Symfony \Component \Console \Input \InputInterface ;
18
19
use Symfony \Component \Console \Output \OutputInterface ;
20
+ use Symfony \Component \Console \Style \SymfonyStyle ;
19
21
use Symfony \Component \DependencyInjection \Compiler \CheckTypeDeclarationsPass ;
20
22
use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
21
23
use Symfony \Component \DependencyInjection \ContainerBuilder ;
24
+ use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
22
25
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
23
26
use Symfony \Component \DependencyInjection \ParameterBag \EnvPlaceholderParameterBag ;
27
+ use Symfony \Component \HttpKernel \Kernel ;
24
28
25
29
final class ContainerLintCommand extends Command
26
30
{
@@ -47,15 +51,32 @@ protected function configure()
47
51
*/
48
52
protected function execute (InputInterface $ input , OutputInterface $ output ): int
49
53
{
50
- $ container = $ this ->getContainerBuilder ();
54
+ $ io = new SymfonyStyle ($ input , $ output );
55
+ $ errorIo = $ io ->getErrorStyle ();
56
+
57
+ try {
58
+ $ container = $ this ->getContainerBuilder ();
59
+ } catch (RuntimeException $ e ) {
60
+ $ errorIo ->error ($ e ->getMessage ());
61
+
62
+ return 2 ;
63
+ }
51
64
52
65
$ container ->setParameter ('container.build_hash ' , 'lint_container ' );
53
66
$ container ->setParameter ('container.build_time ' , time ());
54
67
$ container ->setParameter ('container.build_id ' , 'lint_container ' );
55
68
56
69
$ container ->addCompilerPass (new CheckTypeDeclarationsPass (true ), PassConfig::TYPE_AFTER_REMOVING , -100 );
57
70
58
- $ container ->compile ();
71
+ try {
72
+ $ container ->compile ();
73
+ } catch (InvalidArgumentException $ e ) {
74
+ $ errorIo ->error ($ e ->getMessage ());
75
+
76
+ return 1 ;
77
+ }
78
+
79
+ $ errorIo ->success ('The injected values in the services seem to be valid. ' );
59
80
60
81
return 0 ;
61
82
}
@@ -69,6 +90,10 @@ private function getContainerBuilder(): ContainerBuilder
69
90
$ kernel = $ this ->getApplication ()->getKernel ();
70
91
71
92
if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ), true ))->isFresh ()) {
93
+ if (!$ kernel instanceof Kernel) {
94
+ throw new RuntimeException ('This command does not support the application kernel. ' );
95
+ }
96
+
72
97
$ buildContainer = \Closure::bind (function (): ContainerBuilder {
73
98
$ this ->initializeBundles ();
74
99
0 commit comments