@@ -144,65 +144,93 @@ public static String[] getControllerMethodShortcut(@NotNull Method method) {
144144 return new String [0 ];
145145 }
146146
147+ String shortcutName = null ;
148+ String shortcutNameForOldNotation = null ;
149+
147150 SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil (method .getProject ());
148151 SymfonyBundle symfonyBundle = symfonyBundleUtil .getContainingBundle (phpClass );
149- if (symfonyBundle == null ) {
150- return new String [0 ];
151- }
152-
153- // check if files is in <Bundle>/Controller/*
154- PhpClass bundleClass = symfonyBundle .getPhpClass ();
155- if (!phpClass .getNamespaceName ().startsWith (bundleClass .getNamespaceName () + "Controller\\ " )) {
156- return new String [0 ];
157- }
158-
159- // strip the controller folder name
160- String templateFolderName = phpClass .getNamespaceName ().substring (bundleClass .getNamespaceName ().length () + 11 );
152+ if (symfonyBundle != null ) {
153+ // check if files is in <Bundle>/Controller/*
154+ PhpClass bundleClass = symfonyBundle .getPhpClass ();
155+ if (!phpClass .getNamespaceName ().startsWith (bundleClass .getNamespaceName () + "Controller\\ " )) {
156+ return new String [0 ];
157+ }
158+
159+ // strip the controller folder name
160+ String templateFolderName = phpClass .getNamespaceName ().substring (bundleClass .getNamespaceName ().length () + 11 );
161+
162+ // HomeBundle:default:indexes
163+ // HomeBundle:default/Test:indexes
164+ templateFolderName = templateFolderName .replace ("\\ " , "/" );
165+
166+ // Foobar without (.html.twig)
167+ String templateName = className .substring (0 , className .lastIndexOf ("Controller" ));
168+
169+ if (methodName .equals ("__invoke" )) {
170+ // AppBundle::foo_bar.html.twig
171+ shortcutName = String .format (
172+ "%s::%s%s" ,
173+ symfonyBundle .getName (),
174+ underscore (templateFolderName ),
175+ underscore (templateName )
176+ );
161177
162- // HomeBundle:default:indexes
163- // HomeBundle:default/Test:indexes
164- templateFolderName = templateFolderName .replace ("\\ " , "/" );
178+ // AppBundle::FooBar.html.twig
179+ shortcutNameForOldNotation = String .format (
180+ "%s::%s%s" ,
181+ symfonyBundle .getName (),
182+ templateFolderName ,
183+ templateName
184+ );
185+ } else {
186+ // FooBundle:foo_bar:foo_bar.html.twig
187+ shortcutName = String .format (
188+ "%s:%s%s:%s" ,
189+ symfonyBundle .getName (),
190+ underscore (templateFolderName ),
191+ underscore (templateName ),
192+ underscore (StringUtils .removeEnd (methodName , "Action" ))
193+ );
165194
166- String shortcutName ;
167- String shortcutNameForOldNotation ;
195+ // FooBundle:FooBar:fooBar.html.twig
196+ shortcutNameForOldNotation = String .format (
197+ "%s:%s%s:%s" ,
198+ symfonyBundle .getName (),
199+ templateFolderName ,
200+ templateName ,
201+ StringUtils .removeEnd (methodName , "Action" )
202+ );
203+ }
204+ } else {
205+ // https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/src/Templating/TemplateGuesser.php
206+ String fqn = phpClass .getFQN ();
207+ Matcher matcher = Pattern .compile ("Controller\\ \\ (.+)Controller$" ).matcher (fqn );
208+ if (matcher .find ()) {
209+ String domain = matcher .group (1 );
210+
211+ String matchController = domain .replaceAll ("\\ \\ " , "/" );
212+
213+ String matchAction = null ;
214+ if (!"__invoke" .equals (methodName )) {
215+ matchAction = methodName .replaceAll ("Action$" , "" );
216+ matchAction = matchAction ;
217+ }
168218
169- // Foobar without (.html.twig)
170- String templateName = className .substring (0 , className .lastIndexOf ("Controller" ));
219+ List <String > parts = new ArrayList <>(
220+ Arrays .asList (matchController .split ("/" ))
221+ );
171222
172- if (methodName .equals ("__invoke" )) {
173- // AppBundle::foo_bar.html.twig
174- shortcutName = String .format (
175- "%s::%s%s" ,
176- symfonyBundle .getName (),
177- underscore (templateFolderName ),
178- underscore (templateName )
179- );
223+ if (matchAction != null ) {
224+ parts .add (matchAction );
225+ }
180226
181- // AppBundle::FooBar.html.twig
182- shortcutNameForOldNotation = String .format (
183- "%s::%s%s" ,
184- symfonyBundle .getName (),
185- templateFolderName ,
186- templateName
187- );
188- } else {
189- // FooBundle:foo_bar:foo_bar.html.twig
190- shortcutName = String .format (
191- "%s:%s%s:%s" ,
192- symfonyBundle .getName (),
193- underscore (templateFolderName ),
194- underscore (templateName ),
195- underscore (StringUtils .removeEnd (methodName , "Action" ))
196- );
227+ shortcutNameForOldNotation = String .join ("/" , parts );
228+ shortcutName = underscore (String .join ("/" , parts ));
229+ }
230+ }
197231
198- // FooBundle:FooBar:fooBar.html.twig
199- shortcutNameForOldNotation = String .format (
200- "%s:%s%s:%s" ,
201- symfonyBundle .getName (),
202- templateFolderName ,
203- templateName ,
204- StringUtils .removeEnd (methodName , "Action" )
205- );
232+ if (shortcutName == null || shortcutNameForOldNotation == null ) {
233+ return new String [0 ];
206234 }
207235
208236 // @TODO: we should support types later on; but nicer
0 commit comments