elie29 / zend-di-config

PSR-11 PHP-DI container configurator for Laminas, Mezzio, ZF, Expressive applications or any framework that needs a PSR-11 container
MIT License
19 stars 4 forks source link

Use Expressive Skeleton installer with PHP-DI results in error? #25

Closed holtkamp closed 5 years ago

holtkamp commented 5 years ago

I have some experience with PHP-DI and now try to use it with Zend Expressive.

When using composer create-project zendframework/zend-expressive-skeleton expressive-project and selecting PHP-DI as DependencyInjectionContainer, the default installation seems to result in an error:

composer serve => then open gives:

Fatal error: Uncaught Zend\ServiceManager\Exception\InvalidArgumentException: Zend\ServiceManager\AbstractPluginManager expects a ConfigInterface or ContainerInterface instance as the first argument; received DI\Container in /user/project/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:59 Stack trace: #0 /user/project/vendor/zendframework/zend-view/src/HelperPluginManager.php(253): Zend\ServiceManager\AbstractPluginManager->__construct(Object(DI\Container), Array) #1 /user/project/vendor/zendframework/zend-expressive-zendviewrenderer/src/HelperPluginManagerFactory.php(20): Zend\View\HelperPluginManager->__construct(Object(DI\Container)) #2 [internal function]: Zend\Expressive\ZendView\HelperPluginManagerFactory->__invoke(Object(DI\Container)) #3 /user/project/ in /user/project/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php on line 59

I tried the instructions at https://github.com/elie29/zend-di-config#using-with-expressive

apparently Interop\Container\ContainerInterface is required by Zend\ServiceManager\AbstractPluginManager::__construct(), while the PHP-DI container implements Psr\Container\ContainerInterface.

but am kind of stuck here... Any suggestions? Are the involved libraries still compatible?

Should Zend Expressive switch the the PSR-11 approach?

https://github.com/container-interop/container-interop

Projects consuming container-interop interfaces are very strongly encouraged to directly type-hint on PSR-11 interfaces, in order to be compatible with PSR-11 containers that are not compatible with container-interop.

elie29 commented 5 years ago

Thank you for your feedback.

I am using PHP-DI with zend framework and zend expressive with no issues. However, you may need container-interop as suggested by zend expressive if you want to use lazy or Abstract Factory.

I tried now the installation with composer serve and everything works correctly:

image

holtkamp commented 5 years ago

@elie29 thanks for the swift response. Will try again coming week and report back here. Probably I did something wrong 😉

holtkamp commented 5 years ago

It seems related to my preference to use zend-view as template engine:

Which template engine do you want to use?
  [1] Plates
  [2] Twig
  [3] zend-view installs zend-servicemanager
  [n] None of the above

This involves zend-servicemanager, which uses Interop\Container\ContainerInterface in Zend\ServiceManager\AbstractPluginManager::__construct(), at https://github.com/zendframework/zend-servicemanager/blob/release-2.7.11/src/AbstractPluginManager.php#L12

While the PHP-DI container implements Psr\Container\ContainerInterface, see https://github.com/PHP-DI/PHP-DI/blob/6.0.5/src/Container.php#L29

Workarounds:

Not sure whether a solution exists other then "waiting" zend-servicemanager v4...

elie29 commented 5 years ago

@holtkamp thank you for the report.

I don't really use zend_view. However, zend-view involves zend-service-manager whose uses obsoleted interop-container.

Second, zend service acts as a container so I don't see the interest of using many containers in the same project.

I will close this issue as it is not related to this package we should wait zend service upgrade.

thomasvargiu commented 5 years ago

You need to decorate your PSR container for Interop container compatibility, see this comment

elie29 commented 5 years ago

No need for a decorator. Aliasing with Psr container is enough

elie29 commented 5 years ago

Until zend service manager release version 4, we have to wrap DI\Container by implementing Interop\Container\ContainerInterface

elie29 commented 5 years ago

@holtkamp I released a new version with the wrapper would you please test https://github.com/elie29/zend-di-config/releases/tag/v3.0.9

holtkamp commented 5 years ago

@elie29 works perfectly! Very elegant approach, compliments 👍

elie29 commented 5 years ago

@holtkamp thank you for testing the version. We can promote it now!