1414use Symfony \Component \Config \ConfigCache ;
1515use Symfony \Component \Config \FileLocator ;
1616use Symfony \Component \Console \Command \Command ;
17+ use Symfony \Component \Console \Exception \RuntimeException ;
1718use Symfony \Component \Console \Input \InputInterface ;
1819use Symfony \Component \Console \Output \OutputInterface ;
20+ use Symfony \Component \Console \Style \SymfonyStyle ;
1921use Symfony \Component \DependencyInjection \Compiler \CheckTypeDeclarationsPass ;
2022use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
23+ use Symfony \Component \DependencyInjection \Container ;
2124use Symfony \Component \DependencyInjection \ContainerBuilder ;
2225use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
2326use Symfony \Component \DependencyInjection \ParameterBag \EnvPlaceholderParameterBag ;
27+ use Symfony \Component \HttpKernel \Kernel ;
2428
2529final class ContainerLintCommandextends Command
2630{
@@ -47,13 +51,18 @@ protected function configure()
4751 */
4852protected function execute (InputInterface $ input ,OutputInterface $ output ):int
4953 {
50- $ container =$ this ->getContainerBuilder ();
54+ $ io =new SymfonyStyle ($ input ,$ output );
55+ $ errorIo =$ io ->getErrorStyle ();
5156
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+ }
5564
56- $ container ->addCompilerPass ( new CheckTypeDeclarationsPass ( true ), PassConfig:: TYPE_AFTER_REMOVING , - 100 );
65+ $ container ->setParameter ( ' container.build_time ' , time () );
5766
5867$ container ->compile ();
5968
@@ -67,22 +76,44 @@ private function getContainerBuilder(): ContainerBuilder
6776 }
6877
6978$ kernel =$ this ->getApplication ()->getKernel ();
79+ $ kernelContainer =$ kernel ->getContainer ();
80+
81+ if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernelContainer ->getParameter ('debug.container.dump ' ),true ))->isFresh ()) {
82+ if (!$ kernelinstanceof Kernel) {
83+ throw new RuntimeException ("This command does not support the console application's kernel. " );
84+ }
7085
71- if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ),true ))->isFresh ()) {
7286$ buildContainer = \Closure::bind (function ():ContainerBuilder {
7387$ this ->initializeBundles ();
7488
7589return $ this ->buildContainer ();
7690 },$ kernel ,\get_class ($ kernel ));
7791$ container =$ buildContainer ();
92+
93+ $ skippedIds = [];
7894 }else {
79- (new XmlFileLoader ($ container =new ContainerBuilder ($ parameterBag =new EnvPlaceholderParameterBag ()),new FileLocator ()))->load ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ));
95+ if (!$ kernelContainerinstanceof 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 ' ));
80100
81101$ refl =new \ReflectionProperty ($ parameterBag ,'resolved ' );
82102$ refl ->setAccessible (true );
83103$ refl ->setValue ($ parameterBag ,true );
104+
105+ $ passConfig =$ container ->getCompilerPassConfig ();
106+ $ passConfig ->setRemovingPasses ([]);
107+ $ passConfig ->setAfterRemovingPasses ([]);
108+
109+ $ skippedIds =$ kernelContainer ->getRemovedIds ();
84110 }
85111
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+
86117return $ this ->containerBuilder =$ container ;
87118 }
88119}