12
12
namespace Symfony \Bridge \Twig \Command ;
13
13
14
14
use Symfony \Component \Console \Command \Command ;
15
+ use Symfony \Component \Console \Formatter \OutputFormatter ;
15
16
use Symfony \Component \Console \Input \InputArgument ;
16
17
use Symfony \Component \Console \Input \InputInterface ;
17
18
use Symfony \Component \Console \Input \InputOption ;
@@ -100,6 +101,7 @@ protected function configure()
100
101
protected function execute (InputInterface $ input , OutputInterface $ output )
101
102
{
102
103
$ io = new SymfonyStyle ($ input , $ output );
104
+ $ decorated = $ io ->isDecorated ();
103
105
104
106
// BC to be removed in 4.0
105
107
if (__CLASS__ !== \get_class ($ this )) {
@@ -114,29 +116,35 @@ protected function execute(InputInterface $input, OutputInterface $output)
114
116
throw new \RuntimeException ('The Twig environment needs to be set. ' );
115
117
}
116
118
119
+ $ filter = $ input ->getArgument ('filter ' );
117
120
$ types = ['functions ' , 'filters ' , 'tests ' , 'globals ' ];
118
121
119
122
if ('json ' === $ input ->getOption ('format ' )) {
120
123
$ data = [];
121
124
foreach ($ types as $ type ) {
122
125
foreach ($ this ->twig ->{'get ' .ucfirst ($ type )}() as $ name => $ entity ) {
123
- $ data [$ type ][$ name ] = $ this ->getMetadata ($ type , $ entity );
126
+ if (!$ filter || false !== strpos ($ name , $ filter )) {
127
+ $ data [$ type ][$ name ] = $ this ->getMetadata ($ type , $ entity );
128
+ }
124
129
}
125
130
}
126
- $ data ['tests ' ] = array_keys ($ data ['tests ' ]);
131
+
132
+ if (isset ($ data ['tests ' ])) {
133
+ $ data ['tests ' ] = array_keys ($ data ['tests ' ]);
134
+ }
135
+
127
136
$ data ['loader_paths ' ] = $ this ->getLoaderPaths ();
128
- $ io ->writeln (json_encode ($ data ));
137
+ $ data = json_encode ($ data , JSON_PRETTY_PRINT );
138
+ $ io ->writeln ($ decorated ? OutputFormatter::escape ($ data ) : $ data );
129
139
130
140
return 0 ;
131
141
}
132
142
133
- $ filter = $ input ->getArgument ('filter ' );
134
-
135
143
foreach ($ types as $ index => $ type ) {
136
144
$ items = [];
137
145
foreach ($ this ->twig ->{'get ' .ucfirst ($ type )}() as $ name => $ entity ) {
138
146
if (!$ filter || false !== strpos ($ name , $ filter )) {
139
- $ items [$ name ] = $ name .$ this ->getPrettyMetadata ($ type , $ entity );
147
+ $ items [$ name ] = $ name .$ this ->getPrettyMetadata ($ type , $ entity, $ decorated );
140
148
}
141
149
}
142
150
@@ -262,7 +270,7 @@ private function getMetadata($type, $entity)
262
270
}
263
271
}
264
272
265
- private function getPrettyMetadata ($ type , $ entity )
273
+ private function getPrettyMetadata ($ type , $ entity, $ decorated )
266
274
{
267
275
if ('tests ' === $ type ) {
268
276
return '' ;
@@ -274,15 +282,17 @@ private function getPrettyMetadata($type, $entity)
274
282
return '(unknown?) ' ;
275
283
}
276
284
} catch (\UnexpectedValueException $ e ) {
277
- return ' <error> ' . $ e ->getMessage (). ' </error> ' ;
285
+ return sprintf ( ' <error>%s</error> ' , $ decorated ? OutputFormatter:: escape ( $ e ->getMessage ()) : $ e -> getMessage ()) ;
278
286
}
279
287
280
288
if ('globals ' === $ type ) {
281
289
if (\is_object ($ meta )) {
282
290
return ' = object( ' .\get_class ($ meta ).') ' ;
283
291
}
284
292
285
- return ' = ' .substr (@json_encode ($ meta ), 0 , 50 );
293
+ $ description = substr (@json_encode ($ meta ), 0 , 50 );
294
+
295
+ return sprintf (' = %s ' , $ decorated ? OutputFormatter::escape ($ description ) : $ description );
286
296
}
287
297
288
298
if ('functions ' === $ type ) {
0 commit comments