Skip to content

Commit

Permalink
add config object which gets passed to every generator
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 11, 2023
1 parent 644b9d2 commit e98118c
Show file tree
Hide file tree
Showing 29 changed files with 266 additions and 139 deletions.
4 changes: 3 additions & 1 deletion src/Console/ParseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PSX\Schema\Console;

use PSX\Schema\Generator\Code\Chunks;
use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\FileAwareInterface;
use PSX\Schema\GeneratorFactory;
use PSX\Schema\SchemaManagerInterface;
Expand Down Expand Up @@ -70,7 +71,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$factory = new GeneratorFactory();
$generator = $factory->getGenerator($input->getOption('format'), $input->getOption('config'));
$config = Config::fromQueryString($input->getOption('config'));
$generator = $factory->getGenerator($input->getOption('format'), $config);
$response = $generator->generate($schema);

if ($generator instanceof FileAwareInterface && $response instanceof Chunks) {
Expand Down
11 changes: 8 additions & 3 deletions src/Generator/CodeGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,18 @@ abstract class CodeGeneratorAbstract implements GeneratorInterface, TypeAwareInt
protected DefinitionsInterface $definitions;
private Code\Chunks $chunks;

public function __construct(?string $namespace = null, array $mapping = [], int $indent = 4)
public function __construct(?Config $config = null)
{
$mapping = $config?->get(Config::MAPPING) ?? [];
if (!is_array($mapping)) {
throw new \InvalidArgumentException('Provided mapping config must be an array');
}

$this->normalizer = $this->newNormalizer();
$this->generator = $this->newTypeGenerator($mapping);
$this->namespace = $namespace;
$this->namespace = $config?->get(Config::NAMESPACE);
$this->mapping = $mapping;
$this->indent = str_repeat(' ', $indent);
$this->indent = str_repeat(' ', $config?->get(Config::INDENT) ?? 4);
}

public function generate(SchemaInterface $schema)
Expand Down
56 changes: 56 additions & 0 deletions src/Generator/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Schema\Generator;

use PSX\Record\Record;

/**
* Config
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
class Config extends Record
{
public const NAMESPACE = 'namespace';
public const MAPPING = 'mapping';
public const INDENT = 'indent';
public const HEADING = 'heading';
public const PREFIX = 'prefix';

public static function fromQueryString(?string $query): Config
{
$result = [];
parse_str($query ?? '', $result);

return self::from($result);
}

public static function of(string $namespace, array $mapping = []): Config
{
$config = new self();
$config->put(self::NAMESPACE, $namespace);
$config->put(self::MAPPING, $mapping);

return $config;
}
}
4 changes: 2 additions & 2 deletions src/Generator/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class JsonSchema implements GeneratorInterface
{
private string $refBase;

public function __construct(string $refBase = '#/definitions/')
public function __construct(?Config $config = null)
{
$this->refBase = $refBase;
$this->refBase = $config?->get('ref_base') ?? '#/definitions/';
}

public function generate(SchemaInterface $schema)
Expand Down
6 changes: 4 additions & 2 deletions src/Generator/MarkupAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ abstract class MarkupAbstract extends CodeGeneratorAbstract
protected int $heading;
protected string $prefix;

public function __construct(int $heading = 1, string $prefix = 'psx_model_')
public function __construct(?Config $config = null)
{
parent::__construct();

$heading = (int) $config?->get('heading');

$this->heading = $heading >= 1 && $heading <= 6 ? $heading : 1;
$this->prefix = $prefix;
$this->prefix = $config?->get('prefix') ?? 'psx_model_';
}

protected function getConstraints(TypeInterface $type): array
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class Php extends CodeGeneratorAbstract
private BuilderFactory $factory;
private PrettyPrinter\Standard $printer;

public function __construct(?string $namespace = null, array $mapping = [], int $indent = 4)
public function __construct(?Config $config = null)
{
parent::__construct($namespace, $mapping, $indent);
parent::__construct($config);

$this->factory = new BuilderFactory();
$this->printer = new PrettyPrinter\Standard();
Expand Down
44 changes: 19 additions & 25 deletions src/GeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace PSX\Schema;

use PSX\Schema\Generator\Config;

/**
* GeneratorFactory
*
Expand Down Expand Up @@ -47,64 +49,56 @@ class GeneratorFactory
public const TYPE_TYPESCRIPT = 'typescript';
public const TYPE_VISUALBASIC = 'visualbasic';

public function getGenerator(string $format, ?string $config = null): GeneratorInterface
public function getGenerator(string $format, ?Config $config = null): GeneratorInterface
{
$result = [];
parse_str($config ?? '', $result);
$namespace = $result['namespace'] ?? null;
$mapping = $result['mapping'] ?? [];
$indent = $result['indent'] ?? 4;
$heading = $result['heading'] ?? 1;
$prefix = $result['prefix'] ?? 'psx_model_';

switch ($format) {
case self::TYPE_CSHARP:
return new Generator\CSharp($namespace, $mapping, $indent);
return new Generator\CSharp($config);

case self::TYPE_GO:
return new Generator\Go($namespace, $mapping, $indent);
return new Generator\Go($config);

case self::TYPE_GRAPHQL:
return new Generator\GraphQL($namespace, $mapping, $indent);
return new Generator\GraphQL($config);

case self::TYPE_HTML:
return new Generator\Html((int) $heading, $prefix);
return new Generator\Html($config);

case self::TYPE_JAVA:
return new Generator\Java($namespace, $mapping, $indent);
return new Generator\Java($config);

case self::TYPE_JSONSCHEMA:
return new Generator\JsonSchema();
return new Generator\JsonSchema($config);

case self::TYPE_KOTLIN:
return new Generator\Kotlin($namespace, $mapping, $indent);
return new Generator\Kotlin($config);

case self::TYPE_MARKDOWN:
return new Generator\Markdown((int) $heading, $prefix);
return new Generator\Markdown($config);

case self::TYPE_PHP:
return new Generator\Php($namespace, $mapping, $indent);
return new Generator\Php($config);

case self::TYPE_PROTOBUF:
return new Generator\Protobuf($namespace, $mapping, $indent);
return new Generator\Protobuf($config);

case self::TYPE_PYTHON:
return new Generator\Python($namespace, $mapping, $indent);
return new Generator\Python($config);

case self::TYPE_RUBY:
return new Generator\Ruby($namespace, $mapping, $indent);
return new Generator\Ruby($config);

case self::TYPE_RUST:
return new Generator\Rust($namespace, $mapping, $indent);
return new Generator\Rust($config);

case self::TYPE_SWIFT:
return new Generator\Swift($namespace, $mapping, $indent);
return new Generator\Swift($config);

case self::TYPE_TYPESCRIPT:
return new Generator\TypeScript($namespace, $mapping, $indent);
return new Generator\TypeScript($config);

case self::TYPE_VISUALBASIC:
return new Generator\VisualBasic($namespace, $mapping, $indent);
return new Generator\VisualBasic($config);

default:
case self::TYPE_TYPESCHEMA:
Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/CSharpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\CSharp;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new CSharp('Foo.Bar', ['my_import' => 'My.Import']);
$generator = new CSharp(Config::of('Foo.Bar', ['my_import' => 'My.Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
57 changes: 57 additions & 0 deletions tests/Generator/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Schema\Tests\Generator;

use PHPUnit\Framework\TestCase;
use PSX\Schema\Generator\Config;

/**
* ConfigTest
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
class ConfigTest extends TestCase
{
/**
* @dataProvider stringProvider
*/
public function testFromQueryString(mixed $value, array $expect)
{
$config = Config::fromQueryString($value);

$this->assertInstanceOf(Config::class, $config);
$this->assertEquals($expect, $config->getAll());
}

public function stringProvider(): array
{
return [
[null, []],
['', []],
[' ', []],
['foo', ['foo' => '']],
['foo=bar', ['foo' => 'bar']],
['foo[test]=bar', ['foo' => ['test' => 'bar']]],
];
}
}
3 changes: 2 additions & 1 deletion tests/Generator/GoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\Go;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Go('Foo.Bar', ['my_import' => 'My.Import']);
$generator = new Go(Config::of('Foo.Bar', ['my_import' => 'My.Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/JavaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\Java;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Java('Foo.Bar', ['my_import' => 'My.Import']);
$generator = new Java(Config::of('Foo.Bar', ['my_import' => 'My.Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/KotlinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\Kotlin;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Kotlin('Foo.Bar', ['my_import' => 'My.Import']);
$generator = new Kotlin(Config::of('Foo.Bar', ['my_import' => 'My.Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\Php;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Php('Foo\\Bar', ['my_import' => 'My\\Import']);
$generator = new Php(Config::of('Foo\\Bar', ['my_import' => 'My\\Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/PythonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Schema\Tests\Generator;

use PSX\Schema\Generator\Config;
use PSX\Schema\Generator\Python;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Python('Foo.Bar', ['my_import' => 'My.Import']);
$generator = new Python(Config::of('Foo.Bar', ['my_import' => 'My.Import']));

$actual = (string) $generator->generate($this->getImportSchema());

Expand Down
Loading

0 comments on commit e98118c

Please sign in to comment.