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