Skip to content

Incompatibility with PHP-DI 7.1: NativeProxyFactory not accepted by Container constructor #5

@Robbertvermeulen

Description

@Robbertvermeulen

Description

When using x-wp/di with php-di/php-di version 7.1.x, a fatal TypeError is thrown because the Container constructor expects ?DI\Proxy\ProxyFactory but receives DI\Proxy\NativeProxyFactory.

Error

PHP Fatal error: Uncaught TypeError: XWP\DI\Container::__construct(): Argument #2 ($proxyFactory) must be of type ?DI\Proxy\ProxyFactory, DI\Proxy\NativeProxyFactory given, called in .../vendor/php-di/php-di/src/ContainerBuilder.php on line 161 and defined in .../vendor/x-wp/di/src/Container.php:55

Root Cause

PHP-DI 7.1 introduced NativeProxyFactory as a new class that does not extend ProxyFactory. When no proxy directory is configured, ContainerBuilder::build() now instantiates NativeProxyFactory instead of ProxyFactory:

// PHP-DI ContainerBuilder.php:161
$proxyFactory = $this->proxyDirectory
    ? new NativeProxyFactory()
    : new ProxyFactory($this->proxyDirectory);

However, XWP\DI\Container::__construct() has a strict type hint:

public function __construct(
    array|MutableDefinitionSource $definitions = array(),
    ?ProxyFactory $proxyFactory = null,  // <-- Only accepts ProxyFactory
    ?ContainerInterface $wrapperContainer = null,
)

Environment

  • PHP-DI version: 7.1.1
  • x-wp/di version: 2.0.0-alpha.9
  • PHP version: 8.x

Suggested Fix

Update the type hint in Container.php to accept both factory types:

public function __construct(
    array|MutableDefinitionSource $definitions = array(),
    ProxyFactory|NativeProxyFactory|null $proxyFactory = null,
    ?ContainerInterface $wrapperContainer = null,
)

Workaround

Pin php-di/php-di to ^7.0 <7.1 in composer.json until this is resolved.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions