|
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
|
|
|
* @psalm-import-type OptionsEntityBody from Types |
|
30
|
|
|
*/ |
|
31
|
|
|
final class OptionsRenderer implements RenderInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @SuppressWarnings(PHPMD.BooleanArgumentFlag) $optionsBody is configuration flag, not behavior control */ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
private readonly OptionsMethods $optionsMethod, |
|
36
|
|
|
#[OptionsBody] |
|
37
|
|
|
private readonly bool $optionsBody = true, |
|
38
|
|
|
) { |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
#[Override] |
|
45
|
|
|
public function render(ResourceObject $ro) |
|
46
|
|
|
{ |
|
47
|
|
|
$ro->headers['Content-Type'] = 'application/json'; |
|
48
|
|
|
$allows = $this->getAllows((new ReflectionClass($ro))->getMethods()); |
|
49
|
|
|
$ro->headers['Allow'] = implode(', ', $allows); |
|
50
|
|
|
$ro->view = $this->optionsBody ? (string) json_encode($this->getEntityBody($ro, $allows), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL : ''; |
|
51
|
|
|
|
|
52
|
|
|
return $ro->view; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return allowed methods |
|
57
|
|
|
* |
|
58
|
|
|
* @param ReflectionMethod[] $methods |
|
59
|
|
|
* |
|
60
|
|
|
* @return string[] |
|
61
|
|
|
* @psalm-return list<string> |
|
62
|
|
|
*/ |
|
63
|
|
|
private function getAllows(array $methods): array |
|
64
|
|
|
{ |
|
65
|
|
|
$allows = []; |
|
66
|
|
|
foreach ($methods as $method) { |
|
67
|
|
|
if (! in_array($method->name, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$allows[] = strtoupper(substr($method->name, 2)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $allows; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return OPTIONS entity body |
|
79
|
|
|
* |
|
80
|
|
|
* @param list<string> $allows |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return OptionsEntityBody |
|
|
|
|
|
|
83
|
|
|
*/ |
|
84
|
|
|
private function getEntityBody(ResourceObject $ro, array $allows): array |
|
85
|
|
|
{ |
|
86
|
|
|
$mehtodList = []; |
|
87
|
|
|
foreach ($allows as $method) { |
|
88
|
|
|
$mehtodList[$method] = ($this->optionsMethod)($ro, $method); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $mehtodList; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
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