18
18
use Symfony \Component \Console \Output \OutputInterface ;
19
19
use Symfony \Component \Console \Style \SymfonyStyle ;
20
20
use Twig \Environment ;
21
+ use Twig \Loader \FilesystemLoader ;
21
22
22
23
/**
23
24
* Lists twig functions, filters, globals and tests present in the current project.
@@ -29,11 +30,13 @@ class DebugCommand extends Command
29
30
protected static $ defaultName = 'debug:twig ' ;
30
31
31
32
private $ twig ;
33
+ private $ projectDir ;
32
34
33
35
/**
34
36
* @param Environment $twig
37
+ * @param string|null $projectDir
35
38
*/
36
- public function __construct ($ twig = null )
39
+ public function __construct ($ twig = null , $ projectDir = null )
37
40
{
38
41
if (!$ twig instanceof Environment) {
39
42
@trigger_error (sprintf ('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead. ' , __METHOD__ ), E_USER_DEPRECATED );
@@ -46,6 +49,7 @@ public function __construct($twig = null)
46
49
parent ::__construct ();
47
50
48
51
$ this ->twig = $ twig ;
52
+ $ this ->projectDir = $ projectDir ;
49
53
}
50
54
51
55
public function setTwigEnvironment (Environment $ twig )
@@ -120,6 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
120
124
}
121
125
}
122
126
$ data ['tests ' ] = array_keys ($ data ['tests ' ]);
127
+ $ data ['loader_paths ' ] = $ this ->getLoaderPaths ();
123
128
$ io ->writeln (json_encode ($ data ));
124
129
125
130
return 0 ;
@@ -145,9 +150,54 @@ protected function execute(InputInterface $input, OutputInterface $output)
145
150
$ io ->listing ($ items );
146
151
}
147
152
153
+ $ rows = array ();
154
+ foreach ($ this ->getLoaderPaths () as $ namespace => $ paths ) {
155
+ if (count ($ paths ) > 1 ) {
156
+ $ rows [] = array ('' , '' );
157
+ }
158
+ foreach ($ paths as $ path ) {
159
+ $ rows [] = array ($ namespace , '- ' .$ path );
160
+ $ namespace = '' ;
161
+ }
162
+ if (count ($ paths ) > 1 ) {
163
+ $ rows [] = array ('' , '' );
164
+ }
165
+ }
166
+ array_pop ($ rows );
167
+ $ io ->section ('Loader Paths ' );
168
+ $ io ->table (array ('Namespace ' , 'Paths ' ), $ rows );
169
+
148
170
return 0 ;
149
171
}
150
172
173
+ private function getLoaderPaths ()
174
+ {
175
+ if (!($ loader = $ this ->twig ->getLoader ()) instanceof FilesystemLoader) {
176
+ return array ();
177
+ }
178
+
179
+ $ loaderPaths = array ();
180
+ foreach ($ loader ->getNamespaces () as $ namespace ) {
181
+ $ paths = array_map (function ($ path ) use ($ namespace ) {
182
+ if (null !== $ this ->projectDir && 0 === strpos ($ path , $ this ->projectDir )) {
183
+ $ path = ltrim (substr ($ path , strlen ($ this ->projectDir )), DIRECTORY_SEPARATOR );
184
+ }
185
+
186
+ return $ path ;
187
+ }, $ loader ->getPaths ($ namespace ));
188
+
189
+ if (FilesystemLoader::MAIN_NAMESPACE === $ namespace ) {
190
+ $ namespace = '(None) ' ;
191
+ } else {
192
+ $ namespace = '@ ' .$ namespace ;
193
+ }
194
+
195
+ $ loaderPaths [$ namespace ] = $ paths ;
196
+ }
197
+
198
+ return $ loaderPaths ;
199
+ }
200
+
151
201
private function getMetadata ($ type , $ entity )
152
202
{
153
203
if ($ type === 'globals ' ) {
0 commit comments