Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions etc/qa/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
<arg name="cache" value=".phpcs.cache" /> <!-- cache the results and don't commit them -->
<arg value="np" /> <!-- n = ignore warnings, p = show progress -->

<file>../../src/Generator/Client.php</file>
<!-- <file>../../src</file>-->
<!-- <file>../../tests</file>-->
<file>../../src</file>
<file>../../tests</file>

<rule ref="WyriHaximus-OSS" />
</ruleset>
11 changes: 7 additions & 4 deletions src/Gatherer/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function array_key_exists;
use function in_array;
use function is_array;
use function property_exists;

final class Schema
{
Expand All @@ -19,8 +20,8 @@ public static function gather(
baseSchema $schema,
SchemaRegistry $schemaRegistry,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema {
$className = Utils::fixKeyword($className);
$isArray = $schema->type === 'array';
$className = Utils::fixKeyword($className);
$isArray = $schema->type === 'array';
$properties = [];
$example = [];

Expand All @@ -43,9 +44,11 @@ public static function gather(
break;
}

if (property_exists($schema, $examplePropertyName) && is_array($schema->$examplePropertyName) && array_key_exists($gatheredProperty->sourceName, $schema->$examplePropertyName)) {
$example[$gatheredProperty->sourceName] = $schema->$examplePropertyName[$gatheredProperty->sourceName];
if (! property_exists($schema, $examplePropertyName) || ! is_array($schema->$examplePropertyName) || ! array_key_exists($gatheredProperty->sourceName, $schema->$examplePropertyName)) {
continue;
}

$example[$gatheredProperty->sourceName] = $schema->$examplePropertyName[$gatheredProperty->sourceName];
}

$example[$gatheredProperty->sourceName] = $gatheredProperty->exampleData;
Expand Down
47 changes: 37 additions & 10 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\Step;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
use DivineOmega\CliProgressBar\ProgressBar;
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use Psr\Log\LoggerInterface;
use RuntimeException;

use function array_filter;
Expand Down Expand Up @@ -129,7 +127,7 @@ public function __construct(
$this->state->specHash === $this->currentSpecHash &&
(static function (string $root, Files $files, string ...$additionalFiles): bool {
foreach ($additionalFiles as $additionalFile) {
if ($files->has($additionalFile) && (!file_exists($root . $additionalFile) || $files->get($additionalFile)->hash !== md5(file_get_contents($root . $additionalFile)))) {
if ($files->has($additionalFile) && (! file_exists($root . $additionalFile) || $files->get($additionalFile)->hash !== md5(file_get_contents($root . $additionalFile)))) {
echo $additionalFile, PHP_EOL;

return false;
Expand Down Expand Up @@ -255,6 +253,7 @@ private function all(string $namespace, string $namespaceTest, string $configura
$this->statusOutput->advanceStep('gathering_schemas');
}
}

$this->statusOutput->markStepDone('gathering_schemas');

$webHooks = [];
Expand All @@ -266,9 +265,11 @@ private function all(string $namespace, string $namespaceTest, string $configura
$webHooks[$webHookje->event] = [];
}

$webHooks[$webHookje->event][] = $webHookje;$this->statusOutput->advanceStep('gathering_webhooks');
$webHooks[$webHookje->event][] = $webHookje;
$this->statusOutput->advanceStep('gathering_webhooks');
}
}

$this->statusOutput->markStepDone('gathering_webhooks');

$paths = [];
Expand All @@ -285,10 +286,11 @@ private function all(string $namespace, string $namespaceTest, string $configura
continue;
}

$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $this->configuration->voter);
$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $this->configuration->voter);
$webHooks[$webHookje->event][] = $webHookje;
$this->statusOutput->advanceStep('gathering_paths');
}

$this->statusOutput->markStepDone('gathering_paths');
}

Expand Down Expand Up @@ -316,8 +318,8 @@ private function all(string $namespace, string $namespaceTest, string $configura

private function oneClient(string $namespace, string $namespaceTest, string $configurationLocation, SchemaRegistry $schemaRegistry, ThrowableSchema $throwableSchemaRegistry, array $schemas, array $paths, array $webHooks)
{
$hydrators = [];
$operations = [];
$hydrators = [];
$operations = [];
$this->statusOutput->itemForStep('generating_operations', count($paths));
foreach ($paths as $path) {
$hydrators[] = $path->hydrator;
Expand Down Expand Up @@ -345,6 +347,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con

$this->statusOutput->advanceStep('generating_operations');
}

$this->statusOutput->markStepDone('generating_operations');

$unknownSchemaCount = 0;
Expand All @@ -357,6 +360,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
$this->statusOutput->advanceStep('gathering_unknown_schemas');
}
}

$this->statusOutput->markStepDone('gathering_unknown_schemas');

$this->statusOutput->itemForStep('generating_schemas', count($schemas));
Expand All @@ -375,20 +379,24 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
$schema,
);
}

