|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ensi\LaravelOpenApiServerGenerator\Generators; |
| 4 | + |
| 5 | +use cebe\openapi\SpecObjectInterface; |
| 6 | +use Ensi\LaravelOpenApiServerGenerator\DTO\ParsedRouteHandler; |
| 7 | +use InvalidArgumentException; |
| 8 | +use RuntimeException; |
| 9 | +use stdClass; |
| 10 | + |
| 11 | +class PoliciesGenerator extends BaseGenerator implements GeneratorInterface |
| 12 | +{ |
| 13 | + public function generate(SpecObjectInterface $specObject): void |
| 14 | + { |
| 15 | + $namespaceData = $this->options['policies']['namespace'] ?? null; |
| 16 | + if (!is_array($namespaceData)) { |
| 17 | + throw new InvalidArgumentException("PoliciesGenerator must be configured with array as 'namespace'"); |
| 18 | + } |
| 19 | + |
| 20 | + $policies = $this->extractPolicies($specObject, $namespaceData); |
| 21 | + $this->createPoliciesFiles($policies, $this->templatesManager->getTemplate('Policy.template')); |
| 22 | + } |
| 23 | + |
| 24 | + protected function extractPolicies(SpecObjectInterface $specObject, array $namespaceData): array |
| 25 | + { |
| 26 | + $replaceFromNamespace = array_keys($namespaceData)[0]; |
| 27 | + $replaceToNamespace = array_values($namespaceData)[0]; |
| 28 | + |
| 29 | + $openApiData = $specObject->getSerializableData(); |
| 30 | + |
| 31 | + $policies = []; |
| 32 | + $paths = $openApiData->paths ?: []; |
| 33 | + foreach ($paths as $routes) { |
| 34 | + foreach ($routes as $route) { |
| 35 | + if (!$this->routeValidation($route)) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + $handler = $this->routeHandlerParser->parse($route->{'x-lg-handler'}); |
| 40 | + if (!$this->handlerValidation($handler)) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + $namespace = $this->getReplacedNamespace( |
| 46 | + $handler->namespace, |
| 47 | + $replaceFromNamespace, |
| 48 | + $replaceToNamespace |
| 49 | + ); |
| 50 | + } catch (RuntimeException) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $className = $handler->class . 'Policy'; |
| 55 | + $methods = [$handler->method]; |
| 56 | + |
| 57 | + if (isset($policies["$namespace\\$className"])) { |
| 58 | + $policies["$namespace\\$className"]['methods'][] = $methods[0]; |
| 59 | + } else { |
| 60 | + $policies["$namespace\\$className"] = compact('className', 'namespace', 'methods'); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $policies; |
| 66 | + } |
| 67 | + |
| 68 | + protected function createPoliciesFiles(array $policies, string $template): void |
| 69 | + { |
| 70 | + foreach ($policies as ['className' => $className, 'namespace' => $namespace, 'methods' => $methods]) { |
| 71 | + $filePath = $this->getNamespacedFilePath($className, $namespace); |
| 72 | + if ($this->filesystem->exists($filePath)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + $this->filesystem->put( |
| 77 | + $filePath, |
| 78 | + $this->replacePlaceholders($template, [ |
| 79 | + '{{ namespace }}' => $namespace, |
| 80 | + '{{ className }}' => $className, |
| 81 | + '{{ methods }}' => $this->convertMethodsToString($methods), |
| 82 | + ]) |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private function routeValidation(stdClass $route): bool |
| 88 | + { |
| 89 | + return match (true) { |
| 90 | + !empty($route->{'x-lg-skip-policy-generation'}), |
| 91 | + empty($route->{'x-lg-handler'}), |
| 92 | + empty($route->responses->{403}) => false, |
| 93 | + default => true |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + private function handlerValidation(ParsedRouteHandler $handler): bool |
| 98 | + { |
| 99 | + return match (true) { |
| 100 | + empty($handler->namespace), |
| 101 | + empty($handler->class), |
| 102 | + empty($handler->method) => false, |
| 103 | + default => true |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + private function convertMethodsToString(array $methods): string |
| 108 | + { |
| 109 | + $methodsStrings = []; |
| 110 | + |
| 111 | + foreach ($methods as $method) { |
| 112 | + $methodsStrings[] = $this->replacePlaceholders( |
| 113 | + $this->templatesManager->getTemplate('PolicyGate.template'), |
| 114 | + ['{{ method }}' => $method] |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + return implode("\n\n ", $methodsStrings); |
| 119 | + } |
| 120 | +} |
0 commit comments