Skip to content

Commit

Permalink
Merge pull request #3 from mrakolice/default-classes-customization
Browse files Browse the repository at this point in the history
add default classes changing
  • Loading branch information
wolfy-j committed Feb 8, 2020
2 parents 63895d0 + 49325f0 commit 66d6150
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
27 changes: 20 additions & 7 deletions src/Compiler.php
Expand Up @@ -11,7 +11,10 @@

namespace Cycle\Schema;

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\Schema\Definition\Entity;
use Doctrine\Common\Inflector\Inflector;
use Spiral\Database\Exception\CompilerException;
Expand All @@ -21,17 +24,27 @@ final class Compiler
/** @var array */
private $result = [];

/** @var array */
private $defaults = [
Schema::MAPPER => Mapper::class,
Schema::REPOSITORY => Repository::class,
Schema::SOURCE => Source::class,
Schema::CONSTRAIN => null,
];

/**
* Compile the registry schema.
*
* @param Registry $registry
* @param Registry $registry
* @param GeneratorInterface[] $generators
* @param array $defaults
* @return array
*
* @throws CompilerException
*/
public function compile(Registry $registry, array $generators = []): array
public function compile(Registry $registry, array $generators = [], array $defaults = []): array
{
$this->defaults = $defaults + $this->defaults;

foreach ($generators as $generator) {
if (!$generator instanceof GeneratorInterface) {
throw new CompilerException(sprintf(
Expand Down Expand Up @@ -75,10 +88,10 @@ protected function compute(Registry $registry, Entity $entity): void
{
$schema = [
Schema::ENTITY => $entity->getClass(),
Schema::SOURCE => $entity->getSource(),
Schema::MAPPER => $entity->getMapper(),
Schema::REPOSITORY => $entity->getRepository(),
Schema::CONSTRAIN => $entity->getConstrain(),
Schema::SOURCE => $entity->getSource() ?? $this->defaults[Schema::SOURCE],
Schema::MAPPER => $entity->getMapper() ?? $this->defaults[Schema::MAPPER],
Schema::REPOSITORY => $entity->getRepository() ?? $this->defaults[Schema::REPOSITORY],
Schema::CONSTRAIN => $entity->getConstrain() ?? $this->defaults[Schema::CONSTRAIN],
Schema::SCHEMA => $entity->getSchema(),
Schema::PRIMARY_KEY => $this->getPrimary($entity),
Schema::COLUMNS => $this->renderColumns($entity),
Expand Down
12 changes: 6 additions & 6 deletions src/Definition/Entity.php
Expand Up @@ -133,9 +133,9 @@ public function setMapper(?string $mapper): self
/**
* @return string
*/
public function getMapper(): string
public function getMapper(): ?string
{
return $this->mapper ?? Mapper::class;
return $this->mapper;
}

/**
Expand All @@ -152,9 +152,9 @@ public function setSource(?string $source): self
/**
* @return string
*/
public function getSource(): string
public function getSource(): ?string
{
return $this->source ?? Source::class;
return $this->source;
}

/**
Expand Down Expand Up @@ -190,9 +190,9 @@ public function setRepository(?string $repository): self
/**
* @return string
*/
public function getRepository(): string
public function getRepository(): ?string
{
return $this->repository ?? Repository::class;
return $this->repository;
}

/**
Expand Down

0 comments on commit 66d6150

Please sign in to comment.