Skip to content

Commit

Permalink
Minification of metadata: added option 'di › export › parameters'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 26, 2019
1 parent 0ffdb4e commit 2d173ea
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/DI/Extensions/DIExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function __construct(bool $debugMode = false)
public $excluded = [];
/** @var ?string */
public $parentClass;
/** @var object */
public $export;
};
$this->config->export = new class {
/** @var bool */
public $parameters = true;
};
$this->config->debugger = interface_exists(\Tracy\IBarPanel::class);
}
Expand All @@ -54,6 +60,10 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$class->setExtends($this->config->parentClass);
}

if (!$this->config->export->parameters) {
$class->removeMethod('__construct');
}

$initialize = $class->getMethod('initialize');
$builder = $this->getContainerBuilder();

Expand Down
80 changes: 80 additions & 0 deletions tests/DI/DIExtension.exportParameters.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Test: DIExtension parameters exporting
*/

declare(strict_types=1);

use Nette\DI;
use Nette\DI\Extensions\DIExtension;
use Tester\Assert;


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


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
parameters:
key: val
di:
export:
parameters: true
');

Assert::same(['key' => 'val'], $container->parameters);
});


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
parameters:
key: val
di:
export:
parameters: false
');

Assert::same([], $container->parameters);
});


test(function () {
$compiler = new DI\Compiler;
$compiler->setDynamicParameterNames(['dynamic']);
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
parameters:
key: %dynamic%
di:
export:
parameters: true
', ['dynamic' => 123]);

Assert::same(['dynamic' => 123, 'key' => 123], $container->parameters);
});


test(function () {
$compiler = new DI\Compiler;
$compiler->setDynamicParameterNames(['dynamic']);
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
parameters:
key: %dynamic%
di:
export:
parameters: false
', ['dynamic' => 123]);

Assert::same(['dynamic' => 123], $container->parameters);
});

0 comments on commit 2d173ea

Please sign in to comment.