1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BEAR\Resource; |
||
6 | |||
7 | use BEAR\Resource\Annotation\OptionsBody; |
||
8 | use Override; |
||
9 | use ReflectionClass; |
||
10 | use ReflectionMethod; |
||
11 | |||
12 | use function implode; |
||
13 | use function in_array; |
||
14 | use function json_encode; |
||
15 | use function strtoupper; |
||
16 | use function substr; |
||
17 | |||
18 | use const JSON_PRETTY_PRINT; |
||
19 | use const JSON_UNESCAPED_SLASHES; |
||
20 | use const PHP_EOL; |
||
21 | |||
22 | /** |
||
23 | * RFC2616 OPTIONS method renderer |
||
24 | * |
||
25 | * Set resource request information to `headers` and `view` in ResourceObject. |
||
26 | * |
||
27 | * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html |
||
28 | * @see /docs/options/README.md |
||
29 | */ |
||
30 | final class OptionsRenderer implements RenderInterface |
||
31 | { |
||
32 | /** @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ |
||
33 | public function __construct( |
||
34 | private readonly OptionsMethods $optionsMethod, |
||
35 | #[OptionsBody] |
||
36 | private readonly bool $optionsBody = true, |
||
37 | ) { |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | #[Override] |
||
44 | public function render(ResourceObject $ro) |
||
45 | { |
||
46 | $ro->headers['Content-Type'] = 'application/json'; |
||
47 | $allows = $this->getAllows((new ReflectionClass($ro))->getMethods()); |
||
48 | $ro->headers['Allow'] = implode(', ', $allows); |
||
49 | $ro->view = $this->optionsBody ? (string) json_encode($this->getEntityBody($ro, $allows), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL : ''; |
||
50 | |||
51 | return $ro->view; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Return allowed methods |
||
56 | * |
||
57 | * @param ReflectionMethod[] $methods |
||
58 | * |
||
59 | * @return string[] |
||
60 | * @psalm-return list<string> |
||
61 | */ |
||
62 | private function getAllows(array $methods): array |
||
63 | { |
||
64 | $allows = []; |
||
65 | foreach ($methods as $method) { |
||
66 | if (! in_array($method->name, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) { |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | $allows[] = strtoupper(substr($method->name, 2)); |
||
71 | } |
||
72 | |||
73 | return $allows; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Return OPTIONS entity body |
||
78 | * |
||
79 | * @param list<string> $allows |
||
0 ignored issues
–
show
|
|||
80 | * |
||
81 | * @return array<string, array<array<mixed>|string>> |
||
82 | */ |
||
83 | private function getEntityBody(ResourceObject $ro, array $allows): array |
||
84 | { |
||
85 | $mehtodList = []; |
||
86 | foreach ($allows as $method) { |
||
87 | $mehtodList[$method] = ($this->optionsMethod)($ro, $method); |
||
88 | } |
||
89 | |||
90 | return $mehtodList; |
||
91 | } |
||
92 | } |
||
93 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths