Skip to content

Commit

Permalink
Rafactoring: join factory + generator together
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 4, 2017
1 parent f5d1e98 commit 273017a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/DI/ApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Contributte\Api\Router\IRouter;
use Contributte\Api\Schema\ApiSchema;
use Contributte\Api\Schema\Builder\SchemaBuilder;
use Contributte\Api\Schema\Factory\ArrayFactory;
use Contributte\Api\Schema\Generator\ArrayGenerator;
use Contributte\Api\Schema\Factory\ArrayHydrator;
use Contributte\Api\Schema\Generator\ArraySerializator;
use Contributte\Api\Schema\Validator\Impl\PathValidator;
use Contributte\Api\Schema\Validator\Impl\RootPathValidator;
use Contributte\Api\Schema\Validator\SchemaBuilderValidator;
Expand Down Expand Up @@ -94,11 +94,11 @@ public function beforeCompile()
$this->validateSchema($schemaBuilder);

// Convert schema to array (for DI)
$generator = new ArrayGenerator();
$schema = $generator->generate($schemaBuilder);
$generator = new ArraySerializator();
$schema = $generator->serialize($schemaBuilder);

$builder->addDefinition($this->prefix('schemaFactory'))
->setClass(ArrayFactory::class, [$schema]);
->setClass(ArrayHydrator::class, [$schema]);

$builder->getDefinition($this->prefix('schema'))
->setFactory('@' . $this->prefix('schemaFactory') . '::create');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,18 @@
use Contributte\Api\Schema\EndpointParameter;
use Contributte\Api\Schema\SchemaMapping;

final class ArrayFactory implements IFactory
final class ArrayHydrator implements IHydrator
{

/** @var array */
private $schema;

/**
* @param array $schema
*/
public function __construct(array $schema)
{
$this->schema = $schema;
}

/**
* @return ApiSchema
*/
public function create()
public function hydrate($schema)
{
$schema = new ApiSchema();

foreach ($this->schema as $route) {
foreach ($schema as $route) {
// @todo replace to EndpointHandler::factory()
// move validation inside, make class immutable
$handler = new EndpointHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
use Contributte\Api\Utils\Helpers;
use Contributte\Api\Utils\Regex;

final class ArrayGenerator implements IGenerator
final class ArraySerializator implements ISerializator
{

/**
* @param SchemaBuilder $builder
* @return array
*/
public function generate(SchemaBuilder $builder)
public function serialize(SchemaBuilder $builder)
{
$controllers = $builder->getControllers();
$schema = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use Contributte\Api\Schema\ApiSchema;

interface IFactory
interface IHydrator
{

/**
* @param mixed $data
* @return ApiSchema
*/
public function create();
public function hydrate($data);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Contributte\Api\Schema\Builder\SchemaBuilder;

interface IGenerator
interface ISerializator
{

/**
* @param SchemaBuilder $builder
* @return mixed
*/
public function generate(SchemaBuilder $builder);
public function serialize(SchemaBuilder $builder);

}

0 comments on commit 273017a

Please sign in to comment.