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

refactor token parser #34

Merged
merged 28 commits into from
Dec 18, 2021
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
8 changes: 4 additions & 4 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ checks:
argument-count:
enabled: true
config:
threshold: 4
threshold: 5
complex-logic:
enabled: true
config:
Expand All @@ -24,15 +24,15 @@ checks:
method-lines:
enabled: true
config:
threshold: 25
threshold: 30
nested-control-flow:
enabled: true
config:
threshold: 4
return-statements:
enabled: true
config:
threshold: 4
threshold: 5
similar-code:
enabled: true
config:
Expand All @@ -42,8 +42,8 @@ checks:
config:
threshold: #language-specific defaults. overrides affect all languages.
exclude_patterns:
- "src/Parser/TokenParser.php"
- "src/Command"
- "src/Parser/ClassParser.php"
- "tests/"
- "**/vendor/"
- "example/"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Avro schema generator for PHP
[![Actions Status](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)
[![Maintainability](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/maintainability)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)
[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)
![Supported PHP versions: 7.4 .. 8.x](https://img.shields.io/badge/php-7.4%20..%208.x-blue.svg)
[![Latest Stable Version](https://poser.pugx.org/php-kafka/php-avro-schema-generator/v/stable)](https://packagist.org/packages/php-kafka/php-avro-schema-generator)

## Installation
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"require": {
"ext-json": "*",
"flix-tech/avro-php": "^3.0|^4.0",
"symfony/console": "^4.3|^5.1"
"symfony/console": "^4.3|^5.1",
"nikic/php-parser": "^4.13",
"pimple/pimple": "^3.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
Expand Down
1 change: 0 additions & 1 deletion example/classes/SomeBaseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

abstract class SomeBaseClass
{

/**
* @var Wonderland
*/
Expand Down
6 changes: 1 addition & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
parameters:
level: 8
paths: [ src ]
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- "#Call to function token_get_all\\(\\) on a separate line has no effect.#"
- "#Strict comparison using === between non-empty-array<int, mixed> and ',' will always evaluate to false.#"
- "#Strict comparison using === between non-empty-array<int, mixed> and ';' will always evaluate to false.#"
checkGenericClassInNonGenericObjectType: false
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</include>
<exclude>
<directory>src/Command</directory>
<file>src/AppContainer.php</file>
</exclude>
<report>
<clover outputFile="build/logs/phpunit/clover.xml"/>
Expand Down
35 changes: 35 additions & 0 deletions src/AppContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator;

use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\CommandServiceProvider;
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\ConverterServiceProvider;
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\GeneratorServiceProvider;
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\MergerServiceProvider;
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\ParserServiceProvider;
use PhpKafka\PhpAvroSchemaGenerator\ServiceProvider\RegistryServiceProvider;
use Pimple\Container;

class AppContainer
{
/**
* @return Container
*/
public static function init(): Container
{
$container = new Container();

$container
->register(new GeneratorServiceProvider())
->register(new MergerServiceProvider())
->register(new ParserServiceProvider())
->register(new ConverterServiceProvider())
->register(new RegistryServiceProvider())
->register(new CommandServiceProvider());


return $container;
}
}
20 changes: 20 additions & 0 deletions src/Avro/Avro.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ class Avro
'map' => self::LONELIEST_NUMBER,
'fixed' => self::LONELIEST_NUMBER,
];

/**
* @var string[]
*/
public const MAPPED_TYPES = array(
'null' => 'null',
'bool' => 'boolean',
'boolean' => 'boolean',
'string' => 'string',
'int' => 'int',
'integer' => 'int',
'float' => 'float',
'double' => 'double',
'array' => 'array',
'object' => 'object',
'callable' => 'callable',
'resource' => 'resource',
'mixed' => 'mixed',
'Collection' => 'array',
);
}
28 changes: 20 additions & 8 deletions src/Command/SchemaGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
namespace PhpKafka\PhpAvroSchemaGenerator\Command;

use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator;
use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGeneratorInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SchemaGenerateCommand extends Command
{
private SchemaGeneratorInterface $generator;
private ClassRegistryInterface $classRegistry;

public function __construct(
ClassRegistryInterface $classRegistry,
SchemaGeneratorInterface $generator,
string $name = null
) {
$this->classRegistry = $classRegistry;
$this->generator = $generator;
parent::__construct($name);
}

protected function configure(): void
{
$this
Expand All @@ -36,15 +51,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
$classDirectory = $this->getPath($classDirectoryArg);
$outputDirectory = $this->getPath($outputDirectoryArg);

$registry = (new ClassRegistry())
->addClassDirectory($classDirectory)
->load();

$generator = new SchemaGenerator($registry, $outputDirectory);

$schemas = $generator->generate();
$registry = $this->classRegistry->addClassDirectory($classDirectory)->load();
$this->generator->setOutputDirectory($outputDirectory);
$this->generator->setClassRegistry($registry);

$result = $generator->exportSchemas($schemas);
$schemas = $this->generator->generate();
$result = $this->generator->exportSchemas($schemas);

// retrieve the argument value using getArgument()
$output->writeln(sprintf('Generated %d schema files', $result));
Expand Down
29 changes: 22 additions & 7 deletions src/Command/SubSchemaMergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Command;

use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMergerInterface;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FieldOrderOptimizer;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FullNameOptimizer;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\OptimizerInterface;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\PrimitiveSchemaOptimizer;
use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMerger;
use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -18,12 +20,26 @@

class SubSchemaMergeCommand extends Command
{
private SchemaRegistryInterface $schemaRegistry;
private SchemaMergerInterface $schemaMerger;

/** @var string[] */
protected $optimizerOptionMapping = [
protected array $optimizerOptionMapping = [
'optimizeFieldOrder' => FieldOrderOptimizer::class,
'optimizeFullNames' => FullNameOptimizer::class,
'optimizePrimitiveSchemas' => PrimitiveSchemaOptimizer::class,
];

public function __construct(
SchemaMergerInterface $schemaMerger,
SchemaRegistryInterface $schemaRegistry,
string $name = null
) {
$this->schemaMerger = $schemaMerger;
$this->schemaRegistry = $schemaRegistry;
parent::__construct($name);
}

protected function configure(): void
{
$this
Expand Down Expand Up @@ -71,20 +87,19 @@ public function execute(InputInterface $input, OutputInterface $output): int
$templateDirectory = $this->getPath($templateDirectoryArg);
$outputDirectory = $this->getPath($outputDirectoryArg);

$registry = (new SchemaRegistry())
->addSchemaTemplateDirectory($templateDirectory)
->load();
$registry = $this->schemaRegistry->addSchemaTemplateDirectory($templateDirectory)->load();

$merger = new SchemaMerger($registry, $outputDirectory);
$this->schemaMerger->setSchemaRegistry($registry);
$this->schemaMerger->setOutputDirectory($outputDirectory);

/** @var OptimizerInterface $optimizerClass */
foreach ($this->optimizerOptionMapping as $optionName => $optimizerClass) {
if (true === (bool) $input->getOption($optionName)) {
$merger->addOptimizer(new $optimizerClass());
$this->schemaMerger->addOptimizer(new $optimizerClass());
}
}

$result = $merger->merge(
$result = $this->schemaMerger->merge(
(bool) $input->getOption('prefixWithNamespace'),
(bool) $input->getOption('useFilenameAsSchemaName')
);
Expand Down
Loading