|
5 | 5 | namespace Yiisoft\Middleware\Dispatcher;
|
6 | 6 |
|
7 | 7 | use InvalidArgumentException;
|
| 8 | +use Psr\Http\Server\MiddlewareInterface; |
| 9 | +use Yiisoft\FriendlyException\FriendlyExceptionInterface; |
8 | 10 |
|
9 | 11 | use function get_class;
|
10 | 12 | use function is_array;
|
11 | 13 | use function is_object;
|
12 | 14 | use function is_string;
|
13 | 15 |
|
14 |
| -final class InvalidMiddlewareDefinitionException extends InvalidArgumentException |
| 16 | +final class InvalidMiddlewareDefinitionException extends InvalidArgumentException implements FriendlyExceptionInterface |
15 | 17 | {
|
16 | 18 | /**
|
17 |
| - * @param array|callable|string $middlewareDefinition |
| 19 | + * @var mixed |
| 20 | + */ |
| 21 | + private $definition; |
| 22 | + private ?string $definitionString; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param mixed $middlewareDefinition |
18 | 26 | */
|
19 | 27 | public function __construct($middlewareDefinition)
|
20 | 28 | {
|
| 29 | + $this->definition = $middlewareDefinition; |
| 30 | + $this->definitionString = $this->convertDefinitionToString($middlewareDefinition); |
| 31 | + |
21 | 32 | $message = 'Parameter should be either PSR middleware class name or a callable.';
|
22 | 33 |
|
23 |
| - $definitionString = $this->convertDefinitionToString($middlewareDefinition); |
24 |
| - if ($definitionString !== null) { |
25 |
| - $message .= ' Got ' . $definitionString . '.'; |
| 34 | + if ($this->definitionString !== null) { |
| 35 | + $message .= ' Got ' . $this->definitionString . '.'; |
26 | 36 | }
|
27 | 37 |
|
28 | 38 | parent::__construct($message);
|
29 | 39 | }
|
30 | 40 |
|
| 41 | + public function getName(): string |
| 42 | + { |
| 43 | + return 'Invalid middleware definition'; |
| 44 | + } |
| 45 | + |
| 46 | + public function getSolution(): ?string |
| 47 | + { |
| 48 | + $solution = []; |
| 49 | + |
| 50 | + if ($this->definitionString !== null) { |
| 51 | + $solution[] = <<<SOLUTION |
| 52 | + ## Got definition value |
| 53 | +
|
| 54 | + `{$this->definitionString}` |
| 55 | + SOLUTION; |
| 56 | + } |
| 57 | + |
| 58 | + $suggestion = $this->generateSuggestion(); |
| 59 | + if ($suggestion !== null) { |
| 60 | + $solution[] = '## Suggestion'; |
| 61 | + $solution[] = $suggestion; |
| 62 | + } |
| 63 | + |
| 64 | + $solution[] = <<<SOLUTION |
| 65 | + ## Middleware definition examples |
| 66 | +
|
| 67 | + - PSR middleware class name: `Yiisoft\Session\SessionMiddleware::class`. |
| 68 | + - Action in controller: `[App\Backend\UserController::class, 'index']`. |
| 69 | +
|
| 70 | + ## Related links |
| 71 | +
|
| 72 | + - [Callable PHP documentation](https://www.php.net/manual/language.types.callable.php) |
| 73 | + SOLUTION; |
| 74 | + |
| 75 | + return implode("\n\n", $solution); |
| 76 | + } |
| 77 | + |
| 78 | + private function generateSuggestion(): ?string |
| 79 | + { |
| 80 | + if ($this->isControllerWithNonExistAction()) { |
| 81 | + return <<<SOLUTION |
| 82 | + Class `{$this->definition[0]}` exists, but does not contain method `{$this->definition[1]}()`. |
| 83 | +
|
| 84 | + Try adding `{$this->definition[1]}()` action to `{$this->definition[0]}` controller: |
| 85 | +
|
| 86 | + ```php |
| 87 | + public function {$this->definition[1]}(): ResponseInterface |
| 88 | + { |
| 89 | + // TODO: Implement your action |
| 90 | + } |
| 91 | + ``` |
| 92 | + SOLUTION; |
| 93 | + } |
| 94 | + |
| 95 | + if ($this->isNotMiddlewareClassName()) { |
| 96 | + return sprintf( |
| 97 | + 'Class `%s` exists, but does not implement `%s`.', |
| 98 | + $this->definition, |
| 99 | + MiddlewareInterface::class |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + if ($this->isStringNotClassName()) { |
| 104 | + return sprintf( |
| 105 | + 'Class `%s` not found. It may be needed to install a package with this middleware.', |
| 106 | + $this->definition |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @psalm-assert-if-true string $this->definition |
| 115 | + */ |
| 116 | + private function isStringNotClassName(): bool |
| 117 | + { |
| 118 | + return is_string($this->definition) |
| 119 | + && !class_exists($this->definition); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @psalm-assert-if-true class-string $this->definition |
| 124 | + */ |
| 125 | + private function isNotMiddlewareClassName(): bool |
| 126 | + { |
| 127 | + return is_string($this->definition) |
| 128 | + && class_exists($this->definition); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @psalm-assert-if-true array{0:class-string,1:string} $this->definition |
| 133 | + */ |
| 134 | + private function isControllerWithNonExistAction(): bool |
| 135 | + { |
| 136 | + return is_array($this->definition) |
| 137 | + && array_keys($this->definition) === [0, 1] |
| 138 | + && is_string($this->definition[0]) |
| 139 | + && class_exists($this->definition[0]); |
| 140 | + } |
| 141 | + |
31 | 142 | /**
|
32 | 143 | * @param mixed $middlewareDefinition
|
33 | 144 | */
|
|
0 commit comments