PHP-DI: Error compiling factory definitions since 6.0.7
<?php
declare(strict_types=1);
use function DI\autowire;
use DI\ContainerBuilder;
use function DI\factory;
require_once __DIR__.'/../vendor/autoload.php';
class Fu
{
public function create(): self
{
return new static();
}
}
class Bar
{
public function create(): self
{
return new static();
}
}
class FuBar
{
private $fu;
private $bar;
public function __construct(Fu $fu, Bar $bar)
{
$this->fu = $fu;
$this->bar = $bar;
}
}
$builder = new ContainerBuilder();
$builder->enableCompilation(__DIR__);
$builder->addDefinitions([
FuBar::class => autowire()
->constructorParameter('fu', factory([Fu::class, 'create']))
->constructorParameter('bar', factory([Bar::class, 'create']))
]);
$dic = $builder->build();
In CompiledContainer.php
you’ll find
protected function get3b62a97e28ef909b96b42a793be8a616()
{
$object = new FuBar($this->get5ffee756f39d463eb2a0c22906c0f2be(), $this->get5ffee756f39d463eb2a0c22906c0f2be());
return $object;
}
As you can see the definition for Bar
(Method get5ffee756f39d463eb2a0c22906c0f2be
) is used for every Parameter in FuBar::__construct
.
I was able to nail the issue down to \DI\Compiler\Compiler::getHashedValue
. In this scenario the parameter string $value
is <nested definition>Factory
for every factory definition used like above.
In our case this is a crititcal issue, downgrading to 6.0.6 fixed this.
Relates to #605
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 27 (21 by maintainers)
Commits related to this issue
- Reproduce problem in #648 — committed to holtkamp/PHP-DI by holtkamp 5 years ago
- Attempt to fix #648 and keep the container idempotent The idea is that each definition is compiled into a method which bears a unique name. That unique name is a hash generated from the definition. ... — committed to PHP-DI/PHP-DI by mnapoli 5 years ago
- Attempt to fix #648 and keep the container idempotent The idea is that each definition is compiled into a method which bears a unique name. That unique name is a hash generated from the definition. ... — committed to PHP-DI/PHP-DI by mnapoli 5 years ago
- Merge pull request #654 from PHP-DI/revert-6.0.7 Fix #648 by reverting 6.0.7 changes — committed to PHP-DI/PHP-DI by mnapoli 5 years ago
I’ve tagged a new release with the fixes.
@hultberg thank you for your support…
Reproduced it here: https://github.com/holtkamp/PHP-DI/blob/patch-648/tests/IntegrationTest/CompiledContainerTest.php#L89-L91