Skip to content

Commit

Permalink
Clean up GroupedEndpoints factory
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jul 5, 2021
1 parent 30e4850 commit bfeaddb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
2 changes: 2 additions & 0 deletions camel/Camel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
15 changes: 3 additions & 12 deletions src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@
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
{
Expand All @@ -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;
Expand All @@ -47,7 +37,7 @@ public function handle(RouteMatcherInterface $routeMatcher, GroupedEndpointsFact

$groupedEndpoints = $this->mergeUserDefinedEndpoints(
$groupedEndpointsInstance->get(),
Camel::loadUserDefinedEndpoints(static::$camelDir)
Camel::loadUserDefinedEndpoints(Camel::$camelDir)
);

$writer = new Writer($this->docConfig);
Expand All @@ -69,7 +59,7 @@ public function shouldExtract(): bool
return $this->shouldExtract;
}

public function getDocConfig()
public function getDocConfig(): DocumentationConfig
{
return $this->docConfig;
}
Expand All @@ -94,6 +84,7 @@ public function bootstrap(): void
throw new \Exception("Can't use --force and --no-extraction together.");
}

// Reset this map useful for tests)
Camel::$groupFileNames = [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/GroupedEndpoints/GroupedEndpointsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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);
Expand Down
13 changes: 7 additions & 6 deletions src/GroupedEndpoints/GroupedEndpointsFromApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -23,10 +24,10 @@

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;
Expand All @@ -41,8 +42,8 @@ public function __construct(GenerateDocumentation $command, RouteMatcherInterfac
$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
Expand Down
9 changes: 5 additions & 4 deletions src/GroupedEndpoints/GroupedEndpointsFromCamelDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +49,7 @@ public function writeDocs(array $groupedEndpoints)
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) {
Expand All @@ -60,14 +60,14 @@ protected function writePostmanCollection(array $groups): void
$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) {
Expand All @@ -78,7 +78,7 @@ protected function writeOpenAPISpec(array $parsedRoutes): void
$specPath = 'storage/app/scribe/openapi.yaml';
}

ConsoleOutputUtils::success("Wrote OpenAPI specification to: {$specPath}");
c::success("Wrote OpenAPI specification to: {$specPath}");
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ protected function performFinalTasksForLaravelType(): void

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 */
Expand All @@ -167,7 +167,7 @@ public function writeHtmlDocs(array $groupedEndpoints): void
$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, '/').'/');
}

}

0 comments on commit bfeaddb

Please sign in to comment.