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

Most upvoted comments

I’ve tagged a new release with the fixes.

@hultberg thank you for your support…