@@ -57,33 +57,6 @@ public function match(Nette\Http\IRequest $httpRequest)
57
57
*/
58
58
public function constructUrl (Nette \Application \Request $ appRequest , Nette \Http \Url $ refUrl )
59
59
{
60
- if ($ this ->cachedRoutes === NULL ) {
61
- $ routes = array ();
62
- $ routes ['* ' ] = array ();
63
-
64
- foreach ($ this as $ route ) {
65
- $ presenter = $ route instanceof Route ? $ route ->getTargetPresenter () : NULL ;
66
-
67
- if ($ presenter === FALSE ) {
68
- continue ;
69
- }
70
-
71
- if (is_string ($ presenter )) {
72
- if (!isset ($ routes [$ presenter ])) {
73
- $ routes [$ presenter ] = $ routes ['* ' ];
74
- }
75
- $ routes [$ presenter ][] = $ route ;
76
-
77
- } else {
78
- foreach ($ routes as $ id => $ foo ) {
79
- $ routes [$ id ][] = $ route ;
80
- }
81
- }
82
- }
83
-
84
- $ this ->cachedRoutes = $ routes ;
85
- }
86
-
87
60
if ($ this ->module ) {
88
61
if (strncmp ($ tmp = $ appRequest ->getPresenterName (), $ this ->module , strlen ($ this ->module )) === 0 ) {
89
62
$ appRequest = clone $ appRequest ;
@@ -94,6 +67,10 @@ public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\U
94
67
}
95
68
96
69
$ presenter = $ appRequest ->getPresenterName ();
70
+ if ($ this ->cachedRoutes === NULL ) {
71
+ $ this ->cachedRoutes = $ this ->buildCache ();
72
+ }
73
+
97
74
if (!isset ($ this ->cachedRoutes [$ presenter ])) {
98
75
$ presenter = '* ' ;
99
76
}
@@ -132,4 +109,45 @@ public function getModule()
132
109
return $ this ->module ;
133
110
}
134
111
112
+
113
+ /**
114
+ * Returns list of possible target presenters or NULL if the list is dynamic.
115
+ *
116
+ * @return string[]|NULL
117
+ */
118
+ public function getTargetPresenters ()
119
+ {
120
+ if ($ this ->cachedRoutes === NULL ) {
121
+ $ this ->cachedRoutes = $ this ->buildCache ();
122
+ }
123
+
124
+ if (empty ($ this ->cachedRoutes ['* ' ])) {
125
+ $ presenters = array_keys ($ this ->cachedRoutes );
126
+ unset($ presenters [0 ]); // remove '*'
127
+ return $ presenters ;
128
+ }
129
+
130
+ return NULL ;
131
+ }
132
+
133
+
134
+ /**
135
+ * @return array
136
+ */
137
+ private function buildCache ()
138
+ {
139
+ $ routes = array ('* ' => array ());
140
+ foreach ($ this as $ route ) {
141
+ $ presenters = method_exists ($ route , 'getTargetPresenters ' ) ? $ route ->getTargetPresenters () : NULL ;
142
+ $ keys = is_array ($ presenters ) ? $ presenters : array_keys ($ routes );
143
+ foreach ($ keys as $ key ) {
144
+ if (!isset ($ routes [$ key ])) {
145
+ $ routes [$ key ] = $ routes ['* ' ];
146
+ }
147
+ $ routes [$ key ][] = $ route ;
148
+ }
149
+ }
150
+ return $ routes ;
151
+ }
152
+
135
153
}
0 commit comments