Skip to content

Commit

Permalink
ExtensionsExtension: added extension type checking (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored and dg committed Aug 7, 2019
1 parent 1a47803 commit a49bf89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/DI/Extensions/ExtensionsExtension.php
Expand Up @@ -29,12 +29,14 @@ public function loadConfiguration()
if (is_int($name)) { if (is_int($name)) {
$name = null; $name = null;
} }
$args = [];
if ($class instanceof Nette\DI\Definitions\Statement) { if ($class instanceof Nette\DI\Definitions\Statement) {
$rc = new \ReflectionClass($class->getEntity()); [$class, $args] = [$class->getEntity(), $class->arguments];
$this->compiler->addExtension($name, $rc->newInstanceArgs($class->arguments));
} else {
$this->compiler->addExtension($name, new $class);
} }
if (!is_a($class, Nette\DI\CompilerExtension::class, true)) {
throw new Nette\DI\InvalidConfigurationException("Extension should be Nette\\DI\\CompilerExtension, '$class' given.");
}
$this->compiler->addExtension($name, (new \ReflectionClass($class))->newInstanceArgs($args));
} }
} }
} }
20 changes: 20 additions & 0 deletions tests/DI/ExtensionsExtension.error.phpt
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$compiler = new DI\Compiler;
$compiler->addExtension('extensions', new Nette\DI\Extensions\ExtensionsExtension);

Assert::exception(function () use ($compiler) {
createContainer($compiler, '
extensions:
foo: stdClass
');
}, Nette\DI\InvalidConfigurationException::class, "Extension should be Nette\\DI\\CompilerExtension, 'stdClass' given.");

0 comments on commit a49bf89

Please sign in to comment.