2
2
namespace AppZap \PHPFramework \Mvc ;
3
3
4
4
use AppZap \PHPFramework \Configuration \Configuration ;
5
+ use AppZap \PHPFramework \Http \HttpErrorException ;
5
6
use AppZap \PHPFramework \SignalSlot \Dispatcher as SignalSlotDispatcher ;
6
7
7
8
class Router {
@@ -20,21 +21,23 @@ class Router {
20
21
21
22
/**
22
23
* @param string $resource
23
- * @throws ApplicationPartMissingException
24
+ * @throws HttpErrorException
24
25
* @throws InvalidHttpResponderException
25
26
*/
26
27
public function __construct ($ resource ) {
27
28
$ routes = $ this ->collectRoutesDefinitions ();
28
29
$ this ->route ($ routes , $ resource );
29
30
30
- // check if the responder is valid
31
- if (is_string ($ this ->responder )) {
32
- if (!class_exists ($ this ->responder )) {
33
- throw new InvalidHttpResponderException ('Controller ' . $ this ->responder . ' for uri " ' . $ resource . '" not found! ' , 1415129223 );
34
- }
35
- } elseif (!isset ($ this ->responder )) {
36
- throw new InvalidHttpResponderException ('Route ' . $ resource . ' could not be routed. ' , 1415136995 );
37
- } elseif (!is_callable ($ this ->responder )) {
31
+ if (!isset ($ this ->responder )) {
32
+ HttpStatus::setStatus (HttpStatus::STATUS_404_NOT_FOUND );
33
+ throw new HttpErrorException ('Resource not routable ' , 404 );
34
+ }
35
+
36
+ if (is_string ($ this ->responder ) && !class_exists ($ this ->responder )) {
37
+ throw new InvalidHttpResponderException ('Controller ' . $ this ->responder . ' for uri " ' . $ resource . '" not found! ' , 1415129223 );
38
+ }
39
+
40
+ if (!is_string ($ this ->responder ) && !is_callable ($ this ->responder )) {
38
41
throw new InvalidHttpResponderException ('The responder must either be a class string, a callable or an array of subpaths ' , 1415129333 );
39
42
}
40
43
}
@@ -54,34 +57,58 @@ protected function collectRoutesDefinitions() {
54
57
if (!is_array ($ applicationRoutes )) {
55
58
throw new InvalidHttpResponderException ('The routes file did not return an array with routes ' , 1415135585 );
56
59
}
57
- $ routes = array_merge ( $ routes , $ applicationRoutes ) ;
60
+ $ routes = $ applicationRoutes + $ routes ;
58
61
}
59
62
return $ routes ;
60
63
}
61
64
62
65
/**
63
66
* @param array $routes
64
- * @param string $resource
67
+ * @param mixed $resource
65
68
*/
66
69
protected function route ($ routes , $ resource ) {
70
+ if (is_int ($ resource )) {
71
+ $ this ->routeHttpStatusCode ($ routes , $ resource );
72
+ return ;
73
+ }
67
74
$ resource = ltrim ($ resource , '/ ' );
68
- foreach ($ routes as $ regex => $ regexResponder ) {
75
+ foreach ($ routes as $ regex => $ responder ) {
76
+ if ($ responder === FALSE ) {
77
+ continue ;
78
+ }
69
79
$ regex = $ this ->enhanceRegex ($ regex );
70
- if ($ regexResponder !== FALSE && preg_match ($ regex , $ resource , $ matches )) {
80
+ if (preg_match ($ regex , $ resource , $ matches )) {
71
81
$ matchesCount = count ($ matches );
72
82
for ($ i = 1 ; $ i < $ matchesCount ; $ i ++) {
73
83
$ this ->parameters [] = $ matches [$ i ];
74
84
}
75
- if (is_array ($ regexResponder )) {
76
- $ this ->route ($ regexResponder , preg_replace ($ regex , '' , $ resource ));
85
+ if (is_array ($ responder )) {
86
+ $ this ->route ($ responder , preg_replace ($ regex , '' , $ resource ));
77
87
} else {
78
- $ this ->responder = $ regexResponder ;
88
+ $ this ->responder = $ responder ;
79
89
}
80
90
break ;
81
91
}
82
92
}
83
93
}
84
94
95
+ /**
96
+ * @param array $routes
97
+ * @param int $httpStatusCode
98
+ */
99
+ protected function routeHttpStatusCode ($ routes , $ httpStatusCode ) {
100
+ if (isset ($ routes [$ httpStatusCode ])) {
101
+ $ this ->responder = $ routes [$ httpStatusCode ];
102
+ return ;
103
+ }
104
+ // convert e.g. 405 to 400
105
+ $ baseStatusCode = (int ) floor ($ httpStatusCode / 100 ) * 100 ;
106
+ if (isset ($ routes [$ baseStatusCode ])) {
107
+ $ this ->responder = $ routes [$ baseStatusCode ];
108
+ return ;
109
+ }
110
+ }
111
+
85
112
/**
86
113
* @return array
87
114
*/
0 commit comments