11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Command ;
13
13
14
+ use Symfony \Component \Config \ConfigCache ;
15
+ use Symfony \Component \Config \FileLocator ;
14
16
use Symfony \Component \Console \Command \Command ;
15
17
use Symfony \Component \Console \Input \InputInterface ;
16
18
use Symfony \Component \Console \Input \InputOption ;
17
19
use Symfony \Component \Console \Output \OutputInterface ;
18
- use Symfony \Component \Config \ConfigCache ;
19
- use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
20
- use Symfony \Component \DependencyInjection \ContainerBuilder ;
21
- use Symfony \Component \DependencyInjection \Compiler \CheckTypeHintsPass ;
20
+ use Symfony \Component \DependencyInjection \Compiler \CheckTypeDeclarationsPass ;
22
21
use Symfony \Component \DependencyInjection \Compiler \PassConfig ;
23
- use Symfony \Component \Config \FileLocator ;
22
+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
23
+ use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
24
24
25
- class ContainerLintCommand extends Command
25
+ final class ContainerLintCommand extends Command
26
26
{
27
+ protected static $ defaultName = 'lint:container ' ;
28
+
27
29
/**
28
30
* @var ContainerBuilder
29
31
*/
@@ -35,37 +37,33 @@ class ContainerLintCommand extends Command
35
37
protected function configure ()
36
38
{
37
39
$ this
38
- ->setDescription ('Lints container for services arguments type hints ' )
39
- ->setHelp ('This command will parse all your defined services and check that you are injecting service without type error based on type hints . ' )
40
- ->addOption ('only-used -services ' , 'o ' , InputOption::VALUE_NONE , 'Check only services that are used in your application ' )
40
+ ->setDescription ('Ensures that arguments injected into services match type declarations ' )
41
+ ->setHelp ('This command parses service definitions and ensures that injected values match the type declarations of each services \' class . ' )
42
+ ->addOption ('ignore-unused -services ' , 'o ' , InputOption::VALUE_NONE , 'Ignore unused services ' )
41
43
;
42
44
}
43
45
44
46
/**
45
47
* {@inheritdoc}
46
48
*/
47
- protected function execute (InputInterface $ input , OutputInterface $ output )
49
+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
48
50
{
49
51
$ container = $ this ->getContainerBuilder ();
50
52
51
53
$ container ->setParameter ('container.build_id ' , 'lint_container ' );
52
54
53
55
$ container ->addCompilerPass (
54
- new CheckTypeHintsPass (),
55
- $ input ->getOption ('only-used-services ' ) ? PassConfig::TYPE_AFTER_REMOVING : PassConfig::TYPE_BEFORE_OPTIMIZATION
56
+ new CheckTypeDeclarationsPass (true ),
57
+ $ input ->getOption ('ignore-unused-services ' ) ? PassConfig::TYPE_AFTER_REMOVING : PassConfig::TYPE_OPTIMIZE ,
58
+ -5
56
59
);
57
60
58
61
$ container ->compile ();
62
+
63
+ return 0 ;
59
64
}
60
65
61
- /**
62
- * Loads the ContainerBuilder from the cache.
63
- *
64
- * @return ContainerBuilder
65
- *
66
- * @throws \LogicException
67
- */
68
- protected function getContainerBuilder ()
66
+ private function getContainerBuilder (): ContainerBuilder
69
67
{
70
68
if ($ this ->containerBuilder ) {
71
69
return $ this ->containerBuilder ;
@@ -74,10 +72,9 @@ protected function getContainerBuilder()
74
72
$ kernel = $ this ->getApplication ()->getKernel ();
75
73
76
74
if (!$ kernel ->isDebug () || !(new ConfigCache ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ), true ))->isFresh ()) {
77
- $ buildContainer = \Closure::bind (function () { return $ this ->buildContainer (); }, $ kernel , get_class ($ kernel ));
75
+ $ buildContainer = \Closure::bind (function () { return $ this ->buildContainer (); }, $ kernel , \ get_class ($ kernel ));
78
76
$ container = $ buildContainer ();
79
- $ container ->getCompilerPassConfig ()->setRemovingPasses (array ());
80
- $ container ->compile ();
77
+ $ container ->getCompilerPassConfig ()->setRemovingPasses ([]);
81
78
} else {
82
79
(new XmlFileLoader ($ container = new ContainerBuilder (), new FileLocator ()))->load ($ kernel ->getContainer ()->getParameter ('debug.container.dump ' ));
83
80
}
0 commit comments