From 6b9910cece1e074ad1d1babc6a62c48dfa9ea870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Mon, 12 Feb 2024 13:40:00 +0100 Subject: [PATCH] PhpGenerator: DTO and DateTime are not suspicious objects [Closes #306][Closes #308] --- src/DI/PhpGenerator.php | 3 ++- tests/DI/Compiler.parameters.phpt | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index 42a979059..961f96d8f 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -181,7 +181,8 @@ public function convertArguments(array $args): array } } elseif ( is_object($val) - && !$val instanceof Php\Literal && !$val instanceof \stdClass && !$val instanceof \UnitEnum + && !$val instanceof Php\Literal && !$val instanceof \DateTimeInterface + && (new \ReflectionObject($val))->getProperties(\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED) ) { trigger_error(sprintf('Nette DI: suspicious dumping of objects %s when generating the container', $val::class)); } diff --git a/tests/DI/Compiler.parameters.phpt b/tests/DI/Compiler.parameters.phpt index 778de5e4c..e971ee4d6 100644 --- a/tests/DI/Compiler.parameters.phpt +++ b/tests/DI/Compiler.parameters.phpt @@ -68,6 +68,19 @@ test('Statement within string expansion', function () { }); +test('Statement with datetime', function () { + $compiler = new DI\Compiler; + $container = createContainer($compiler, ' + parameters: + datetime: 2000-01-01 00:00:00 +0000 + + services: + one: Service(%datetime%) + '); + + Assert::same('2000-01-01', $container->getService('one')->arg->format('Y-m-d')); +}); + test('Statement within array expansion', function () { $compiler = new DI\Compiler; $container = createContainer($compiler, '