Ver código fonte

Switch to clara for logging

shalvah 5 anos atrás
pai
commit
d6100fcf5f

+ 1 - 1
.travis.yml

@@ -9,7 +9,7 @@ matrix:
   fast_finish: true
   include:
     - php: 7.4
-      env: COMPOSER=dingo.composer.json
+      env: COMPOSER=composer.dingo.json
       name: "With Dingo router"
     - php: 7.4
       env: SETUP=lint

+ 3 - 2
dingo.composer.json → composer.dingo.json

@@ -26,7 +26,8 @@
         "mpociot/reflection-docblock": "^1.0.1",
         "nunomaduro/collision": "^3.0|^4.0",
         "ramsey/uuid": "^3.8|^4.0",
-        "symfony/var-exporter": "^4.0|^5.0"
+        "symfony/var-exporter": "^4.0|^5.0",
+        "shalvah/clara": "^2.5"
     },
     "require-dev": {
         "dingo/api": "^2.3",
@@ -59,7 +60,7 @@
             "providers": [
                 "Mpociot\\ApiDoc\\ApiDocGeneratorServiceProvider"
             ]
-        },
+        }
     },
     "config": {
         "preferred-install": "dist",

+ 1 - 0
composer.json

@@ -26,6 +26,7 @@
         "mpociot/reflection-docblock": "^1.0.1",
         "nunomaduro/collision": "^3.0|^4.0",
         "ramsey/uuid": "^3.8|^4.0",
+        "shalvah/clara": "^2.5",
         "symfony/var-exporter": "^4.0|^5.0"
     },
     "require-dev": {

+ 12 - 6
src/Commands/GenerateDocumentation.php

@@ -16,6 +16,7 @@ use Mpociot\ApiDoc\Writing\Writer;
 use Mpociot\Reflection\DocBlock;
 use ReflectionClass;
 use ReflectionException;
+use Shalvah\Clara\Clara;
 
 class GenerateDocumentation extends Command
 {
@@ -45,6 +46,11 @@ class GenerateDocumentation extends Command
      */
     private $baseUrl;
 
+    /**
+     * @var Clara
+     */
+    private $clara;
+
     /**
      * Execute the console command.
      *
@@ -57,6 +63,7 @@ class GenerateDocumentation extends Command
         // Using a global static variable here, so fuck off if you don't like it.
         // Also, the --verbose option is included with all Artisan commands.
         Flags::$shouldBeVerbose = $this->option('verbose');
+        $this->clara = clara('knuckleswtf/scribe', Flags::$shouldBeVerbose)->only();
 
         $this->docConfig = new DocumentationConfig(config('apidoc'));
         $this->baseUrl = $this->docConfig->get('base_url') ?? config('app.url');
@@ -75,7 +82,6 @@ class GenerateDocumentation extends Command
                 return $group->first()['metadata']['groupName'];
             }, SORT_NATURAL);
         $writer = new Writer(
-            $this,
             $this->docConfig,
             $this->option('force')
         );
@@ -102,25 +108,25 @@ class GenerateDocumentation extends Command
 
             $routeControllerAndMethod = Utils::getRouteClassAndMethodNames($route->getAction());
             if (! $this->isValidRoute($routeControllerAndMethod)) {
-                $this->warn(sprintf($messageFormat, 'Skipping invalid', $routeMethods, $routePath));
+                $this->clara->warn(sprintf($messageFormat, 'Skipping invalid', $routeMethods, $routePath));
                 continue;
             }
 
             if (! $this->doesControllerMethodExist($routeControllerAndMethod)) {
-                $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': Controller method does not exist.');
+                $this->clara->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': Controller method does not exist.');
                 continue;
             }
 
             if (! $this->isRouteVisibleForDocumentation($routeControllerAndMethod)) {
-                $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': @hideFromAPIDocumentation was specified.');
+                $this->clara->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': @hideFromAPIDocumentation was specified.');
                 continue;
             }
 
             try {
                 $parsedRoutes[] = $generator->processRoute($route, $routeItem->getRules());
-                $this->info(sprintf($messageFormat, 'Processed', $routeMethods, $routePath));
+                $this->clara->info(sprintf($messageFormat, 'Processed', $routeMethods, $routePath));
             } catch (\Exception $exception) {
-                $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . '- Exception ' . get_class($exception) . ' encountered : ' . $exception->getMessage());
+                $this->clara->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . '- Exception ' . get_class($exception) . ' encountered : ' . $exception->getMessage());
             }
         }
 

+ 13 - 3
src/Commands/RebuildDocumentation.php

@@ -4,7 +4,9 @@ namespace Mpociot\ApiDoc\Commands;
 
 use Illuminate\Console\Command;
 use Mpociot\ApiDoc\Tools\DocumentationConfig;
+use Mpociot\ApiDoc\Tools\Flags;
 use Mpociot\ApiDoc\Writing\Writer;
+use Shalvah\Clara\Clara;
 
 class RebuildDocumentation extends Command
 {
@@ -12,18 +14,26 @@ class RebuildDocumentation extends Command
 
     protected $description = 'Rebuild your API documentation from your markdown file.';
 
+    /**
+     * @var Clara
+     */
+    private $clara;
+
     public function handle()
     {
+        Flags::$shouldBeVerbose = $this->option('verbose');
+        $this->clara = clara('knuckleswtf/scribe',  Flags::$shouldBeVerbose)->only();
+
         $sourceOutputPath = 'resources/docs/source';
         if (! is_dir($sourceOutputPath)) {
-            $this->error('There is no existing documentation available at ' . $sourceOutputPath . '.');
+            $this->clara->error('There is no existing documentation available at ' . $sourceOutputPath . '.');
 
             return false;
         }
 
-        $this->info('Rebuilding API documentation from ' . $sourceOutputPath . '/index.md');
+        $this->clara->info('Rebuilding API documentation from ' . $sourceOutputPath . '/index.md');
 
-        $writer = new Writer($this, new DocumentationConfig(config('apidoc')));
+        $writer = new Writer(new DocumentationConfig(config('apidoc')));
         $writer->writeHtmlDocs();
     }
 }

+ 14 - 12
src/Writing/Writer.php

@@ -7,13 +7,15 @@ use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Storage;
 use Knuckles\Pastel\Pastel;
 use Mpociot\ApiDoc\Tools\DocumentationConfig;
+use Mpociot\ApiDoc\Tools\Flags;
+use Shalvah\Clara\Clara;
 
 class Writer
 {
     /**
-     * @var Command
+     * @var Clara
      */
-    protected $output;
+    protected $clara;
 
     /**
      * @var DocumentationConfig
@@ -55,13 +57,13 @@ class Writer
      */
     private $outputPath;
 
-    public function __construct(Command $output, DocumentationConfig $config = null, bool $forceIt = false)
+    public function __construct(DocumentationConfig $config = null, bool $forceIt = false)
     {
         // If no config is injected, pull from global
         $this->config = $config ?: new DocumentationConfig(config('apidoc'));
         $this->baseUrl = $this->config->get('base_url') ?? config('app.url');
         $this->forceIt = $forceIt;
-        $this->output = $output;
+        $this->clara = clara('knuckleswtf/scribe',  Flags::$shouldBeVerbose)->only();
         $this->shouldGeneratePostmanCollection = $this->config->get('postman.enabled', false);
         $this->pastel = new Pastel();
         $this->isStatic = $this->config->get('type') === 'static';
@@ -123,10 +125,10 @@ class Writer
                         $routeDocumentationChanged = (preg_match('/<!-- START_' . $route['id'] . ' -->(.*)<!-- END_' . $route['id'] . ' -->/is', $compareDocumentation, $lastDocWeGeneratedForThisRoute) && $lastDocWeGeneratedForThisRoute[1] !== $existingRouteDoc[1]);
                         if ($routeDocumentationChanged === false || $this->forceIt) {
                             if ($routeDocumentationChanged) {
-                                $this->output->warn('Discarded manual changes for route [' . implode(',', $route['methods']) . '] ' . $route['uri']);
+                                $this->clara->warn('Discarded manual changes for route [' . implode(',', $route['methods']) . '] ' . $route['uri']);
                             }
                         } else {
-                            $this->output->warn('Skipping modified route [' . implode(',', $route['methods']) . '] ' . $route['uri']);
+                            $this->clara->warn('Skipping modified route [' . implode(',', $route['methods']) . '] ' . $route['uri']);
                             $route['modified_output'] = $existingRouteDoc[0];
                         }
                     }
@@ -149,7 +151,7 @@ class Writer
             ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection)
             ->with('parsedRoutes', $parsedRouteOutput);
 
-        $this->output->info('Writing index.md to: ' . $this->sourceOutputPath);
+        $this->clara->info('Writing index.md to: ' . $this->sourceOutputPath);
 
         if (! is_dir($this->sourceOutputPath. '/source')) {
             mkdir($this->sourceOutputPath . '/source', 0777, true);
@@ -171,7 +173,7 @@ class Writer
 
         file_put_contents($compareFile, $compareMarkdown);
 
-        $this->output->info('Wrote index.md to: ' . $this->sourceOutputPath);
+        $this->clara->info('Wrote index.md to: ' . $this->sourceOutputPath);
     }
 
     public function generateMarkdownOutputForEachRoute(Collection $parsedRoutes, array $settings): Collection
@@ -201,7 +203,7 @@ class Writer
     protected function writePostmanCollection(Collection $parsedRoutes): void
     {
         if ($this->shouldGeneratePostmanCollection) {
-            $this->output->info('Generating Postman collection');
+            $this->clara->info('Generating Postman collection');
 
             $collection = $this->generatePostmanCollection($parsedRoutes);
             if ($this->isStatic) {
@@ -212,7 +214,7 @@ class Writer
                 $collectionPath = 'storage/app/apidoc/collection.json';
             }
 
-            $this->output->info("Wrote Postman collection to: {$collectionPath}");
+            $this->clara->success("Wrote Postman collection to: {$collectionPath}");
         }
     }
 
@@ -270,7 +272,7 @@ class Writer
 
     public function writeHtmlDocs(): void
     {
-        $this->output->info('Generating API HTML code');
+        $this->clara->info('Generating API HTML code');
 
         $this->pastel->generate($this->sourceOutputPath. '/source/index.md', 'public/docs');
 
@@ -278,6 +280,6 @@ class Writer
             $this->moveOutputFromPublicFolderToResourcesFolder();
         }
 
-        $this->output->info("Wrote HTML documentation to: {$this->outputPath}");
+        $this->clara->success("Wrote HTML documentation to: {$this->outputPath}");
     }
 }