Skip to content

Commit

Permalink
added ConfiguratorsExtension [problem v nette\database]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 14, 2021
1 parent f3462df commit dfc3796
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Compiler
private const
SERVICES = 'services',
PARAMETERS = 'parameters',
DI = 'di';
DI = 'di',
CONFIGURATORS = 'configurators';

/** @var CompilerExtension[] */
private $extensions = [];
Expand Down Expand Up @@ -53,6 +54,7 @@ public function __construct(?ContainerBuilder $builder = null)
$this->dependencies = new DependencyChecker;
$this->addExtension(self::SERVICES, new Extensions\ServicesExtension);
$this->addExtension(self::PARAMETERS, new Extensions\ParametersExtension($this->configs));
$this->addExtension(self::CONFIGURATORS, new Extensions\ConfiguratorsExtension);
}


Expand Down Expand Up @@ -217,7 +219,10 @@ public function compile(): string
/** @internal */
public function processExtensions(): void
{
$first = $this->getExtensions(Extensions\ParametersExtension::class) + $this->getExtensions(Extensions\ExtensionsExtension::class);
$first = $this->getExtensions(Extensions\ConfiguratorsExtension::class)
+ $this->getExtensions(Extensions\ParametersExtension::class)
+ $this->getExtensions(Extensions\ExtensionsExtension::class);

foreach ($first as $name => $extension) {
$config = $this->processSchema($extension->getConfigSchema(), $this->configs[$name] ?? [], $name);
$extension->setConfig($this->config[$name] = $config);
Expand All @@ -233,7 +238,11 @@ public function processExtensions(): void

$extensions = array_diff_key($this->extensions, $first, [self::SERVICES => 1]);
foreach ($extensions as $name => $extension) {
$config = $this->processSchema($extension->getConfigSchema(), $this->configs[$name] ?? [], $name);
$config = $this->processSchema(
$extension->getConfigSchema(),
array_merge([(array) $extension->getConfig()], $this->configs[$name] ?? []),
$name
);
$extension->setConfig($this->config[$name] = $config);
}

Expand Down
5 changes: 4 additions & 1 deletion src/DI/Config/Adapters/PhpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ final class PhpAdapter implements Nette\DI\Config\Adapter
*/
public function load(string $file): array
{
return require $file;
$data = require $file;
return $data instanceof \Closure
? ['configurators' => [$data]]
: $data;
}


Expand Down
26 changes: 26 additions & 0 deletions src/DI/Extensions/ConfiguratorsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\DI\Extensions;

use Nette;


/**
* PHP configurators.
*/
class ConfiguratorsExtension extends Nette\DI\CompilerExtension
{
public function loadConfiguration()
{
foreach ($this->config as $callback) {
$callback($this->compiler);
}
}
}
1 change: 1 addition & 0 deletions tests/DI/Compiler.config.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('Compiler config', function () {

Assert::same(
[
'configurators' => [],
'parameters' => [
'item1' => 1,
],
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.extension.anonymous.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ $compiler = new DI\Compiler;
$compiler->addExtension(null, new FooExtension);
$compiler->addExtension(null, new FooExtension);

Assert::count(4, $compiler->getExtensions());
Assert::count(5, $compiler->getExtensions());
2 changes: 2 additions & 0 deletions tests/DI/Compiler.loadConfig.include.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ Assert::same([
__DIR__ . DIRECTORY_SEPARATOR . 'files/loader.includes.grandchild.neon',
(new ReflectionClass(Nette\DI\Extensions\ServicesExtension::class))->getFileName(),
(new ReflectionClass(Nette\DI\Extensions\ParametersExtension::class))->getFileName(),
(new ReflectionClass(Nette\DI\Extensions\ConfiguratorsExtension::class))->getFileName(),
], array_keys($compiler->exportDependencies()[1]));


Assert::equal([
'configurators' => [],
'parameters' => [
'me' => [
'loader.includes.child.neon',
Expand Down
22 changes: 22 additions & 0 deletions tests/DI/ConfiguratorsExtension.basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;

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


class Service
{
}


$compiler = new DI\Compiler;
$compiler->loadConfig(__DIR__ . '/files/phpConfig1.php');
$compiler->loadConfig(__DIR__ . '/files/phpConfig2.php');
$container = createContainer($compiler);

Assert::type(Service::class, $container->getByType(Service::class));
Assert::same(['foo' => 123], $container->parameters);
2 changes: 1 addition & 1 deletion tests/DI/ExtensionsExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ foo:

Assert::same('hello', $container->parameters['foo']);
Assert::same('test', $container->parameters['bar']);
Assert::same(['services', 'parameters', 'first', 'extensions', 'foo', 'bar'], $container->parameters['first']);
Assert::same(['services', 'parameters', 'configurators', 'first', 'extensions', 'foo', 'bar'], $container->parameters['first']);
9 changes: 9 additions & 0 deletions tests/DI/files/phpConfig1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

return function (Nette\DI\Compiler $compiler) {
$builder = $compiler->getContainerBuilder();
$builder->addDefinition(null)
->setFactory(Service::class);
};
11 changes: 11 additions & 0 deletions tests/DI/files/phpConfig2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return function (Nette\DI\Compiler $compiler) {
$compiler->addConfig([
'parameters' => [
'foo' => 123,
],
]);
};

0 comments on commit dfc3796

Please sign in to comment.