@@ -38,6 +38,11 @@ public function __construct(KernelInterface $kernel)
38
38
* Converts a short notation a:b:c to a class::method.
39
39
*
40
40
* @param string $controller A short notation controller (a:b:c)
41
+ *
42
+ * @return string A string with class::method
43
+ *
44
+ * @throws \InvalidArgumentException when the specified bundle is not enabled
45
+ * or the controller cannot be found
41
46
*/
42
47
public function parse ($ controller )
43
48
{
@@ -47,39 +52,22 @@ public function parse($controller)
47
52
48
53
list ($ bundle , $ controller , $ action ) = $ parts ;
49
54
$ controller = str_replace ('/ ' , '\\' , $ controller );
50
- $ class = null ;
51
- $ logs = array ();
55
+ $ bundles = array ();
56
+
57
+ // this throws an exception if there is no such bundle
52
58
foreach ($ this ->kernel ->getBundle ($ bundle , false ) as $ b ) {
53
59
$ try = $ b ->getNamespace ().'\\Controller \\' .$ controller .'Controller ' ;
54
- if (!class_exists ($ try )) {
55
- $ logs [] = sprintf ('Unable to find controller "%s:%s" - class "%s" does not exist. ' , $ bundle , $ controller , $ try );
56
- } else {
57
- $ class = $ try ;
58
-
59
- break ;
60
+ if (class_exists ($ try )) {
61
+ return $ try .':: ' .$ action .'Action ' ;
60
62
}
61
- }
62
63
63
- if ( null === $ class ) {
64
- $ this -> handleControllerNotFoundException ( $ bundle , $ controller , $ logs );
64
+ $ bundles [] = $ b -> getName ();
65
+ $ msg = sprintf ( ' Unable to find controller "%s:%s" - class "%s" does not exist. ' , $ bundle , $ controller , $ try );
65
66
}
66
67
67
- return $ class .':: ' .$ action .'Action ' ;
68
- }
69
-
70
- private function handleControllerNotFoundException ($ bundle , $ controller , array $ logs )
71
- {
72
- // just one log, return it as the exception
73
- if (1 == count ($ logs )) {
74
- throw new \InvalidArgumentException ($ logs [0 ]);
75
- }
76
-
77
- // many logs, use a message that mentions each searched bundle
78
- $ names = array ();
79
- foreach ($ this ->kernel ->getBundle ($ bundle , false ) as $ b ) {
80
- $ names [] = $ b ->getName ();
68
+ if (count ($ bundles ) > 1 ) {
69
+ $ msg = sprintf ('Unable to find controller "%s:%s" in bundles %s. ' , $ bundle , $ controller , implode (', ' , $ bundles ));
81
70
}
82
- $ msg = sprintf ('Unable to find controller "%s:%s" in bundles %s. ' , $ bundle , $ controller , implode (', ' , $ names ));
83
71
84
72
throw new \InvalidArgumentException ($ msg );
85
73
}
0 commit comments