bearsunday /
BEAR.Resource
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\Resource; |
||
| 6 | |||
| 7 | use LogicException; |
||
| 8 | use ReflectionClass; |
||
| 9 | use ReflectionMethod; |
||
| 10 | |||
| 11 | use function array_shift; |
||
| 12 | use function assert; |
||
| 13 | use function class_exists; |
||
| 14 | use function explode; |
||
| 15 | use function implode; |
||
| 16 | use function is_string; |
||
| 17 | use function str_starts_with; |
||
| 18 | use function strtolower; |
||
| 19 | use function substr; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @psalm-import-type PackageMetadata from Types |
||
| 23 | * @psalm-import-type ResourceClassName from Types |
||
| 24 | */ |
||
| 25 | final class Meta |
||
| 26 | { |
||
| 27 | private const EXTRAS_VENDOR = 'vendor'; |
||
| 28 | private const EXTRAS_PACKAGE = 'package'; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | public $uri; |
||
| 32 | |||
| 33 | /** @var Options */ |
||
| 34 | public $options; |
||
| 35 | |||
| 36 | /** @var PackageMetadata */ |
||
| 37 | public $extras = []; |
||
| 38 | |||
| 39 | /** @param ResourceClassName $class */ |
||
| 40 | public function __construct(string $class) |
||
| 41 | { |
||
| 42 | $this->uri = $this->getUri($class); |
||
| 43 | $this->options = $this->getOptions($class); |
||
| 44 | } |
||
| 45 | |||
| 46 | private function getUri(string $class): string |
||
| 47 | { |
||
| 48 | /** @var list<string> $classPath */ |
||
| 49 | $classPath = explode('\\', $class); |
||
| 50 | // $class |
||
| 51 | $vendor = array_shift($classPath); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | assert(is_string($vendor)); |
||
| 53 | $this->extras[self::EXTRAS_VENDOR] = $vendor; |
||
| 54 | $package = array_shift($classPath); |
||
| 55 | assert(is_string($package)); |
||
| 56 | $this->extras[self::EXTRAS_PACKAGE] = $package; |
||
| 57 | array_shift($classPath); // "/Resource/" |
||
| 58 | $scheme = array_shift($classPath); |
||
| 59 | assert(is_string($scheme)); |
||
| 60 | |||
| 61 | return strtolower("{$scheme}://self/" . implode('/', $classPath)); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Return available resource request method |
||
| 66 | * |
||
| 67 | * @param ResourceClassName $class |
||
| 68 | */ |
||
| 69 | private function getOptions(string $class): Options |
||
| 70 | { |
||
| 71 | if (! class_exists($class)) { |
||
| 72 | throw new LogicException(); // @codeCoverageIgnore |
||
| 73 | } |
||
| 74 | |||
| 75 | $ref = new ReflectionClass($class); |
||
| 76 | $allows = $this->getAllows($ref->getMethods()); |
||
| 77 | $params = []; |
||
| 78 | foreach ($allows as $method) { |
||
| 79 | $params[] = $this->getParams($class, $method); |
||
| 80 | } |
||
| 81 | |||
| 82 | return new Options($allows, $params); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param ReflectionMethod[] $methods |
||
| 87 | * |
||
| 88 | * @return string[] |
||
| 89 | * @psalm-return list<string> |
||
| 90 | */ |
||
| 91 | private function getAllows(array $methods): array |
||
| 92 | { |
||
| 93 | $allows = []; |
||
| 94 | foreach ($methods as $method) { |
||
| 95 | $isRequestMethod = str_starts_with($method->name, 'on') && ! str_starts_with($method->name, 'onLink'); |
||
| 96 | if (! $isRequestMethod) { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $allows[] = strtolower(substr($method->name, 2)); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $allows; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** @param ResourceClassName $class */ |
||
| 107 | private function getParams(string $class, string $method): Params |
||
| 108 | { |
||
| 109 | $refMethod = new ReflectionMethod($class, 'on' . $method); |
||
| 110 | $parameters = $refMethod->getParameters(); |
||
| 111 | $optionalParams = $requiredParams = []; |
||
| 112 | foreach ($parameters as $parameter) { |
||
| 113 | $name = $parameter->name; |
||
| 114 | if ($parameter->isOptional()) { |
||
| 115 | $optionalParams[] = $name; |
||
| 116 | |||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $requiredParams[] = $name; |
||
| 121 | } |
||
| 122 | |||
| 123 | return new Params($method, $requiredParams, $optionalParams); |
||
| 124 | } |
||
| 125 | } |
||
| 126 |