11<?php
22namespace PhpRouter ;
33
4- use Exception ;
4+ use PhpRouter \Exception \RouteCallbackNotFoundException ;
5+ use PhpRouter \Exception \RouteInvalidDefinitionException ;
6+ use PhpRouter \Exception \RouteInvalidTypeException ;
7+ use PhpRouter \Exception \RouteWrongCallbackException ;
58use ReflectionClass ;
69
710/**
@@ -15,7 +18,7 @@ class Route
1518 private $ patterns = [
1619 'route ' => '/^
1720 (?<method>[\|\w]+)\h+
18- (?<path >([@\/\w]+))
21+ (?<url >([@\/\w]+))
1922 (?:\.(?<extension>\w+))?
2023 (?:\h+\[(?<type>\w+)\])?$/x ' ,
2124 'callback ' => '/^
@@ -71,7 +74,8 @@ class Route
7174 * @param array|callable $rules
7275 * @param callable|null $callback
7376 * @param array $callbackArgs
74- * @throws Exception
77+ * @throws RouteInvalidDefinitionException
78+ * @throws RouteInvalidTypeException
7579 */
7680 public function __construct ($ route , $ rules , $ callback = null , $ callbackArgs = [])
7781 {
@@ -91,17 +95,17 @@ public function __construct($route, $rules, $callback = null, $callbackArgs = []
9195 * Parse route and save results in object properties
9296 *
9397 * @param string $route
94- * @throws \Exception
95- * @return void
98+ * @throws RouteInvalidDefinitionException
99+ * @throws RouteInvalidTypeException
96100 */
97101 private function parseRoute ($ route )
98102 {
99103 if (!preg_match ($ this ->patterns ['route ' ], $ route , $ result )) {
100- throw new \ Exception ( ' Wrong route format! ' );
104+ throw new RouteInvalidDefinitionException ( );
101105 }
102106
103107 $ this ->methods = explode ('| ' , $ result ['method ' ]);
104- $ this ->url = $ result ['path ' ];
108+ $ this ->url = $ result ['url ' ];
105109
106110 if (!empty ($ result ['extension ' ])) {
107111 $ this ->extension = $ result ['extension ' ];
@@ -110,7 +114,7 @@ private function parseRoute($route)
110114
111115 if (!empty ($ result ['type ' ])) {
112116 if (!in_array ($ result ['type ' ], $ this ->types )) {
113- throw new \ Exception ( sprintf ( ' Wrong type format! Allowed [%s] ' , implode ( ' , ' , $ this -> types )) );
117+ throw new RouteInvalidTypeException ( );
114118 }
115119
116120 $ this ->type = $ result ['type ' ];
@@ -189,7 +193,7 @@ public function parseParams($requestUrl)
189193 * Execute specified Route - anonymous function or pointed class->method
190194 *
191195 * @return mixed
192- * @throws Exception
196+ * @throws RouteWrongCallbackException
193197 */
194198 public function dispatch ()
195199 {
@@ -198,7 +202,7 @@ public function dispatch()
198202 } else if (preg_match ($ this ->patterns ['callback ' ], $ this ->callback , $ result )) {
199203 return $ this ->call ($ result ['class ' ], $ result ['method ' ], $ result ['type ' ], [$ this ->namedParams ]);
200204 }
201- throw new Exception ( ' Wrong callback ' );
205+ throw new RouteWrongCallbackException ( );
202206 }
203207
204208 /**
@@ -209,12 +213,12 @@ public function dispatch()
209213 * @param string $type
210214 * @param array $params
211215 * @return mixed
212- * @throws Exception
216+ * @throws RouteCallbackNotFoundException
213217 */
214218 private function call ($ class , $ method , $ type , array $ params = [])
215219 {
216220 if (!class_exists ($ class ) || !method_exists ($ class , $ method )) {
217- throw new Exception ( ' No class or method found! ' );
221+ throw new RouteCallbackNotFoundException ( );
218222 }
219223
220224 if ('-> ' == $ type ) {
0 commit comments