Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract schema generator #9

Merged
merged 1 commit into from
Jan 25, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ return [
```yaml
# config/packages/asyncapi_doc_bundle.yaml
ferror_asyncapi_doc_bundle:
asyncapi_version: '2.6.0' # Async API specification version (default: 2.6.0)
title: 'Service Example API'
version: '1.2.3' # Your API version
events: # The event class namespace
Expand Down
2 changes: 1 addition & 1 deletion src/Attribute/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Ferror\AsyncapiDocBundle\Attribute;

use Attribute;
use Ferror\AsyncapiDocBundle\Schema\ChannelType;
use Ferror\AsyncapiDocBundle\Schema\V2\ChannelType;

#[Attribute(Attribute::TARGET_CLASS)]
class Message implements PropertyInterface
Expand Down
10 changes: 5 additions & 5 deletions src/GeneratorFactory.php → src/Generator/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle;
namespace Ferror\AsyncapiDocBundle\Generator;

use Ferror\AsyncapiDocBundle\Generator\JsonGenerator;
use Ferror\AsyncapiDocBundle\Generator\YamlGenerator;
use InvalidArgumentException;
use Ferror\AsyncapiDocBundle\DataFormat;
use Ferror\AsyncapiDocBundle\GeneratorInterface;
use Ferror\AsyncapiDocBundle\Schema\V2\SchemaRenderer;

final readonly class GeneratorFactory
{
public function __construct(private SchemaGenerator $generator)
public function __construct(private SchemaRenderer $generator)
{
}

Expand Down
7 changes: 3 additions & 4 deletions src/Generator/JsonGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace Ferror\AsyncapiDocBundle\Generator;

use Ferror\AsyncapiDocBundle\GeneratorInterface;
use Ferror\AsyncapiDocBundle\SchemaGenerator;
use Ferror\AsyncapiDocBundle\Schema\V2\SchemaRenderer;
use JsonException;

final readonly class JsonGenerator implements GeneratorInterface
{
public function __construct(
private SchemaGenerator $generator,
) {
public function __construct(private SchemaRenderer $generator)
{
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Generator/YamlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace Ferror\AsyncapiDocBundle\Generator;

use Ferror\AsyncapiDocBundle\GeneratorInterface;
use Ferror\AsyncapiDocBundle\SchemaGenerator;
use Ferror\AsyncapiDocBundle\Schema\V2\SchemaRenderer;
use Symfony\Component\Yaml\Yaml;

final readonly class YamlGenerator implements GeneratorInterface
{
public function __construct(
private SchemaGenerator $generator,
) {
public function __construct(private SchemaRenderer $generator)
{
}

public function generate(): string
Expand Down
15 changes: 0 additions & 15 deletions src/Schema/InfoObject.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Schema/SchemaGeneratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema;

use Ferror\AsyncapiDocBundle\Schema\V2\SchemaRenderer;
use Ferror\AsyncapiDocBundle\SchemaRendererInterface;
use InvalidArgumentException;

final readonly class SchemaGeneratorFactory
{
public function __construct(
private SchemaRenderer $schemaGeneratorV2,
) {
}

public function __invoke(string $version): SchemaRendererInterface
{
[$major, $minor, $patch] = explode('.', $version);

if ($major === '2') {
return $this->schemaGeneratorV2;
}

throw new InvalidArgumentException("Not supported Async API Schema $version");
}
}
21 changes: 21 additions & 0 deletions src/Schema/V2/ChannelRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema\V2;

class ChannelRenderer
{
public function render(array $document): array
{
$channel[$document['channel']] = [
$document['channelType'] => [
'message' => [
'$ref' => '#/components/messages/' . $document['name'],
],
],
];

return $channel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema;
namespace Ferror\AsyncapiDocBundle\Schema\V2;

enum ChannelType: string
{
Expand Down
24 changes: 24 additions & 0 deletions src/Schema/V2/InfoRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema\V2;

final readonly class InfoRenderer
{
public function __construct(
public string $title,
public string $description,
public string $version,
) {
}

public function render(): array
{
return [
'title' => $this->title,
'version' => $this->version,
'description' => $this->description,
];
}
}
27 changes: 3 additions & 24 deletions src/Schema/SchemaV2.php → src/Schema/V2/MessageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema;
namespace Ferror\AsyncapiDocBundle\Schema\V2;

use Ferror\AsyncapiDocBundle\PropertyTypeTranslator;
use Ferror\AsyncapiDocBundle\SchemaInterface;

class SchemaV2 implements SchemaInterface
class MessageRenderer
{
public function supports(string $version): bool
{
[$major, $minor, $patch] = explode('.', $version);

return $major === '2';
}

public function renderMessage(array $document): array
public function render(array $document): array
{
$properties = [];
$required = [];
Expand Down Expand Up @@ -51,17 +43,4 @@ public function renderMessage(array $document): array

return $message;
}

public function renderChannel(array $document): array
{
$channel[$document['channel']] = [
$document['channelType'] => [
'message' => [
'$ref' => '#/components/messages/' . $document['name'],
],
],
];

return $channel;
}
}
25 changes: 11 additions & 14 deletions src/SchemaGenerator.php → src/Schema/V2/SchemaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle;
namespace Ferror\AsyncapiDocBundle\Schema\V2;

use Ferror\AsyncapiDocBundle\ClassFinder\ClassFinderInterface;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\DocumentationStrategyInterface;
use Ferror\AsyncapiDocBundle\Schema\InfoObject;
use Ferror\AsyncapiDocBundle\SchemaRendererInterface;

final readonly class SchemaGenerator
final readonly class SchemaRenderer implements SchemaRendererInterface
{
public function __construct(
private ClassFinderInterface $classFinder,
private DocumentationStrategyInterface $documentationStrategy,
private SchemaInterface $schema,
private ChannelRenderer $channelRenderer,
private MessageRenderer $messageRenderer,
private InfoRenderer $infoRenderer,
private array $servers,
private InfoObject $infoObject,
private string $asyncApiVersion = '2.6.0'
private string $schemaVersion,
) {
}

Expand All @@ -29,8 +30,8 @@ public function generate(): array

foreach ($classes as $class) {
$document = $this->documentationStrategy->document($class);
$channel = $this->schema->renderChannel($document);
$message = $this->schema->renderMessage($document);
$channel = $this->channelRenderer->render($document);
$message = $this->messageRenderer->render($document);

$channelKey = key($channel);
$messageKey = key($message);
Expand All @@ -40,12 +41,8 @@ public function generate(): array
}

return [
'asyncapi' => $this->asyncApiVersion,
'info' => [
'title' => $this->infoObject->title,
'version' => $this->infoObject->version,
'description' => $this->infoObject->description,
],
'asyncapi' => $this->schemaVersion,
'info' => $this->infoRenderer->render(),
'servers' => $this->servers,
'channels' => $channels,
'components' => [
Expand Down
16 changes: 16 additions & 0 deletions src/Schema/V3/SchemaRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle\Schema\V3;

use Ferror\AsyncapiDocBundle\SchemaRendererInterface;
use RuntimeException;

final readonly class SchemaRenderer implements SchemaRendererInterface
{
public function generate(): array
{
throw new RuntimeException("Async API V3 not yet supported");
}
}
12 changes: 0 additions & 12 deletions src/SchemaInterface.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/SchemaRendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Ferror\AsyncapiDocBundle;

interface SchemaRendererInterface
{
public function generate(): array;
}
1 change: 1 addition & 0 deletions src/Symfony/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$root = $builder->getRootNode();
$root
->children()
->scalarNode('asyncapi_version')->defaultValue('2.6.0')->end()
->scalarNode('title')->end()
->scalarNode('version')->end()
->scalarNode('description')->defaultValue('')->end()
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Console/DumpSpecificationConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

use Ferror\AsyncapiDocBundle\DataFormat;
use Ferror\AsyncapiDocBundle\DocumentationStrategy\DocumentationStrategyInterface;
use Ferror\AsyncapiDocBundle\GeneratorFactory;
use Ferror\AsyncapiDocBundle\SchemaInterface;
use Ferror\AsyncapiDocBundle\Generator\GeneratorFactory;
use Ferror\AsyncapiDocBundle\Schema\V2\MessageRenderer;
use Ferror\AsyncapiDocBundle\Tests\Examples\UserSignedUp;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -22,7 +22,7 @@ class DumpSpecificationConsole extends Command
public function __construct(
private readonly GeneratorFactory $generatorFactory,
private readonly DocumentationStrategyInterface $documentationStrategy,
private readonly SchemaInterface $schema,
private readonly MessageRenderer $messageRenderer,
) {
parent::__construct('ferror:asyncapi:dump');
$this->addArgument('class', InputArgument::OPTIONAL, sprintf('Class name. Example %s', UserSignedUp::class));
Expand All @@ -36,7 +36,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
if ($input->getArgument('class')) {
$document = $this->documentationStrategy->document($input->getArgument('class'));

$schema = $this->schema->renderMessage($document);
$schema = $this->messageRenderer->render($document);

$io->writeln(Yaml::dump($schema, 10, 2));

Expand Down
Loading
Loading