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 ;
23
+ use Symfony \Component \DependencyInjection \Container ;
21
24
use Symfony \Component \DependencyInjection \ContainerBuilder ;
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,13 +51,18 @@ 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 ();
51
56
52
- $ container ->setParameter ('container.build_hash ' , 'lint_container ' );
53
- $ container ->setParameter ('container.build_time ' , time ());
54
- $ container ->setParameter ('container.build_id ' , 'lint_container ' );
57
+ try {
58
+ $ container = $ this ->getContainerBuilder ();
59
+ } catch (RuntimeException $ e ) {
60
+ $ errorIo ->error ($ e ->getMessage ());
61
+
62
+ return 2 ;
63
+ }
55
64
56
- $ container ->addCompilerPass ( new CheckTypeDeclarationsPass ( true ), PassConfig:: TYPE_AFTER_REMOVING , - 100 );
65
+ $ container ->setParameter ( ' container.build_time ' , time () );
57
66
58
67
$ container ->compile ();
59
68
@@ -67,22 +76,44 @@ private function getContainerBuilder(): ContainerBuilder
67
76
}
68
77
69
78
$ kernel = $ this ->getApplication ()->getKernel ();
79
+ $ kernelContainer = $ kernel ->getContainer ();
80
+
81
+ if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernelContainer ->getParameter ('debug.container.dump ' ), true ))->isFresh ()) {
82
+ if (!$ kernel instanceof Kernel) {
83
+ throw new RuntimeException ("This command does not support the console application's kernel. " );
84
+ }
70
85
71
- if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ), true ))->isFresh ()) {
72
86
$ buildContainer = \Closure::bind (function (): ContainerBuilder {
73
87
$ this ->initializeBundles ();
74
88
75
89
return $ this ->buildContainer ();
76
90
}, $ kernel , \get_class ($ kernel ));
77
91
$ container = $ buildContainer ();
92
+
93
+ $ skippedIds = [];
78
94
} else {
79
- (new XmlFileLoader ($ container = new ContainerBuilder ($ parameterBag = new EnvPlaceholderParameterBag ()), new FileLocator ()))->load ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ));
95
+ if (!$ kernelContainer instanceof Container) {
96
+ throw new RuntimeException ("This command does not support the console application kernel's container. " );
97
+ }
98
+
99
+ (new XmlFileLoader ($ container = new ContainerBuilder ($ parameterBag = new EnvPlaceholderParameterBag ()), new FileLocator ()))->load ($ kernelContainer ->getParameter ('debug.container.dump ' ));
80
100
81
101
$ refl = new \ReflectionProperty ($ parameterBag , 'resolved ' );
82
102
$ refl ->setAccessible (true );
83
103
$ refl ->setValue ($ parameterBag , true );
104
+
105
+ $ passConfig = $ container ->getCompilerPassConfig ();
106
+ $ passConfig ->setRemovingPasses ([]);
107
+ $ passConfig ->setAfterRemovingPasses ([]);
108
+
109
+ $ skippedIds = $ kernelContainer ->getRemovedIds ();
84
110
}
85
111
112
+ $ container ->setParameter ('container.build_hash ' , 'lint_container ' );
113
+ $ container ->setParameter ('container.build_id ' , 'lint_container ' );
114
+
115
+ $ container ->addCompilerPass (new CheckTypeDeclarationsPass (true , $ skippedIds ), PassConfig::TYPE_AFTER_REMOVING , -100 );
116
+
86
117
return $ this ->containerBuilder = $ container ;
87
118
}
88
119
}
0 commit comments