浏览代码

Switch to DTOs for writing HTML

shalvah 4 年之前
父节点
当前提交
838d95006f

+ 6 - 0
camel/Endpoint/EndpointData.php

@@ -85,6 +85,7 @@ class EndpointData extends BaseDTO
 
     public bool $showresponse = false;
     public ?string $boundUri;
+    public ?string $output;
 
     public function __construct(array $parameters = [])
     {
@@ -131,4 +132,9 @@ class EndpointData extends BaseDTO
     {
         return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
     }
+
+    public function endpointId()
+    {
+        return $this->methods[0].str_replace(['/', '?', '{', '}', ':'], '-', $this->uri);
+    }
 }

+ 1 - 1
resources/views/partials/group.blade.php

@@ -2,6 +2,6 @@
 {!! $groupDescription !!}
 
 @foreach($routes as $route)
-{!! $route['output'] !!}
+{!! $route->output !!}
 @endforeach
 

+ 1 - 1
src/Commands/GenerateDocumentation.php

@@ -76,7 +76,7 @@ class GenerateDocumentation extends Command
             ->groupBy('metadata.groupName')
             ->sortBy(static function ($group) {
                 /* @var $group Collection */
-                return $group->first()['metadata']['groupName'];
+                return $group->first()->metadata->groupName;
             }, SORT_NATURAL);
 
         $writer = new Writer($this->docConfig, $this->option('force'));

+ 12 - 11
src/Writing/Writer.php

@@ -6,6 +6,7 @@ use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
+use Knuckles\Camel\Endpoint\EndpointData;
 use Knuckles\Pastel\Pastel;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils;
 use Knuckles\Scribe\Tools\DocumentationConfig;
@@ -146,10 +147,10 @@ class Writer
     public function generateMarkdownOutputForEachRoute(Collection $parsedRoutes, array $settings): Collection
     {
         $routesWithOutput = $parsedRoutes->map(function (Collection $routeGroup) use ($settings) {
-            return $routeGroup->map(function (array $route) use ($settings) {
-                $hasRequestOptions = !empty($route['headers'])
-                    || !empty($route['cleanQueryParameters'])
-                    || !empty($route['cleanBodyParameters']);
+            return $routeGroup->map(function (EndpointData $endpointData) use ($settings) {
+                $hasRequestOptions = !empty($endpointData->headers)
+                    || !empty($endpointData->cleanQueryParameters)
+                    || !empty($endpointData->cleanBodyParameters);
                 // Needed for Try It Out
                 $auth = $settings['auth'];
                 if ($auth['in'] === 'bearer' || $auth['in'] === 'basic') {
@@ -160,16 +161,16 @@ class Writer
                     $auth['location'] = $auth['in'];
                     $auth['prefix'] = '';
                 }
-                $route['output'] = (string)view('scribe::partials.endpoint')
+                $endpointData->output = (string)view('scribe::partials.endpoint')
                     ->with('hasRequestOptions', $hasRequestOptions)
-                    ->with('route', $route)
-                    ->with('endpointId', $route['methods'][0].str_replace(['/', '?', '{', '}', ':'], '-', $route['uri']))
+                    ->with('route', $endpointData->toArray())
+                    ->with('endpointId', $endpointData->endpointId())
                     ->with('settings', $settings)
                     ->with('auth', $auth)
                     ->with('baseUrl', $this->baseUrl)
                     ->render();
 
-                return $route;
+                return $endpointData;
             });
         });
 
@@ -406,9 +407,9 @@ class Writer
                 }
             }
 
-            $groupDescription = Arr::first($routesInGroup, function ($route) {
-                    return $route['metadata']['groupDescription'] !== '';
-                })['metadata']['groupDescription'] ?? '';
+            $groupDescription = Arr::first($routesInGroup, function (EndpointData $endpointData) {
+                    return $endpointData->metadata->groupDescription !== '';
+                })->metadata->groupDescription ?? '';
 
             $groupMarkdown = view('scribe::partials.group')
                 ->with('groupName', $groupName)