@@ -44,6 +44,8 @@ protected function configure()
44
44
->setDefinition (array (
45
45
new InputArgument ('name ' , InputArgument::OPTIONAL , 'A service name (foo) ' ),
46
46
new InputOption ('show-private ' , null , InputOption::VALUE_NONE , 'Use to show public *and* private services ' ),
47
+ new InputOption ('tag ' , null , InputOption::VALUE_REQUIRED , 'Show all services with a specific tag ' ),
48
+ new InputOption ('tags ' , null , InputOption::VALUE_NONE , 'Displays tagged services for an application ' )
47
49
))
48
50
->setDescription ('Displays current services for an application ' )
49
51
->setHelp (<<<EOF
@@ -59,6 +61,14 @@ protected function configure()
59
61
using the --show-private flag:
60
62
61
63
<info>php %command.full_name% --show-private</info>
64
+
65
+ Use the --tags option to display tagged <comment>public</comment> services grouped by tag:
66
+
67
+ <info>php %command.full_name% --tags</info>
68
+
69
+ Find all services with a specific tag by specifying the tag name with the --tag option:
70
+
71
+ <info>php %command.full_name% --tag=form.type</info>
62
72
EOF
63
73
)
64
74
;
@@ -72,26 +82,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
72
82
$ name = $ input ->getArgument ('name ' );
73
83
74
84
$ this ->containerBuilder = $ this ->getContainerBuilder ();
75
- $ serviceIds = $ this ->containerBuilder ->getServiceIds ();
85
+
86
+ if ($ input ->getOption ('tags ' )) {
87
+ $ this ->outputTags ($ output , $ input ->getOption ('show-private ' ));
88
+ return ;
89
+ }
90
+
91
+ $ tag = $ input ->getOption ('tag ' );
92
+ if (null !== $ tag ) {
93
+ $ serviceIds = array_keys ($ this ->containerBuilder ->findTaggedServiceIds ($ tag ));
94
+ } else {
95
+ $ serviceIds = $ this ->containerBuilder ->getServiceIds ();
96
+ }
76
97
77
98
// sort so that it reads like an index of services
78
99
asort ($ serviceIds );
79
100
80
101
if ($ name ) {
81
102
$ this ->outputService ($ output , $ name );
82
103
} else {
83
- $ this ->outputServices ($ output , $ serviceIds , $ input ->getOption ('show-private ' ));
104
+ $ this ->outputServices ($ output , $ serviceIds , $ input ->getOption ('show-private ' ), $ tag );
84
105
}
85
106
}
86
107
87
- protected function outputServices (OutputInterface $ output , $ serviceIds , $ showPrivate = false )
108
+ protected function outputServices (OutputInterface $ output , $ serviceIds , $ showPrivate = false , $ showTagAttributes = null )
88
109
{
89
110
// set the label to specify public or public+private
90
111
if ($ showPrivate ) {
91
112
$ label = '<comment>Public</comment> and <comment>private</comment> services ' ;
92
113
} else {
93
114
$ label = '<comment>Public</comment> services ' ;
94
115
}
116
+ if ($ showTagAttributes ) {
117
+ $ label .= ' with tag <info> ' .$ showTagAttributes .'</info> ' ;
118
+ }
95
119
96
120
$ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('container ' , $ label ));
97
121
@@ -136,6 +160,23 @@ protected function outputServices(OutputInterface $output, $serviceIds, $showPri
136
160
$ service = $ definition ;
137
161
$ output ->writeln (sprintf ($ format , $ serviceId , '' , get_class ($ service )));
138
162
}
163
+
164
+ if (null !== $ showTagAttributes ) {
165
+ $ tags = $ definition ->getTag ($ showTagAttributes );
166
+ foreach ($ tags as $ tag ) {
167
+ $ output ->write (' <comment> ' .$ showTagAttributes .' tag attributes</comment>: ' );
168
+ if (count ($ tag )) {
169
+ $ arguments = array ();
170
+ foreach ($ tag as $ key => $ value ) {
171
+ $ arguments [] = $ key ;
172
+ $ arguments [] = $ value ;
173
+ }
174
+ $ output ->writeln (vsprintf (implode (", " , array_fill (0 , count ($ tag ), '<info>%s</info>: %s ' )), $ arguments ));
175
+ } else {
176
+ $ output ->writeln ('none ' );
177
+ }
178
+ }
179
+ }
139
180
}
140
181
}
141
182
@@ -223,4 +264,45 @@ protected function resolveServiceDefinition($serviceId)
223
264
// the service has been injected in some special way, just return the service
224
265
return $ this ->containerBuilder ->get ($ serviceId );
225
266
}
267
+
268
+ /**
269
+ * Renders list of tagged services grouped by tag
270
+ *
271
+ * @param OutputInterface $output
272
+ * @param bool $showPrivate
273
+ */
274
+ protected function outputTags (OutputInterface $ output , $ showPrivate = false )
275
+ {
276
+ $ tags = $ this ->containerBuilder ->findTags ();
277
+ asort ($ tags );
278
+
279
+ $ label = 'Tagged services ' ;
280
+ $ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('container ' , $ label ));
281
+
282
+ foreach ($ tags as $ tag ) {
283
+ $ serviceIds = $ this ->containerBuilder ->findTaggedServiceIds ($ tag );
284
+
285
+ foreach ($ serviceIds as $ serviceId => $ attributes ) {
286
+ $ definition = $ this ->resolveServiceDefinition ($ serviceId );
287
+ if ($ definition instanceof Definition) {
288
+ if (!$ showPrivate && !$ definition ->isPublic ()) {
289
+ unset($ serviceIds [$ serviceId ]);
290
+ continue ;
291
+ }
292
+ }
293
+ }
294
+
295
+ if (count ($ serviceIds ) === 0 ) {
296
+ continue ;
297
+ }
298
+
299
+ $ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('tag ' , $ tag ));
300
+
301
+ foreach ($ serviceIds as $ serviceId => $ attributes ) {
302
+ $ output ->writeln ($ serviceId );
303
+ }
304
+
305
+ $ output ->writeln ('' );
306
+ }
307
+ }
226
308
}
0 commit comments