| @@ 11-75 (lines=65) @@ | ||
| 8 | /** |
|
| 9 | * The ErrorRouter class wraps a PSR-7 middleware for handling errors with the associated path. |
|
| 10 | */ |
|
| 11 | class ErrorRouter implements RouterInterface |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var string |
|
| 15 | */ |
|
| 16 | private $path; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var ErrorMiddlewareInterface |
|
| 20 | */ |
|
| 21 | private $middleware; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Whether the middleware must be enabled or not. |
|
| 25 | * |
|
| 26 | * @var ConditionInterface |
|
| 27 | */ |
|
| 28 | private $enableCondition; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @Important IfSet |
|
| 32 | * |
|
| 33 | * @param ErrorMiddlewareInterface|callable $middleware The PSR-7 middleware to call |
|
| 34 | * @param string $path The path to that middleware (defaults to /). |
|
| 35 | * @param ConditionInterface $enableCondition Whether the middleware must be enabled or not. |
|
| 36 | */ |
|
| 37 | public function __construct($middleware, $path = '/', ConditionInterface $enableCondition = null) |
|
| 38 | { |
|
| 39 | $this->path = $path; |
|
| 40 | $this->middleware = $middleware; |
|
| 41 | $this->enableCondition = $enableCondition; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * The path to that middleware (defaults to /). |
|
| 46 | * |
|
| 47 | * @return string |
|
| 48 | */ |
|
| 49 | public function getPath() |
|
| 50 | { |
|
| 51 | return $this->path; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * The PSR-7 middleware to call. |
|
| 56 | * |
|
| 57 | * @return ErrorMiddlewareInterface |
|
| 58 | */ |
|
| 59 | public function getMiddleware() |
|
| 60 | { |
|
| 61 | return $this->middleware; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * If this returns false, the router is skipped. |
|
| 66 | * |
|
| 67 | * @return bool |
|
| 68 | */ |
|
| 69 | public function isActive() |
|
| 70 | { |
|
| 71 | if ($this->enableCondition !== null && $this->enableCondition->isOk() === false) { |
|
| 72 | return false; |
|
| 73 | } else { |
|
| 74 | return true; |
|
| 75 | } |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ||
| @@ 11-75 (lines=65) @@ | ||
| 8 | /** |
|
| 9 | * The Router class wraps a PSR-7 middleware with the associated path. |
|
| 10 | */ |
|
| 11 | class Router implements RouterInterface |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var string |
|
| 15 | */ |
|
| 16 | private $path; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var MiddlewareInterface |
|
| 20 | */ |
|
| 21 | private $middleware; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Whether the middleware must be enabled or not. |
|
| 25 | * |
|
| 26 | * @var ConditionInterface |
|
| 27 | */ |
|
| 28 | private $enableCondition; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @Important IfSet |
|
| 32 | * |
|
| 33 | * @param MiddlewareInterface $middleware The PSR-7 middleware to call |
|
| 34 | * @param string $path The path to that middleware (defaults to /). |
|
| 35 | * @param ConditionInterface $enableCondition Whether the middleware must be enabled or not. |
|
| 36 | */ |
|
| 37 | public function __construct(MiddlewareInterface $middleware, $path = '/', ConditionInterface $enableCondition = null) |
|
| 38 | { |
|
| 39 | $this->path = $path; |
|
| 40 | $this->middleware = $middleware; |
|
| 41 | $this->enableCondition = $enableCondition; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * The path to that middleware (defaults to /). |
|
| 46 | * |
|
| 47 | * @return string |
|
| 48 | */ |
|
| 49 | public function getPath() |
|
| 50 | { |
|
| 51 | return $this->path; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * The PSR-7 middleware to call. |
|
| 56 | * |
|
| 57 | * @return MiddlewareInterface |
|
| 58 | */ |
|
| 59 | public function getMiddleware() |
|
| 60 | { |
|
| 61 | return $this->middleware; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * If this returns false, the router is skipped. |
|
| 66 | * |
|
| 67 | * @return bool |
|
| 68 | */ |
|
| 69 | public function isActive() |
|
| 70 | { |
|
| 71 | if ($this->enableCondition !== null && $this->enableCondition->isOk() === false) { |
|
| 72 | return false; |
|
| 73 | } else { |
|
| 74 | return true; |
|
| 75 | } |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ||