1919use Psr \Http \Message \ServerRequestInterface ;
2020use Psr \Http \Server \MiddlewareInterface ;
2121use Psr \Http \Server \RequestHandlerInterface ;
22+ use Sunrise \Http \Router \Exception \InvalidArgumentException ;
2223use Sunrise \Http \Router \Exception \MethodNotAllowedException ;
23- use Sunrise \Http \Router \Exception \MiddlewareAlreadyExistsException ;
2424use Sunrise \Http \Router \Exception \PageNotFoundException ;
25- use Sunrise \Http \Router \Exception \RouteAlreadyExistsException ;
2625use Sunrise \Http \Router \Exception \RouteNotFoundException ;
2726use Sunrise \Http \Router \Loader \LoaderInterface ;
2827use Sunrise \Http \Router \RequestHandler \CallableRequestHandler ;
3433use function array_flip ;
3534use function array_keys ;
3635use function array_values ;
36+ use function get_class ;
3737use function spl_object_hash ;
3838use function sprintf ;
3939
@@ -50,6 +50,18 @@ class Router implements MiddlewareInterface, RequestHandlerInterface, RequestMet
5050 */
5151 public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error ' ;
5252
53+ /**
54+ * Global patterns
55+ *
56+ * @var array<string, string>
57+ *
58+ * @since 2.9.0
59+ */
60+ public static $ patterns = [
61+ '@slug ' => '[0-9a-z-]+ ' ,
62+ '@uuid ' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} ' ,
63+ ];
64+
5365 /**
5466 * The router host table
5567 *
@@ -125,17 +137,18 @@ public function addHost(string $alias, string ...$hostname) : void
125137 *
126138 * @return void
127139 *
128- * @throws RouteAlreadyExistsException
140+ * @throws InvalidArgumentException
141+ * if one of the given routes already exists.
129142 */
130143 public function addRoute (RouteInterface ...$ routes ) : void
131144 {
132145 foreach ($ routes as $ route ) {
133146 $ name = $ route ->getName ();
134-
135147 if (isset ($ this ->routes [$ name ])) {
136- throw new RouteAlreadyExistsException (
137- sprintf ('A route with the name "%s" already exists. ' , $ name )
138- );
148+ throw new InvalidArgumentException (sprintf (
149+ 'The route "%s" already exists. ' ,
150+ $ name
151+ ));
139152 }
140153
141154 $ this ->routes [$ name ] = $ route ;
@@ -149,17 +162,18 @@ public function addRoute(RouteInterface ...$routes) : void
149162 *
150163 * @return void
151164 *
152- * @throws MiddlewareAlreadyExistsException
165+ * @throws InvalidArgumentException
166+ * if one of the given middlewares already exists.
153167 */
154168 public function addMiddleware (MiddlewareInterface ...$ middlewares ) : void
155169 {
156170 foreach ($ middlewares as $ middleware ) {
157171 $ hash = spl_object_hash ($ middleware );
158-
159172 if (isset ($ this ->middlewares [$ hash ])) {
160- throw new MiddlewareAlreadyExistsException (
161- sprintf ('A middleware with the hash "%s" already exists. ' , $ hash )
162- );
173+ throw new InvalidArgumentException (sprintf (
174+ 'The middleware "%s" already exists. ' ,
175+ get_class ($ middleware )
176+ ));
163177 }
164178
165179 $ this ->middlewares [$ hash ] = $ middleware ;
@@ -195,9 +209,10 @@ public function getAllowedMethods() : array
195209 public function getRoute (string $ name ) : RouteInterface
196210 {
197211 if (!isset ($ this ->routes [$ name ])) {
198- throw new RouteNotFoundException (
199- sprintf ('No route found for the name "%s". ' , $ name )
200- );
212+ throw new RouteNotFoundException (sprintf (
213+ 'No route found for the name "%s". ' ,
214+ $ name
215+ ));
201216 }
202217
203218 return $ this ->routes [$ name ];
@@ -354,22 +369,18 @@ public function load(LoaderInterface ...$loaders) : void
354369 /**
355370 * Compares the given route host and the given request host
356371 *
357- * Returns ` true` if the route host is ` null`
358- * or if the route host is equal to the request host,
359- * otherwise returns ` false` .
372+ * Returns true if the route host is null or
373+ * if the route host is equal to the request host,
374+ * otherwise returns false.
360375 *
361- * @param null| string $routeHost
376+ * @param string|null $routeHost
362377 * @param string $requestHost
363378 *
364379 * @return bool
365380 */
366381 private function compareHosts (?string $ routeHost , string $ requestHost ) : bool
367382 {
368- if (null === $ routeHost ) {
369- return true ;
370- }
371-
372- if ($ requestHost === $ routeHost ) {
383+ if (null === $ routeHost || $ requestHost === $ routeHost ) {
373384 return true ;
374385 }
375386
0 commit comments