Skip to content

Commit

Permalink
OrmAnnotationsExtension: Use Nette Schema for config validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Oct 14, 2020
1 parent 9b2d854 commit 1f9f3ec
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/Orm/DI/OrmAnnotationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Nette\DI\Helpers;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Validators;

final class OrmAnnotationsExtension extends CompilerExtension
Expand All @@ -45,16 +47,6 @@ final class OrmAnnotationsExtension extends CompilerExtension
/** @var string[] */
private static $annotationPaths = [];

/** @var mixed[] */
public $defaults = [
'paths' => [],
'excludePaths' => [],
'ignore' => [],
'defaultCache' => 'filesystem',
'cache' => null,
'debug' => false,
];


/**
* @return string[]
Expand All @@ -78,14 +70,28 @@ public static function addAnnotationPath(string $namespace, string $directoryPat
}


public function getConfigSchema(): Schema
{
return Expect::structure([
'paths' => Expect::arrayOf(Expect::string()),
'excludePaths' => Expect::arrayOf(Expect::string()),
'ignore' => Expect::arrayOf(Expect::string()),
'defaultCache' => Expect::string('filesystem'),
'cache' => Expect::string(),
'debug' => Expect::bool(false),
])->castTo('array');
}


public function loadConfiguration(): void
{
if ($this->compiler->getExtensions(OrmExtension::class) === []) {
throw new \RuntimeException(__CLASS__ . ': Extension "' . OrmExtension::class . '" must be defined before this extension.');
}

/** @var mixed[] $config */
$config = $this->config;
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

$reader = $builder->addDefinition($this->prefix('annotationReader'))
->setType(AnnotationReader::class)
Expand Down Expand Up @@ -123,8 +129,9 @@ public function loadConfiguration(): void

public function beforeCompile(): void
{
/** @var mixed[] $config */
$config = $this->config;
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

$builder->addDefinition($this->prefix('annotationDriver'))
->setFactory(AnnotationDriver::class, [$this->prefix('@reader'), Helpers::expand(array_merge(self::$annotationPaths, $config['paths']), $builder->parameters)])
Expand All @@ -147,7 +154,8 @@ public function afterCompile(ClassType $classType): void

private function getDefaultCache(): ServiceDefinition
{
$config = $this->getConfig();
/** @var mixed[] $config */
$config = $this->config;
$builder = $this->getContainerBuilder();

if (!isset(self::DRIVERS[$config['defaultCache']])) {
Expand Down

0 comments on commit 1f9f3ec

Please sign in to comment.