Browse Source

ResponseFileStrategy. Uses a file payload as response. Recommended for large responses.
@responseFile /apidoc_responses/search.json

Gabriel Peixoto 6 years ago
parent
commit
7532a5ab36

+ 2 - 0
src/Tools/ResponseResolver.php

@@ -3,6 +3,7 @@
 namespace Mpociot\ApiDoc\Tools;
 
 use Illuminate\Routing\Route;
+use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseFileStrategy;
 use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseTagStrategy;
 use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseCallStrategy;
 use Mpociot\ApiDoc\Tools\ResponseStrategies\TransformerTagsStrategy;
@@ -12,6 +13,7 @@ class ResponseResolver
     public static $strategies = [
         ResponseTagStrategy::class,
         TransformerTagsStrategy::class,
+        ResponseFileStrategy::class,
         ResponseCallStrategy::class,
     ];
 

+ 39 - 0
src/Tools/ResponseStrategies/ResponseFileStrategy.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace Mpociot\ApiDoc\Tools\ResponseStrategies;
+
+use Illuminate\Routing\Route;
+use Mpociot\Reflection\DocBlock\Tag;
+
+/**
+ * Get a response from from a file in the docblock ( @responseFile ).
+ */
+class ResponseFileStrategy
+{
+    public function __invoke(Route $route, array $tags, array $routeProps)
+    {
+        return $this->getFileResponse($tags);
+    }
+
+    /**
+     * Get the response from the file if available.
+     *
+     * @param array $tags
+     *
+     * @return mixed
+     */
+    protected function getFileResponse(array $tags)
+    {
+        $responseTags = array_filter($tags, function ($tag) {
+            return $tag instanceof Tag && strtolower($tag->getName()) == 'responsefile';
+        });
+        if (empty($responseTags)) {
+            return;
+        }
+        $responseTag = array_first($responseTags);
+
+        $json = json_decode(file_get_contents(storage_path($responseTag->getContent()), true), true);
+
+        return response()->json($json);
+    }
+}