Parcourir la source

Inject generator configuration rather than access global state.

shalvah il y a 6 ans
Parent
commit
3aa1becf03

+ 19 - 10
src/Commands/GenerateDocumentation.php

@@ -2,6 +2,7 @@
 
 namespace Mpociot\ApiDoc\Commands;
 
+use Mpociot\ApiDoc\Tools\DocumentationConfig;
 use ReflectionClass;
 use ReflectionException;
 use Illuminate\Routing\Route;
@@ -34,6 +35,11 @@ class GenerateDocumentation extends Command
 
     private $routeMatcher;
 
+    /**
+     * @var DocumentationConfig
+     */
+    private $docConfig;
+
     public function __construct(RouteMatcher $routeMatcher)
     {
         parent::__construct();
@@ -47,20 +53,23 @@ class GenerateDocumentation extends Command
      */
     public function handle()
     {
+        $this->docConfig = new DocumentationConfig(config('apidoc'));
+        
         try {
             URL::forceRootUrl(config('app.url'));
         } catch (\Exception $e) {
             echo "Warning: Couldn't force base url as Lumen currently doesn't have the forceRootUrl method.\n";
             echo "You should probably double check URLs in your generated documentation.\n";
         }
-        $usingDingoRouter = strtolower(config('apidoc.router')) == 'dingo';
+        $usingDingoRouter = strtolower($this->docConfig->get('router')) == 'dingo';
+        $routes = $this->docConfig->get('routes');
         if ($usingDingoRouter) {
-            $routes = $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes'));
+            $routes = $this->routeMatcher->getDingoRoutesToBeDocumented($routes);
         } else {
-            $routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes'));
+            $routes = $this->routeMatcher->getLaravelRoutesToBeDocumented($routes);
         }
 
-        $generator = new Generator(config('apidoc.faker_seed'));
+        $generator = new Generator($this->docConfig);
         $parsedRoutes = $this->processRoutes($generator, $routes);
         $parsedRoutes = collect($parsedRoutes)->groupBy('group')
             ->sortBy(static function ($group) {
@@ -78,7 +87,7 @@ class GenerateDocumentation extends Command
      */
     private function writeMarkdown($parsedRoutes)
     {
-        $outputPath = config('apidoc.output');
+        $outputPath = $this->docConfig->get('output');
         $targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md';
         $compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md';
         $prependFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'prepend.md';
@@ -88,7 +97,7 @@ class GenerateDocumentation extends Command
             ->with('outputPath', ltrim($outputPath, 'public/'))
             ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection());
 
-        $settings = ['languages' => config('apidoc.example_languages')];
+        $settings = ['languages' => $this->docConfig->get('example_languages')];
         $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) use ($settings) {
             return $routeGroup->map(function ($route) use ($settings) {
                 if (count($route['cleanBodyParameters']) && ! isset($route['headers']['Content-Type'])) {
@@ -149,7 +158,7 @@ class GenerateDocumentation extends Command
             ->with('infoText', $infoText)
             ->with('prependMd', $prependFileContents)
             ->with('appendMd', $appendFileContents)
-            ->with('outputPath', config('apidoc.output'))
+            ->with('outputPath', $this->docConfig->get('output'))
             ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection())
             ->with('parsedRoutes', $parsedRouteOutput);
 
@@ -167,7 +176,7 @@ class GenerateDocumentation extends Command
             ->with('infoText', $infoText)
             ->with('prependMd', $prependFileContents)
             ->with('appendMd', $appendFileContents)
-            ->with('outputPath', config('apidoc.output'))
+            ->with('outputPath', $this->docConfig->get('output'))
             ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection())
             ->with('parsedRoutes', $parsedRouteOutput);
 
@@ -187,7 +196,7 @@ class GenerateDocumentation extends Command
             file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes));
         }
 
-        if ($logo = config('apidoc.logo')) {
+        if ($logo = $this->docConfig->get('logo')) {
             copy(
                 $logo,
                 $outputPath.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'logo.png'
@@ -280,6 +289,6 @@ class GenerateDocumentation extends Command
      */
     private function shouldGeneratePostmanCollection()
     {
-        return config('apidoc.postman.enabled', is_bool(config('apidoc.postman')) ? config('apidoc.postman') : false);
+        return $this->docConfig->get('postman.enabled', is_bool($this->docConfig->get('postman')) ? $this->docConfig->get('postman') : false);
     }
 }

+ 21 - 0
src/Tools/DocumentationConfig.php

@@ -0,0 +1,21 @@
+<?php
+
+
+namespace Mpociot\ApiDoc\Tools;
+
+
+class DocumentationConfig
+{
+
+    private $data;
+
+    public function __construct(array $config = [])
+    {
+        $this->data = $config;
+    }
+
+    public function get($key, $default = null)
+    {
+        return data_get($this->data, $key, $default);
+    }
+}

+ 8 - 8
src/Tools/Generator.php

@@ -15,14 +15,14 @@ class Generator
     use ParamHelpers;
 
     /**
-     * @var string The seed to be used with Faker.
-     * Useful when you want to always have the same fake output.
+     * @var DocumentationConfig
      */
-    private $fakerSeed = null;
+    private $config;
 
-    public function __construct(string $fakerSeed = null)
+    public function __construct(DocumentationConfig $config = null)
     {
-        $this->fakerSeed = $fakerSeed;
+        // If no config is injected, pull from global
+        $this->config = $config ?: new DocumentationConfig(config('apidoc'));
     }
 
     /**
@@ -297,7 +297,7 @@ class Generator
             }
         }
 
-        return config('apidoc.default_group', 'general');
+        return $this->config->get(('default_group'));
     }
 
     private function normalizeParameterType($type)
@@ -314,8 +314,8 @@ class Generator
     private function generateDummyValue(string $type)
     {
         $faker = Factory::create();
-        if ($this->fakerSeed) {
-            $faker->seed($this->fakerSeed);
+        if ($this->config->get('faker_seed')) {
+            $faker->seed($this->config->get('faker_seed'));
         }
         $fakeFactories = [
             'integer' => function () use ($faker) {

+ 2 - 1
tests/Unit/GeneratorTestCase.php

@@ -2,6 +2,7 @@
 
 namespace Mpociot\ApiDoc\Tests\Unit;
 
+use Mpociot\ApiDoc\Tools\DocumentationConfig;
 use Orchestra\Testbench\TestCase;
 use Mpociot\ApiDoc\Tools\Generator;
 use Illuminate\Support\Facades\Storage;
@@ -547,7 +548,7 @@ abstract class GeneratorTestCase extends TestCase
         // Examples should have different values
         $this->assertNotEquals(count($results), 1);
 
-        $generator = new Generator(12345);
+        $generator = new Generator(new DocumentationConfig(['faker_seed' => 12345]));
         $results = [];
         $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
         $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;