2626 */
2727class Web extends ContainerAwareObject
2828{
29+ const CLASS_METHOD = 'renderPage ' ;
30+ const HTTP_VERBS = ['GET ' , 'POST ' ];
31+
2932 /**
3033 * @var Dispatcher dispatcher
3134 */
@@ -57,6 +60,9 @@ public function __construct(ContainerInterface $container)
5760 $ path = str_replace ("app/site/controllers/ " , "" , str_replace ("\\" , "/ " , strtolower ($ controllerClass )));
5861 $ route_name = str_replace ("/ " , ". " , trim ($ path , "/ " ));
5962
63+ $ classMethod = self ::CLASS_METHOD ;
64+ $ verbs = self ::HTTP_VERBS ;
65+
6066 if (($ tmp = explode ("/ " , $ path , 2 )) && count ($ tmp ) > 1 ) {
6167 $ tmp = array_map (
6268 function ($ el ) {
@@ -78,14 +84,24 @@ function ($el) {
7884 $ group = call_user_func ([$ controllerClass , 'getRouteGroup ' ]) ?? $ group ;
7985 }
8086
87+ if (method_exists ($ controllerClass , 'getRouteVerbs ' )) {
88+ $ verbs = call_user_func ([$ controllerClass , 'getRouteVerbs ' ]) ?? $ verbs ;
89+ if (!is_array ($ verbs )) {
90+ $ verbs = [$ verbs ];
91+ }
92+ if (!empty ($ errors = $ this ->checkRouteVerbs ($ verbs ))) {
93+ throw new InvalidValueException (implode (', ' , $ errors ).": Invalid route verbs " , 1 );
94+ }
95+ }
96+
8197 if (method_exists ($ controllerClass , 'getRoutePath ' )) {
8298 $ path = call_user_func ([$ controllerClass , 'getRoutePath ' ]) ?? $ path ;
8399 if (!$ this ->checkRouteParameters ($ path )) {
84100 throw new InvalidValueException ("' {$ path }': Invalid route string " , 1 );
85101 }
86102 }
87103
88- $ this ->addRoute ($ group , $ route_name , "/ " .ltrim ($ path , "/ " ), $ controllerClass );
104+ $ this ->addRoute ($ group , $ route_name , "/ " .ltrim ($ path , "/ " ), $ controllerClass, $ classMethod , $ verbs );
89105 }
90106 }
91107
@@ -123,6 +139,24 @@ protected function checkRouteParameters($route)
123139 return true ;
124140 }
125141
142+ /**
143+ * checks route http verbs
144+ *
145+ * @param array $verbs
146+ * @return array
147+ */
148+ protected function checkRouteVerbs ($ verbs )
149+ {
150+ if (!is_array ($ verbs )) {
151+ $ verbs = [$ verbs ];
152+ }
153+
154+ return array_diff (
155+ array_filter (array_map ('strtoupper ' , array_map ('trim ' , $ verbs ))),
156+ ['DELETE ' , 'GET ' , 'HEAD ' , 'OPTIONS ' , 'PATCH ' , 'POST ' , 'PUT ' ]
157+ );
158+ }
159+
126160 /**
127161 * adds routes
128162 *
@@ -148,7 +182,7 @@ private function _insertRoutes(RouteCollector $r, array $paths)
148182 */
149183 private function _insertRoute (RouteCollector $ r , array $ p )
150184 {
151- $ r ->addRoute ([ ' GET ' , ' POST ' ], $ p ['path ' ], [$ p ['class ' ], $ p ['method ' ]]);
185+ $ r ->addRoute ($ p [ ' verbs ' ], $ p ['path ' ], [$ p ['class ' ], $ p ['method ' ]]);
152186
153187 return $ this ;
154188 }
@@ -185,9 +219,15 @@ function ($el) use ($class) {
185219 * @param string $class
186220 * @param string $method
187221 */
188- public function addRoute ($ group , $ name , $ path , $ class , $ method = 'renderPage ' )
222+ public function addRoute ($ group , $ name , $ path , $ class , $ method = 'renderPage ' , $ verbs = [ ' GET ' , ' POST ' ] )
189223 {
190- $ this ->routes [$ group ][] = ['path ' => $ path , 'class ' => $ class , 'method ' => $ method , 'name ' => $ name ];
224+ $ this ->routes [$ group ][] = [
225+ 'path ' => $ path ,
226+ 'class ' => $ class ,
227+ 'method ' => $ method ,
228+ 'name ' => $ name ,
229+ 'verbs ' => $ verbs
230+ ];
191231 return $ this ;
192232 }
193233
0 commit comments