@@ -22,102 +22,102 @@ |
||
| 22 | 22 | trait ResourceCollectorTrait |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - abstract public function set($method, $pattern, $action); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A map of all routes of resources. |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - |
|
| 33 | - protected $map = [ |
|
| 34 | - "index" => ["get", "/{name}"], |
|
| 35 | - "make" => ["get", "/{name}/make"], |
|
| 36 | - "create" => ["post", "/{name}"], |
|
| 37 | - "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | - "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | - "update" => ["put", "/{name}/{id:int+}"], |
|
| 40 | - "delete" => ["delete", "/{name}/{id:int+}"], |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
| 45 | - * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
| 46 | - * a resourceful route declares them in a single line of code. |
|
| 47 | - * |
|
| 48 | - * @param string $controller The controller name. |
|
| 49 | - * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
| 50 | - * explicit say that only this routes will be registered, and |
|
| 51 | - * "except" that register all the routes except the indicates. |
|
| 52 | - * @return Resource |
|
| 53 | - */ |
|
| 54 | - |
|
| 55 | - public function resource($controller, array $options = array()) |
|
| 56 | - { |
|
| 57 | - $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
| 58 | - $name .= $this->getResourceName($controller, $options); |
|
| 59 | - $actions = $this->getResourceActions($options); |
|
| 60 | - $resource = new Resource; |
|
| 61 | - |
|
| 62 | - foreach ($actions as $action => $map) { |
|
| 63 | - $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return $resource; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Collect several resources at same time. |
|
| 71 | - * |
|
| 72 | - * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
| 73 | - * @return Resource |
|
| 74 | - */ |
|
| 75 | - |
|
| 76 | - public function resources(array $controllers) |
|
| 77 | - { |
|
| 78 | - $resource = new Resource; |
|
| 79 | - foreach ($controllers as $controller) |
|
| 80 | - $resource->set($this->resource($controller)); |
|
| 81 | - return $resource; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $controller |
|
| 86 | - * @param array $options |
|
| 87 | - * |
|
| 88 | - * @return mixed |
|
| 89 | - */ |
|
| 90 | - |
|
| 91 | - protected function getResourceName($controller, array $options) |
|
| 92 | - { |
|
| 93 | - return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param array $options |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - |
|
| 101 | - protected function getResourceActions(array $options) |
|
| 102 | - { |
|
| 103 | - return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
| 104 | - (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $action |
|
| 109 | - * @param string $path |
|
| 110 | - * @param string $name |
|
| 111 | - * @param string[] $options |
|
| 112 | - * |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - |
|
| 116 | - protected function getResourcePath($action, $path, $name, array $options) |
|
| 117 | - { |
|
| 118 | - return str_replace("{name}", $name, |
|
| 119 | - $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
| 120 | - ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 121 | - } |
|
| 25 | + abstract public function set($method, $pattern, $action); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A map of all routes of resources. |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + |
|
| 33 | + protected $map = [ |
|
| 34 | + "index" => ["get", "/{name}"], |
|
| 35 | + "make" => ["get", "/{name}/make"], |
|
| 36 | + "create" => ["post", "/{name}"], |
|
| 37 | + "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | + "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | + "update" => ["put", "/{name}/{id:int+}"], |
|
| 40 | + "delete" => ["delete", "/{name}/{id:int+}"], |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
| 45 | + * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
| 46 | + * a resourceful route declares them in a single line of code. |
|
| 47 | + * |
|
| 48 | + * @param string $controller The controller name. |
|
| 49 | + * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
| 50 | + * explicit say that only this routes will be registered, and |
|
| 51 | + * "except" that register all the routes except the indicates. |
|
| 52 | + * @return Resource |
|
| 53 | + */ |
|
| 54 | + |
|
| 55 | + public function resource($controller, array $options = array()) |
|
| 56 | + { |
|
| 57 | + $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
| 58 | + $name .= $this->getResourceName($controller, $options); |
|
| 59 | + $actions = $this->getResourceActions($options); |
|
| 60 | + $resource = new Resource; |
|
| 61 | + |
|
| 62 | + foreach ($actions as $action => $map) { |
|
| 63 | + $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return $resource; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Collect several resources at same time. |
|
| 71 | + * |
|
| 72 | + * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
| 73 | + * @return Resource |
|
| 74 | + */ |
|
| 75 | + |
|
| 76 | + public function resources(array $controllers) |
|
| 77 | + { |
|
| 78 | + $resource = new Resource; |
|
| 79 | + foreach ($controllers as $controller) |
|
| 80 | + $resource->set($this->resource($controller)); |
|
| 81 | + return $resource; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $controller |
|
| 86 | + * @param array $options |
|
| 87 | + * |
|
| 88 | + * @return mixed |
|
| 89 | + */ |
|
| 90 | + |
|
| 91 | + protected function getResourceName($controller, array $options) |
|
| 92 | + { |
|
| 93 | + return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param array $options |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + |
|
| 101 | + protected function getResourceActions(array $options) |
|
| 102 | + { |
|
| 103 | + return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
| 104 | + (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $action |
|
| 109 | + * @param string $path |
|
| 110 | + * @param string $name |
|
| 111 | + * @param string[] $options |
|
| 112 | + * |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + |
|
| 116 | + protected function getResourcePath($action, $path, $name, array $options) |
|
| 117 | + { |
|
| 118 | + return str_replace("{name}", $name, |
|
| 119 | + $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
| 120 | + ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | } |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | class BadDispatchStrategyException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - public function __construct($strategy) { |
|
| 24 | - parent::__construct("`$strategy` is not a valid route dispatch strategy, it must implement the `Codeburner\Router\DispatchStrategies\DispatchStrategyInterface` interface."); |
|
| 25 | - } |
|
| 23 | + public function __construct($strategy) { |
|
| 24 | + parent::__construct("`$strategy` is not a valid route dispatch strategy, it must implement the `Codeburner\Router\DispatchStrategies\DispatchStrategyInterface` interface."); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -20,71 +20,71 @@ |
||
| 20 | 20 | class MethodNotAllowedException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The HTTP method from request. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 23 | + /** |
|
| 24 | + * The HTTP method from request. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | 28 | |
| 29 | - public $requestedMethod; |
|
| 29 | + public $requestedMethod; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The requested URi. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 31 | + /** |
|
| 32 | + * The requested URi. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | 36 | |
| 37 | - public $requestedUri; |
|
| 37 | + public $requestedUri; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * All the allowed HTTP methods and routes for the request. |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 39 | + /** |
|
| 40 | + * All the allowed HTTP methods and routes for the request. |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | 44 | |
| 45 | - public $allowedMethods; |
|
| 45 | + public $allowedMethods; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Exception constructor. |
|
| 49 | - * |
|
| 50 | - * @param string $requestedMethod The request HTTP method. |
|
| 51 | - * @param string $requestedUri The request URi. |
|
| 52 | - * @param array $allowedMethods All the allowed HTTP methods and routes for the request. |
|
| 53 | - * @param string $message The exception error message. |
|
| 54 | - * @param integer $code The exception error code. |
|
| 55 | - */ |
|
| 47 | + /** |
|
| 48 | + * Exception constructor. |
|
| 49 | + * |
|
| 50 | + * @param string $requestedMethod The request HTTP method. |
|
| 51 | + * @param string $requestedUri The request URi. |
|
| 52 | + * @param array $allowedMethods All the allowed HTTP methods and routes for the request. |
|
| 53 | + * @param string $message The exception error message. |
|
| 54 | + * @param integer $code The exception error code. |
|
| 55 | + */ |
|
| 56 | 56 | |
| 57 | - public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405) |
|
| 58 | - { |
|
| 59 | - $this->requestedMethod = $requestedMethod; |
|
| 60 | - $this->requestedUri = $requestedUri; |
|
| 61 | - $this->allowedMethods = $allowedMethods; |
|
| 62 | - parent::__construct($message, $code); |
|
| 63 | - } |
|
| 57 | + public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405) |
|
| 58 | + { |
|
| 59 | + $this->requestedMethod = $requestedMethod; |
|
| 60 | + $this->requestedUri = $requestedUri; |
|
| 61 | + $this->allowedMethods = $allowedMethods; |
|
| 62 | + parent::__construct($message, $code); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Verify if the given HTTP method is allowed for the request. |
|
| 67 | - * |
|
| 68 | - * @param string An HTTP method |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 65 | + /** |
|
| 66 | + * Verify if the given HTTP method is allowed for the request. |
|
| 67 | + * |
|
| 68 | + * @param string An HTTP method |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | 71 | |
| 72 | - public function can($method) |
|
| 73 | - { |
|
| 74 | - return isset($this->allowedMethods[strtoupper($method)]); |
|
| 75 | - } |
|
| 72 | + public function can($method) |
|
| 73 | + { |
|
| 74 | + return isset($this->allowedMethods[strtoupper($method)]); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The HTTP specification requires that a 405 Method Not Allowed response include the |
|
| 79 | - * Allow: header to detail available methods for the requested resource. |
|
| 80 | - * |
|
| 81 | - * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 77 | + /** |
|
| 78 | + * The HTTP specification requires that a 405 Method Not Allowed response include the |
|
| 79 | + * Allow: header to detail available methods for the requested resource. |
|
| 80 | + * |
|
| 81 | + * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | 84 | |
| 85 | - public function allowed() |
|
| 86 | - { |
|
| 87 | - return implode(', ', array_keys($this->allowedMethods)); |
|
| 88 | - } |
|
| 85 | + public function allowed() |
|
| 86 | + { |
|
| 87 | + return implode(', ', array_keys($this->allowedMethods)); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | class MethodNotSupportedException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - public function __construct($method) { |
|
| 24 | - parent::__construct("The HTTP method '$method' is not supported by the route collector."); |
|
| 25 | - } |
|
| 23 | + public function __construct($method) { |
|
| 24 | + parent::__construct("The HTTP method '$method' is not supported by the route collector."); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -19,36 +19,36 @@ |
||
| 19 | 19 | class NotFoundException extends \Exception |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The HTTP method from request. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 22 | + /** |
|
| 23 | + * The HTTP method from request. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | 27 | |
| 28 | - public $requestedMethod; |
|
| 28 | + public $requestedMethod; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The requested Path. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 30 | + /** |
|
| 31 | + * The requested Path. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | 35 | |
| 36 | - public $requestedPath; |
|
| 36 | + public $requestedPath; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Exception constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $requestedMethod The request HTTP method. |
|
| 42 | - * @param string $requestedPath The request Path. |
|
| 43 | - * @param string $message The exception error message. |
|
| 44 | - * @param integer $code The exception error code. |
|
| 45 | - */ |
|
| 46 | - |
|
| 47 | - public function __construct($requestedMethod, $requestedPath, $message = null, $code = 405) |
|
| 48 | - { |
|
| 49 | - $this->requestedMethod = $requestedMethod; |
|
| 50 | - $this->requestedPath = $requestedPath; |
|
| 51 | - parent::__construct($message, $code); |
|
| 52 | - } |
|
| 38 | + /** |
|
| 39 | + * Exception constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $requestedMethod The request HTTP method. |
|
| 42 | + * @param string $requestedPath The request Path. |
|
| 43 | + * @param string $message The exception error message. |
|
| 44 | + * @param integer $code The exception error code. |
|
| 45 | + */ |
|
| 46 | + |
|
| 47 | + public function __construct($requestedMethod, $requestedPath, $message = null, $code = 405) |
|
| 48 | + { |
|
| 49 | + $this->requestedMethod = $requestedMethod; |
|
| 50 | + $this->requestedPath = $requestedPath; |
|
| 51 | + parent::__construct($message, $code); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | 54 | } |
@@ -19,255 +19,255 @@ |
||
| 19 | 19 | class Group |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * All grouped route objects. |
|
| 24 | - * |
|
| 25 | - * @var Route[] |
|
| 26 | - */ |
|
| 27 | - |
|
| 28 | - protected $routes; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Group constructor. |
|
| 32 | - * |
|
| 33 | - * @param Route[] $routes |
|
| 34 | - */ |
|
| 35 | - |
|
| 36 | - public function __construct(array $routes = []) |
|
| 37 | - { |
|
| 38 | - $this->routes = $routes; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Set a new Route or merge an existing group of routes. |
|
| 43 | - * |
|
| 44 | - * @param Group|Route $route |
|
| 45 | - * @return self |
|
| 46 | - */ |
|
| 47 | - |
|
| 48 | - public function set($route) |
|
| 49 | - { |
|
| 50 | - if ($route instanceof Group) { |
|
| 51 | - foreach ($route->all() as $r) |
|
| 52 | - $this->routes[] = $r; |
|
| 53 | - } else $this->routes[] = $route; |
|
| 54 | - return $this; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Return all grouped routes objects. |
|
| 59 | - * |
|
| 60 | - * @return Route[] |
|
| 61 | - */ |
|
| 62 | - |
|
| 63 | - public function all() |
|
| 64 | - { |
|
| 65 | - return $this->routes; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get a specific route of the group, routes receive a key based on |
|
| 70 | - * the order they are added to the group. |
|
| 71 | - * |
|
| 72 | - * @param int $number |
|
| 73 | - * @return Route |
|
| 74 | - */ |
|
| 75 | - |
|
| 76 | - public function nth($number) |
|
| 77 | - { |
|
| 78 | - return $this->routes[$number]; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Forget the registration of all grouped routes on to collector. |
|
| 83 | - * After the forget the route object will still exist but will not |
|
| 84 | - * count for the matcher. |
|
| 85 | - * |
|
| 86 | - * @return self |
|
| 87 | - */ |
|
| 88 | - |
|
| 89 | - public function forget() |
|
| 90 | - { |
|
| 91 | - foreach ($this->routes as $route) |
|
| 92 | - $route->forget(); |
|
| 93 | - return $this; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Set one HTTP method to all grouped routes. |
|
| 98 | - * |
|
| 99 | - * @param string $method The HTTP Method |
|
| 100 | - * @return self |
|
| 101 | - */ |
|
| 102 | - |
|
| 103 | - public function setMethod($method) |
|
| 104 | - { |
|
| 105 | - foreach ($this->routes as $route) |
|
| 106 | - $route->setMethod($method); |
|
| 107 | - return $this; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Set one action to all grouped routes. |
|
| 112 | - * |
|
| 113 | - * @param string $action |
|
| 114 | - * @return self |
|
| 115 | - */ |
|
| 116 | - |
|
| 117 | - public function setAction($action) |
|
| 118 | - { |
|
| 119 | - foreach ($this->routes as $route) |
|
| 120 | - $route->setAction($action); |
|
| 121 | - return $this; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Set one namespace to all grouped routes. |
|
| 126 | - * |
|
| 127 | - * @param string $namespace |
|
| 128 | - * @return self |
|
| 129 | - */ |
|
| 130 | - |
|
| 131 | - public function setNamespace($namespace) |
|
| 132 | - { |
|
| 133 | - foreach ($this->routes as $route) |
|
| 134 | - $route->setNamespace($namespace); |
|
| 135 | - return $this; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Add a prefix to all grouped routes pattern. |
|
| 140 | - * |
|
| 141 | - * @param string $prefix |
|
| 142 | - * @return self |
|
| 143 | - */ |
|
| 144 | - |
|
| 145 | - public function setPrefix($prefix) |
|
| 146 | - { |
|
| 147 | - $prefix = "/" . ltrim($prefix, "/"); |
|
| 148 | - foreach ($this->routes as $route) |
|
| 149 | - $route->setPattern(rtrim($prefix . $route->getPattern(), "/")); |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Set metadata to all grouped routes. |
|
| 155 | - * |
|
| 156 | - * @param string $key |
|
| 157 | - * @param string $value |
|
| 158 | - * |
|
| 159 | - * @return $this |
|
| 160 | - */ |
|
| 161 | - |
|
| 162 | - public function setMetadata($key, $value) |
|
| 163 | - { |
|
| 164 | - foreach ($this->routes as $route) |
|
| 165 | - $route->setMetadata($key, $value); |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Set a bunch of metadata to all grouped routes. |
|
| 171 | - * |
|
| 172 | - * @param mixed[] $metadata |
|
| 173 | - * @return $this |
|
| 174 | - */ |
|
| 175 | - |
|
| 176 | - public function setMetadataArray(array $metadata) |
|
| 177 | - { |
|
| 178 | - foreach ($this->routes as $route) |
|
| 179 | - $route->setMetadataArray($metadata); |
|
| 180 | - return $this; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Set default parameters to all grouped routes. |
|
| 185 | - * |
|
| 186 | - * @param mixed[] $defaults |
|
| 187 | - * @return $this |
|
| 188 | - */ |
|
| 189 | - |
|
| 190 | - public function setDefaults(array $defaults) |
|
| 191 | - { |
|
| 192 | - foreach ($this->routes as $route) |
|
| 193 | - $route->setDefaults($defaults); |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Set a default parameter to all grouped routes. |
|
| 199 | - * |
|
| 200 | - * @param string $key |
|
| 201 | - * @param mixed $value |
|
| 202 | - * |
|
| 203 | - * @return $this |
|
| 204 | - */ |
|
| 205 | - |
|
| 206 | - public function setDefault($key, $value) |
|
| 207 | - { |
|
| 208 | - foreach ($this->routes as $route) |
|
| 209 | - $route->setDefault($key, $value); |
|
| 210 | - return $this; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Set one dispatch strategy to all grouped routes. |
|
| 215 | - * |
|
| 216 | - * @param string|Strategies\StrategyInterface $strategy |
|
| 217 | - * @return self |
|
| 218 | - */ |
|
| 219 | - |
|
| 220 | - public function setStrategy($strategy) |
|
| 221 | - { |
|
| 222 | - foreach ($this->routes as $route) |
|
| 223 | - $route->setStrategy($strategy); |
|
| 224 | - return $this; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Replace or define a constraint for all dynamic segments named by $name. |
|
| 229 | - * |
|
| 230 | - * @param string $name |
|
| 231 | - * @param string $regex |
|
| 232 | - * |
|
| 233 | - * @return self |
|
| 234 | - */ |
|
| 235 | - |
|
| 236 | - public function setConstraint($name, $regex) |
|
| 237 | - { |
|
| 238 | - foreach ($this->routes as $route) { |
|
| 239 | - $pattern = $route->getPattern(); |
|
| 240 | - $initPos = strpos($pattern, "{" . $name); |
|
| 241 | - |
|
| 242 | - if ($initPos !== false) { |
|
| 243 | - $endPos = strpos($pattern, "}", $initPos); |
|
| 244 | - $newPattern = substr_replace($pattern, "{" . "$name:$regex" . "}", $initPos, $endPos - $initPos + 1); |
|
| 245 | - $route->setPatternWithoutReset($newPattern); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $this; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Define a constraint for a variable in all grouped routes pattern, but don't replace the |
|
| 254 | - * delimiter if one already exists. |
|
| 255 | - * |
|
| 256 | - * @param string $name |
|
| 257 | - * @param string $regex |
|
| 258 | - * |
|
| 259 | - * @return self |
|
| 260 | - */ |
|
| 261 | - |
|
| 262 | - public function setConstraintPreservingQuantifiers($name, $regex) |
|
| 263 | - { |
|
| 264 | - $wildcards = $this->routes[0]->getCollector()->getWildcards(); |
|
| 265 | - $quantifierPos = strpos($regex, "{") + strpos($regex, "+") + strpos($regex, "*"); |
|
| 266 | - $quantifier = substr($regex, $quantifierPos); |
|
| 267 | - $quantifierErased = $quantifierPos !== false ? substr($regex, 0, $quantifierPos) : null; |
|
| 268 | - $regex = isset($wildcards[$quantifierErased]) ? $wildcards[$quantifierErased] . $quantifier : $regex; |
|
| 269 | - |
|
| 270 | - return $this->setConstraint($name, $regex); |
|
| 271 | - } |
|
| 22 | + /** |
|
| 23 | + * All grouped route objects. |
|
| 24 | + * |
|
| 25 | + * @var Route[] |
|
| 26 | + */ |
|
| 27 | + |
|
| 28 | + protected $routes; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Group constructor. |
|
| 32 | + * |
|
| 33 | + * @param Route[] $routes |
|
| 34 | + */ |
|
| 35 | + |
|
| 36 | + public function __construct(array $routes = []) |
|
| 37 | + { |
|
| 38 | + $this->routes = $routes; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Set a new Route or merge an existing group of routes. |
|
| 43 | + * |
|
| 44 | + * @param Group|Route $route |
|
| 45 | + * @return self |
|
| 46 | + */ |
|
| 47 | + |
|
| 48 | + public function set($route) |
|
| 49 | + { |
|
| 50 | + if ($route instanceof Group) { |
|
| 51 | + foreach ($route->all() as $r) |
|
| 52 | + $this->routes[] = $r; |
|
| 53 | + } else $this->routes[] = $route; |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Return all grouped routes objects. |
|
| 59 | + * |
|
| 60 | + * @return Route[] |
|
| 61 | + */ |
|
| 62 | + |
|
| 63 | + public function all() |
|
| 64 | + { |
|
| 65 | + return $this->routes; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get a specific route of the group, routes receive a key based on |
|
| 70 | + * the order they are added to the group. |
|
| 71 | + * |
|
| 72 | + * @param int $number |
|
| 73 | + * @return Route |
|
| 74 | + */ |
|
| 75 | + |
|
| 76 | + public function nth($number) |
|
| 77 | + { |
|
| 78 | + return $this->routes[$number]; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Forget the registration of all grouped routes on to collector. |
|
| 83 | + * After the forget the route object will still exist but will not |
|
| 84 | + * count for the matcher. |
|
| 85 | + * |
|
| 86 | + * @return self |
|
| 87 | + */ |
|
| 88 | + |
|
| 89 | + public function forget() |
|
| 90 | + { |
|
| 91 | + foreach ($this->routes as $route) |
|
| 92 | + $route->forget(); |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Set one HTTP method to all grouped routes. |
|
| 98 | + * |
|
| 99 | + * @param string $method The HTTP Method |
|
| 100 | + * @return self |
|
| 101 | + */ |
|
| 102 | + |
|
| 103 | + public function setMethod($method) |
|
| 104 | + { |
|
| 105 | + foreach ($this->routes as $route) |
|
| 106 | + $route->setMethod($method); |
|
| 107 | + return $this; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Set one action to all grouped routes. |
|
| 112 | + * |
|
| 113 | + * @param string $action |
|
| 114 | + * @return self |
|
| 115 | + */ |
|
| 116 | + |
|
| 117 | + public function setAction($action) |
|
| 118 | + { |
|
| 119 | + foreach ($this->routes as $route) |
|
| 120 | + $route->setAction($action); |
|
| 121 | + return $this; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Set one namespace to all grouped routes. |
|
| 126 | + * |
|
| 127 | + * @param string $namespace |
|
| 128 | + * @return self |
|
| 129 | + */ |
|
| 130 | + |
|
| 131 | + public function setNamespace($namespace) |
|
| 132 | + { |
|
| 133 | + foreach ($this->routes as $route) |
|
| 134 | + $route->setNamespace($namespace); |
|
| 135 | + return $this; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Add a prefix to all grouped routes pattern. |
|
| 140 | + * |
|
| 141 | + * @param string $prefix |
|
| 142 | + * @return self |
|
| 143 | + */ |
|
| 144 | + |
|
| 145 | + public function setPrefix($prefix) |
|
| 146 | + { |
|
| 147 | + $prefix = "/" . ltrim($prefix, "/"); |
|
| 148 | + foreach ($this->routes as $route) |
|
| 149 | + $route->setPattern(rtrim($prefix . $route->getPattern(), "/")); |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Set metadata to all grouped routes. |
|
| 155 | + * |
|
| 156 | + * @param string $key |
|
| 157 | + * @param string $value |
|
| 158 | + * |
|
| 159 | + * @return $this |
|
| 160 | + */ |
|
| 161 | + |
|
| 162 | + public function setMetadata($key, $value) |
|
| 163 | + { |
|
| 164 | + foreach ($this->routes as $route) |
|
| 165 | + $route->setMetadata($key, $value); |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Set a bunch of metadata to all grouped routes. |
|
| 171 | + * |
|
| 172 | + * @param mixed[] $metadata |
|
| 173 | + * @return $this |
|
| 174 | + */ |
|
| 175 | + |
|
| 176 | + public function setMetadataArray(array $metadata) |
|
| 177 | + { |
|
| 178 | + foreach ($this->routes as $route) |
|
| 179 | + $route->setMetadataArray($metadata); |
|
| 180 | + return $this; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Set default parameters to all grouped routes. |
|
| 185 | + * |
|
| 186 | + * @param mixed[] $defaults |
|
| 187 | + * @return $this |
|
| 188 | + */ |
|
| 189 | + |
|
| 190 | + public function setDefaults(array $defaults) |
|
| 191 | + { |
|
| 192 | + foreach ($this->routes as $route) |
|
| 193 | + $route->setDefaults($defaults); |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Set a default parameter to all grouped routes. |
|
| 199 | + * |
|
| 200 | + * @param string $key |
|
| 201 | + * @param mixed $value |
|
| 202 | + * |
|
| 203 | + * @return $this |
|
| 204 | + */ |
|
| 205 | + |
|
| 206 | + public function setDefault($key, $value) |
|
| 207 | + { |
|
| 208 | + foreach ($this->routes as $route) |
|
| 209 | + $route->setDefault($key, $value); |
|
| 210 | + return $this; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Set one dispatch strategy to all grouped routes. |
|
| 215 | + * |
|
| 216 | + * @param string|Strategies\StrategyInterface $strategy |
|
| 217 | + * @return self |
|
| 218 | + */ |
|
| 219 | + |
|
| 220 | + public function setStrategy($strategy) |
|
| 221 | + { |
|
| 222 | + foreach ($this->routes as $route) |
|
| 223 | + $route->setStrategy($strategy); |
|
| 224 | + return $this; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Replace or define a constraint for all dynamic segments named by $name. |
|
| 229 | + * |
|
| 230 | + * @param string $name |
|
| 231 | + * @param string $regex |
|
| 232 | + * |
|
| 233 | + * @return self |
|
| 234 | + */ |
|
| 235 | + |
|
| 236 | + public function setConstraint($name, $regex) |
|
| 237 | + { |
|
| 238 | + foreach ($this->routes as $route) { |
|
| 239 | + $pattern = $route->getPattern(); |
|
| 240 | + $initPos = strpos($pattern, "{" . $name); |
|
| 241 | + |
|
| 242 | + if ($initPos !== false) { |
|
| 243 | + $endPos = strpos($pattern, "}", $initPos); |
|
| 244 | + $newPattern = substr_replace($pattern, "{" . "$name:$regex" . "}", $initPos, $endPos - $initPos + 1); |
|
| 245 | + $route->setPatternWithoutReset($newPattern); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $this; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Define a constraint for a variable in all grouped routes pattern, but don't replace the |
|
| 254 | + * delimiter if one already exists. |
|
| 255 | + * |
|
| 256 | + * @param string $name |
|
| 257 | + * @param string $regex |
|
| 258 | + * |
|
| 259 | + * @return self |
|
| 260 | + */ |
|
| 261 | + |
|
| 262 | + public function setConstraintPreservingQuantifiers($name, $regex) |
|
| 263 | + { |
|
| 264 | + $wildcards = $this->routes[0]->getCollector()->getWildcards(); |
|
| 265 | + $quantifierPos = strpos($regex, "{") + strpos($regex, "+") + strpos($regex, "*"); |
|
| 266 | + $quantifier = substr($regex, $quantifierPos); |
|
| 267 | + $quantifierErased = $quantifierPos !== false ? substr($regex, 0, $quantifierPos) : null; |
|
| 268 | + $regex = isset($wildcards[$quantifierErased]) ? $wildcards[$quantifierErased] . $quantifier : $regex; |
|
| 269 | + |
|
| 270 | + return $this->setConstraint($name, $regex); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | 273 | } |
@@ -24,37 +24,37 @@ |
||
| 24 | 24 | abstract class EnhancerAbstractStrategy implements StrategyInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Key used to store the real route strategy on metadata. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - |
|
| 33 | - protected $metadataStrategyKey = "strategy"; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @inheritdoc |
|
| 37 | - * @throws BadRouteException |
|
| 38 | - */ |
|
| 39 | - |
|
| 40 | - public function call(Route $route) |
|
| 41 | - { |
|
| 42 | - if ($route->hasMetadata($this->metadataStrategyKey)) { |
|
| 43 | - $route->setStrategy($route->getMetadata($this->metadataStrategyKey)); |
|
| 44 | - } else $route->setStrategy(null); |
|
| 45 | - |
|
| 46 | - $this->enhance($route); |
|
| 47 | - |
|
| 48 | - return $route->call(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Manipulate route object before the dispatch. |
|
| 53 | - * |
|
| 54 | - * @param Route $route |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - |
|
| 58 | - abstract public function enhance(Route $route); |
|
| 27 | + /** |
|
| 28 | + * Key used to store the real route strategy on metadata. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + |
|
| 33 | + protected $metadataStrategyKey = "strategy"; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @inheritdoc |
|
| 37 | + * @throws BadRouteException |
|
| 38 | + */ |
|
| 39 | + |
|
| 40 | + public function call(Route $route) |
|
| 41 | + { |
|
| 42 | + if ($route->hasMetadata($this->metadataStrategyKey)) { |
|
| 43 | + $route->setStrategy($route->getMetadata($this->metadataStrategyKey)); |
|
| 44 | + } else $route->setStrategy(null); |
|
| 45 | + |
|
| 46 | + $this->enhance($route); |
|
| 47 | + |
|
| 48 | + return $route->call(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Manipulate route object before the dispatch. |
|
| 53 | + * |
|
| 54 | + * @param Route $route |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + |
|
| 58 | + abstract public function enhance(Route $route); |
|
| 59 | 59 | |
| 60 | 60 | } |
@@ -20,16 +20,16 @@ |
||
| 20 | 20 | interface MatcherAwareInterface |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param \Codeburner\Router\Matcher $matcher |
|
| 25 | - */ |
|
| 23 | + /** |
|
| 24 | + * @param \Codeburner\Router\Matcher $matcher |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - public function setMatcher(\Codeburner\Router\Matcher $matcher); |
|
| 27 | + public function setMatcher(\Codeburner\Router\Matcher $matcher); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return \Codeburner\Router\Matcher |
|
| 31 | - */ |
|
| 29 | + /** |
|
| 30 | + * @return \Codeburner\Router\Matcher |
|
| 31 | + */ |
|
| 32 | 32 | |
| 33 | - public function getMatcher(); |
|
| 33 | + public function getMatcher(); |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -19,13 +19,13 @@ |
||
| 19 | 19 | interface StrategyInterface |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Dispatch the matched route action. |
|
| 24 | - * |
|
| 25 | - * @param \Codeburner\Router\Route $route |
|
| 26 | - * @return mixed The response of request. |
|
| 27 | - */ |
|
| 22 | + /** |
|
| 23 | + * Dispatch the matched route action. |
|
| 24 | + * |
|
| 25 | + * @param \Codeburner\Router\Route $route |
|
| 26 | + * @return mixed The response of request. |
|
| 27 | + */ |
|
| 28 | 28 | |
| 29 | - public function call(\Codeburner\Router\Route $route); |
|
| 29 | + public function call(\Codeburner\Router\Route $route); |
|
| 30 | 30 | |
| 31 | 31 | } |