From 2d173ea97cf3c09def266dc49ebce34228e3cd0c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 28 Feb 2019 05:12:43 +0100 Subject: [PATCH] =?UTF-8?q?Minification=20of=20metadata:=20added=20option?= =?UTF-8?q?=20'di=20=E2=80=BA=20export=20=E2=80=BA=20parameters'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DI/Extensions/DIExtension.php | 10 +++ tests/DI/DIExtension.exportParameters.phpt | 80 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/DI/DIExtension.exportParameters.phpt diff --git a/src/DI/Extensions/DIExtension.php b/src/DI/Extensions/DIExtension.php index 010220dfa..864ad8089 100644 --- a/src/DI/Extensions/DIExtension.php +++ b/src/DI/Extensions/DIExtension.php @@ -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); } @@ -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(); diff --git a/tests/DI/DIExtension.exportParameters.phpt b/tests/DI/DIExtension.exportParameters.phpt new file mode 100644 index 000000000..0a50a2bd1 --- /dev/null +++ b/tests/DI/DIExtension.exportParameters.phpt @@ -0,0 +1,80 @@ +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); +});