Skip to content

Commit

Permalink
Compiler, CompilerExtension: improved Loader extendability (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
lookyman authored and dg committed Jan 30, 2017
1 parent 5d38530 commit a325b17
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DI/Compiler.php
Expand Up @@ -105,9 +105,9 @@ public function addConfig(array $config)
* Adds new configuration from file.
* @return static
*/
public function loadConfig(string $file)
public function loadConfig(string $file, Config\Loader $loader = NULL)
{
$loader = new Config\Loader;
$loader = $loader ?: new Config\Loader;
$this->addConfig($loader->load($file));
$this->dependencies->add($loader->getDependencies());
return $this;
Expand Down
8 changes: 7 additions & 1 deletion src/DI/CompilerExtension.php
Expand Up @@ -90,13 +90,19 @@ public function getContainerBuilder(): ContainerBuilder
*/
public function loadFromFile(string $file): array
{
$loader = new Config\Loader;
$loader = $this->createLoader();
$res = $loader->load($file);
$this->compiler->addDependencies($loader->getDependencies());
return $res;
}


protected function createLoader(): Config\Loader
{
return new Config\Loader;
}


/**
* Prepend extension name to identifier or service name.
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/DI/CompilerExtension.loadFromFile.phpt
@@ -0,0 +1,30 @@
<?php

/**
* Test: Nette\DI\CompilerExtension::loadFromFile()
*/

use Nette\DI;
use Tester\Assert;


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


class MyExtension extends Nette\DI\CompilerExtension
{
}


test(function () {
$ext = new MyExtension;
$ext->setCompiler(new DI\Compiler, 'my');
$res = $ext->loadFromFile(__DIR__ . '/files/compilerExtension.loadFromFile.neon');
Assert::equal([
'services' => [
'one' => [
'class' => 'Ipsum',
],
],
], $res);
});
3 changes: 3 additions & 0 deletions tests/DI/files/compilerExtension.loadFromFile.neon
@@ -0,0 +1,3 @@
services:
one:
class: Ipsum

0 comments on commit a325b17

Please sign in to comment.