$this->statusOutput->advanceStep('generating_schemas');
}

$this->statusOutput->markStepDone('generating_schemas');

$client = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Client::gather($this->spec, ...$paths);

$this->statusOutput->markStepDone('generating_clientinterface');

yield from ClientInterface::generate(
$this->configuration->destination->source . DIRECTORY_SEPARATOR,
$namespace,
$operations,
);

$this->statusOutput->markStepDone('generating_client');

yield from Client::generate(
$this->configuration->destination->source . DIRECTORY_SEPARATOR,
$namespace,
Expand All @@ -410,11 +418,14 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
$schemaRegistry,
...$webHook,
);

$this->statusOutput->advanceStep('generating_webhooks');
}

$this->statusOutput->markStepDone('generating_webhooks');

$this->statusOutput->markStepDone('generating_webhooks_entry_point');

yield from WebHooks::generate($this->configuration->destination->source . DIRECTORY_SEPARATOR, $namespace, $webHooksHydrators, $webHooks);

$this->statusOutput->itemForStep('generating_hydrators', count($hydrators));
Expand All @@ -423,9 +434,11 @@ private function oneClient(string $namespace, string $namespaceTest, string $con

$this->statusOutput->advanceStep('generating_hydrators');
}

$this->statusOutput->markStepDone('generating_hydrators');

$this->statusOutput->markStepDone('generating_hydrators_entry_point');

yield from Hydrators::generate($this->configuration->destination->source . DIRECTORY_SEPARATOR, $namespace, ...$hydrators);

$this->statusOutput->markStepDone('generating_templated_files');
Expand All @@ -447,9 +460,9 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
*/
private function subSplitClient(string $namespace, string $namespaceTest, string $configurationLocation, SchemaRegistry $schemaRegistry, ThrowableSchema $throwableSchemaRegistry, array $schemas, array $paths, array $webHooks)
{
$splits = [];
$hydrators = [];
$operations = [];
$splits = [];
$hydrators = [];
$operations = [];
$this->statusOutput->itemForStep('generating_operations', count($paths));
foreach ($paths as $path) {
foreach ($this->configuration->subSplit->sectionGenerator as $generator) {
Expand Down Expand Up @@ -482,8 +495,10 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
$this->configuration,
);
}

$this->statusOutput->advanceStep('generating_operations');
}

$this->statusOutput->markStepDone('generating_operations');

$webHooksHydrators = [];
Expand Down Expand Up @@ -512,6 +527,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
$this->statusOutput->advanceStep('gathering_unknown_schemas');
}
}

$this->statusOutput->markStepDone('gathering_unknown_schemas');

$sortedSchemas = [];
Expand Down Expand Up @@ -575,20 +591,24 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
$aliases,
);
}

$this->statusOutput->advanceStep('generating_schemas');
}

$this->statusOutput->markStepDone('generating_schemas');

$client = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Client::gather($this->spec, ...$paths);

$this->statusOutput->markStepDone('generating_clientinterface');

yield from ClientInterface::generate(
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
$namespace,
$operations,
);

$this->statusOutput->markStepDone('generating_client');

