Pārlūkot izejas kodu

Clean up GroupedEndpoints factory

shalvah 3 gadi atpakaļ
vecāks
revīzija
bfeaddb05c

+ 2 - 0
camel/Camel.php

@@ -20,6 +20,8 @@ class Camel
      * @var array<string, string>
      */
     public static array $groupFileNames = [];
+    public static string $cacheDir = ".scribe/endpoints.cache";
+    public static string $camelDir = ".scribe/endpoints";
 
     /**
      * Load endpoints from the Camel files into groups (arrays).

+ 3 - 12
src/Commands/GenerateDocumentation.php

@@ -5,21 +5,14 @@ namespace Knuckles\Scribe\Commands;
 use Illuminate\Console\Command;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\URL;
-use Illuminate\Support\Str;
 use Knuckles\Camel\Camel;
-use Knuckles\Camel\Extraction\ExtractedEndpointData;
 use Knuckles\Camel\Output\OutputEndpointData;
-use Knuckles\Scribe\Extracting\ApiDetails;
-use Knuckles\Scribe\Extracting\Extractor;
 use Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFactory;
 use Knuckles\Scribe\Matching\RouteMatcherInterface;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use Knuckles\Scribe\Tools\DocumentationConfig;
-use Knuckles\Scribe\Tools\ErrorHandlingUtils as e;
 use Knuckles\Scribe\Tools\Globals;
-use Knuckles\Scribe\Tools\Utils;
 use Knuckles\Scribe\Writing\Writer;
-use Symfony\Component\Yaml\Yaml;
 
 class GenerateDocumentation extends Command
 {
@@ -32,9 +25,6 @@ class GenerateDocumentation extends Command
 
     private DocumentationConfig $docConfig;
 
-    public static string $camelDir = ".scribe/endpoints";
-    public static string $cacheDir = ".scribe/endpoints.cache";
-
     private bool $shouldExtract;
 
     private bool $forcing;
@@ -47,7 +37,7 @@ class GenerateDocumentation extends Command
 
         $groupedEndpoints = $this->mergeUserDefinedEndpoints(
             $groupedEndpointsInstance->get(),
-            Camel::loadUserDefinedEndpoints(static::$camelDir)
+            Camel::loadUserDefinedEndpoints(Camel::$camelDir)
         );
 
         $writer = new Writer($this->docConfig);
@@ -69,7 +59,7 @@ class GenerateDocumentation extends Command
         return $this->shouldExtract;
     }
 
-    public function getDocConfig()
+    public function getDocConfig(): DocumentationConfig
     {
         return $this->docConfig;
     }
@@ -94,6 +84,7 @@ class GenerateDocumentation extends Command
             throw new \Exception("Can't use --force and --no-extraction together.");
         }
 
+        // Reset this map useful for tests)
         Camel::$groupFileNames = [];
     }
 

+ 1 - 1
src/GroupedEndpoints/GroupedEndpointsFactory.php

@@ -8,7 +8,7 @@ use Knuckles\Scribe\Matching\RouteMatcherInterface;
 
 class GroupedEndpointsFactory
 {
-    public static function make(GenerateDocumentation $command, RouteMatcherInterface $routeMatcher): GroupedEndpointsContract
+    public function make(GenerateDocumentation $command, RouteMatcherInterface $routeMatcher): GroupedEndpointsContract
     {
         if ($command->isForcing()) {
             return new GroupedEndpointsFromApp($command, $routeMatcher, false);

+ 7 - 6
src/GroupedEndpoints/GroupedEndpointsFromApp.php

@@ -13,6 +13,7 @@ use Knuckles\Scribe\Extracting\Extractor;
 use Knuckles\Scribe\Matching\MatchedRoute;
 use Knuckles\Scribe\Matching\RouteMatcherInterface;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
+use Knuckles\Scribe\Tools\DocumentationConfig;
 use Knuckles\Scribe\Tools\ErrorHandlingUtils as e;
 use Knuckles\Scribe\Tools\Utils as u;
 use Knuckles\Scribe\Tools\Utils;
@@ -23,10 +24,10 @@ use Symfony\Component\Yaml\Yaml;
 
 class GroupedEndpointsFromApp implements GroupedEndpointsContract
 {
-    private $command;
-    private $routeMatcher;
-    private $docConfig;
-    private $preserveUserChanges;
+    private GenerateDocumentation $command;
+    private RouteMatcherInterface $routeMatcher;
+    private DocumentationConfig $docConfig;
+    private bool $preserveUserChanges = true;
     private bool $encounteredErrors = false;
 
     public static string $camelDir;
@@ -41,8 +42,8 @@ class GroupedEndpointsFromApp implements GroupedEndpointsContract
         $this->docConfig = $command->getDocConfig();
         $this->preserveUserChanges = $preserveUserChanges;
 
-        static::$camelDir = GenerateDocumentation::$camelDir;
-        static::$cacheDir = GenerateDocumentation::$cacheDir;
+        static::$camelDir = Camel::$camelDir;
+        static::$cacheDir = Camel::$cacheDir;
     }
 
     public function get(): array

+ 5 - 4
src/GroupedEndpoints/GroupedEndpointsFromCamelDir.php

@@ -9,12 +9,13 @@ class GroupedEndpointsFromCamelDir implements GroupedEndpointsContract
 {
     public function get(): array
     {
-        if (!is_dir(GenerateDocumentation::$camelDir)) {
-            throw new \InvalidArgumentException("Can't use --no-extraction because there are no endpoints in the " .
-                GenerateDocumentation::$camelDir . " directory.");
+        if (!is_dir(Camel::$camelDir)) {
+            throw new \InvalidArgumentException(
+                "Can't use --no-extraction because there are no endpoints in the " . Camel::$camelDir . " directory."
+            );
         }
 
-        return Camel::loadEndpointsIntoGroups(GenerateDocumentation::$camelDir);
+        return Camel::loadEndpointsIntoGroups(Camel::$camelDir);
     }
 
     public function hasEncounteredErrors(): bool

+ 7 - 7
src/Writing/Writer.php

@@ -3,7 +3,7 @@
 namespace Knuckles\Scribe\Writing;
 
 use Illuminate\Support\Facades\Storage;
-use Knuckles\Scribe\Tools\ConsoleOutputUtils;
+use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use Knuckles\Scribe\Tools\DocumentationConfig;
 use Knuckles\Scribe\Tools\Utils;
 use Symfony\Component\Yaml\Yaml;
@@ -49,7 +49,7 @@ class Writer
     protected function writePostmanCollection(array $groups): void
     {
         if ($this->config->get('postman.enabled', true)) {
-            ConsoleOutputUtils::info('Generating Postman collection');
+            c::info('Generating Postman collection');
 
             $collection = $this->generatePostmanCollection($groups);
             if ($this->isStatic) {
@@ -60,14 +60,14 @@ class Writer
                 $collectionPath = 'storage/app/scribe/collection.json';
             }
 
-            ConsoleOutputUtils::success("Wrote Postman collection to: {$collectionPath}");
+            c::success("Wrote Postman collection to: {$collectionPath}");
         }
     }
 
     protected function writeOpenAPISpec(array $parsedRoutes): void
     {
         if ($this->config->get('openapi.enabled', false)) {
-            ConsoleOutputUtils::info('Generating OpenAPI specification');
+            c::info('Generating OpenAPI specification');
 
             $spec = $this->generateOpenAPISpec($parsedRoutes);
             if ($this->isStatic) {
@@ -78,7 +78,7 @@ class Writer
                 $specPath = 'storage/app/scribe/openapi.yaml';
             }
 
-            ConsoleOutputUtils::success("Wrote OpenAPI specification to: {$specPath}");
+            c::success("Wrote OpenAPI specification to: {$specPath}");
         }
     }
 
@@ -156,7 +156,7 @@ class Writer
 
     public function writeHtmlDocs(array $groupedEndpoints): void
     {
-        ConsoleOutputUtils::info('Writing HTML docs...');
+        c::info('Writing HTML docs...');
 
         // Then we convert them to HTML, and throw in the endpoints as well.
         /** @var HtmlWriter $writer */
@@ -167,7 +167,7 @@ class Writer
             $this->performFinalTasksForLaravelType();
         }
 
-        ConsoleOutputUtils::success("Wrote HTML documentation to: " . ($this->isStatic ? $this->staticTypeOutputPath : $this->laravelTypeOutputPath));
+        c::success("Wrote HTML docs to: " . rtrim($this->isStatic ? $this->staticTypeOutputPath : $this->laravelTypeOutputPath, '/').'/');
     }
 
 }