|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ensi\LaravelOpenApiServerGenerator\Generators; |
| 4 | + |
| 5 | +use cebe\openapi\SpecObjectInterface; |
| 6 | +use RuntimeException; |
| 7 | +use stdClass; |
| 8 | + |
| 9 | +class ResourcesGenerator extends BaseGenerator implements GeneratorInterface |
| 10 | +{ |
| 11 | + public function generate(SpecObjectInterface $specObject): void |
| 12 | + { |
| 13 | + $resources = $this->extractResources($specObject); |
| 14 | + $this->createResourcesFiles($resources, $this->templatesManager->getTemplate('Resource.template')); |
| 15 | + } |
| 16 | + |
| 17 | + protected function extractResources(SpecObjectInterface $specObject): array |
| 18 | + { |
| 19 | + $replaceFrom = 'Controller'; |
| 20 | + $replaceTo = 'Resource'; |
| 21 | + |
| 22 | + $openApiData = $specObject->getSerializableData(); |
| 23 | + |
| 24 | + $resources = []; |
| 25 | + $paths = $openApiData->paths ?: []; |
| 26 | + foreach ($paths as $routes) { |
| 27 | + foreach ($routes as $route) { |
| 28 | + if (!empty($route->{'x-lg-skip-resource-generation'})) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if (empty($route->{'x-lg-handler'})) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + $response = $route->responses->{201} ?? $route->responses->{200} ?? null; |
| 37 | + if (!$response) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + $responseSchema = $response->content?->{'application/json'}?->schema ?? null; |
| 42 | + if (!$responseSchema) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + $handler = $this->routeHandlerParser->parse($route->{'x-lg-handler'}); |
| 47 | + |
| 48 | + try { |
| 49 | + $namespace = $this->getReplacedNamespace($handler->namespace, $replaceFrom, $replaceTo); |
| 50 | + $className = $responseSchema->{'x-lg-resource-class-name'} ?? $this->getReplacedClassName($handler->class, $replaceFrom, $replaceTo); |
| 51 | + } catch (RuntimeException) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + if (isset($resources["$namespace\\$className"])) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + $responseData = $responseSchema; |
| 60 | + |
| 61 | + $responseKey = $responseSchema->{'x-lg-resource-response-key'} ?? |
| 62 | + $this->options['resources']['response_key'] ?? |
| 63 | + null; |
| 64 | + if ($responseKey) { |
| 65 | + $responseKeyParts = explode('.', $responseKey); |
| 66 | + foreach ($responseKeyParts as $responseKeyPart) { |
| 67 | + $flag = false; |
| 68 | + do_with_all_of($responseData, function (stdClass $p) use (&$responseData, &$flag, $responseKeyPart) { |
| 69 | + if (std_object_has($p, 'properties')) { |
| 70 | + if (std_object_has($p->properties, $responseKeyPart)) { |
| 71 | + $responseData = $p->properties->$responseKeyPart; |
| 72 | + $flag = true; |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + if (!$flag) { |
| 77 | + $responseData = null; |
| 78 | + |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (!$responseData) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $properties = $this->convertToString($this->getProperties($responseData)); |
| 89 | + |
| 90 | + if (empty($properties)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + $resources["$namespace\\$className"] = compact('className', 'namespace', 'properties'); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $resources; |
| 99 | + } |
| 100 | + |
| 101 | + protected function createResourcesFiles(array $resources, string $template): void |
| 102 | + { |
| 103 | + foreach ($resources as ['className' => $className, 'namespace' => $namespace, 'properties' => $properties]) { |
| 104 | + $filePath = $this->getNamespacedFilePath($className, $namespace); |
| 105 | + if ($this->filesystem->exists($filePath)) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $this->filesystem->put( |
| 110 | + $filePath, |
| 111 | + $this->replacePlaceholders($template, [ |
| 112 | + '{{ namespace }}' => $namespace, |
| 113 | + '{{ className }}' => $className, |
| 114 | + '{{ properties }}' => $properties, |
| 115 | + ]) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private function getProperties(stdClass $object): array |
| 121 | + { |
| 122 | + $properties = []; |
| 123 | + |
| 124 | + do_with_all_of($object, function (stdClass $p) use (&$properties) { |
| 125 | + if (std_object_has($p, 'properties')) { |
| 126 | + $properties = array_merge($properties, array_keys(get_object_vars($p->properties))); |
| 127 | + } |
| 128 | + |
| 129 | + if (std_object_has($p, 'items')) { |
| 130 | + $properties = array_merge($properties, $this->getProperties($p->items)); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + return $properties; |
| 135 | + } |
| 136 | + |
| 137 | + private function convertToString(array $properties): string |
| 138 | + { |
| 139 | + $propertyStrings = []; |
| 140 | + |
| 141 | + foreach ($properties as $property) { |
| 142 | + $propertyStrings[] = "'$property' => \$this->$property,"; |
| 143 | + } |
| 144 | + |
| 145 | + return implode("\n ", $propertyStrings); |
| 146 | + } |
| 147 | +} |
0 commit comments