yield from Client::generate(
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
$namespace,
Expand All @@ -613,11 +633,14 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
$schemaRegistry,
...$webHook,
);

$this->statusOutput->advanceStep('generating_webhooks');
}

$this->statusOutput->markStepDone('generating_webhooks');

$this->statusOutput->markStepDone('generating_webhooks_entry_point');

yield from WebHooks::generate(
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
$namespace,
Expand All @@ -634,11 +657,14 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
$hydrator
);
}

$this->statusOutput->advanceStep('generating_hydrators');
}

$this->statusOutput->markStepDone('generating_hydrators');

$this->statusOutput->markStepDone('generating_hydrators_entry_point');

yield from Hydrators::generate(
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
$namespace,
Expand Down Expand Up @@ -739,6 +765,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
);
$this->statusOutput->advanceStep('generating_templates_files_subsplit_package');
}

$this->statusOutput->markStepDone('generating_templates_files_subsplit_package');

$this->statusOutput->markStepDone('generating_subsplit_configuration');
Expand Down
4 changes: 1 addition & 3 deletions src/Registry/ThrowableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

final class ThrowableSchema
{
/**
* @var array<string>
*/
/** @var array<string> */
private array $throwables = [];

public function add(string $class): void
Expand Down
46 changes: 23 additions & 23 deletions src/StatusOutput.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
<?php

declare(strict_types=1);

namespace ApiClients\Tools\OpenApiClientGenerator;

use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\OverWritingOutPut;
use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\Step;
use Termwind\Terminal;
use Symfony\Component\Console\Output\ConsoleOutput;

use function Termwind\terminal;
use function Termwind\render;
use function Termwind\renderUsing;
use function time;

final class StatusOutput
{
/**
* @var array<Step>
*/
/** @var array<Step> */
private readonly array $steps;

/**
* @var array<bool>
*/
/** @var array<bool> */
private array $stepsStatus = [];

/**
* @var array<int>
*/
/** @var array<int> */
private array $itemsCountForStep = [];

/**
* @var array<int>
*/
/** @var array<int> */
private array $stepProgress = [];

private readonly Terminal $terminal;

private int $lastPaint = 0;

public function __construct(Step ...$steps)
{
$this->steps = $steps;
foreach ($this->steps as $step) {
$this->stepsStatus[$step->key] = '🌀';
if ($step->progressBer) {
$this->itemsCountForStep[$step->key] = 0;
$this->stepProgress[$step->key] = 0;
if (! $step->progressBer) {
continue;
}

$this->itemsCountForStep[$step->key] = 0;
$this->stepProgress[$step->key] = 0;
}
$this->terminal = terminal();

renderUsing(new OverWritingOutPut(new ConsoleOutput()));
}

public function render(): void
{
$html = '<table>';
$html = '<table>';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>Status</th>';
Expand All @@ -61,15 +59,16 @@ public function render(): void
if ($step->progressBer && $this->itemsCountForStep[$step->key] > 0) {
$progress = $this->stepProgress[$step->key] . '/' . $this->itemsCountForStep[$step->key];
}

$html .= '<tr>';
$html .= '<td>' . $this->stepsStatus[$step->key] . '</td>';
$html .= '<td>' . $step->name . '</td>';
$html .= '<td>' . $progress . '</td>';
$html .= '</tr>';
}

$html .= '</table>';

$this->terminal->clear();
render($html);
$this->lastPaint = time();
}
Expand All @@ -94,6 +93,7 @@ public function markStepWontDo(string ...$keys): void
foreach ($keys as $key) {
$this->stepsStatus[$key] = '🚫';
}

$this->render();
}

Expand All @@ -106,7 +106,7 @@ public function itemForStep(string $key, int $count): void
public function advanceStep(string $key): void
{
$this->stepProgress[$key]++;
$percentage = (100 / $this->itemsCountForStep[$key]) * $this->stepProgress[$key];
$percentage = 100 / $this->itemsCountForStep[$key] * $this->stepProgress[$key];
switch (true) {
case $percentage <= 12.5:
$this->stepsStatus[$key] = '🌑';
Expand Down
Loading