PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Cannot use callable class to build service #40

Closed carc1n0gen closed 6 years ago

carc1n0gen commented 6 years ago

Hello, I am having trouble switching from anonymous function to a callable class when building my services:

// bootstrap/app.php

require 'autoload.php';

$app = new App\App;
$container = $app->getContainer();

return $app;
// App class
class App extends DIApp
{
    protected function configureContainer(ContainerBuilder $builder)
    {
        $builder->addDefinitions(__DIR__.'/../config.php');
        $builder->addDefinitions([
            'notFoundHandler' => new Providers\NotFoundProvider(),
        ]);
    }
}
// NotFoundProvider
class NotFoundProvider
{
    public function __invoke($c)
    {
        return new NotFoundHandler($c->get(Twig::class));
    }
}

For some reason I get an instance of Slim\Http\Request instead of the container passed when the callable gets called.

PHP Fatal error: Call to undefined method Slim\Http\Request::get()

EDIT: I tried type hinting the container with no luck:

PHP Catchable fatal error: Argument 1 passed to App\Providers\SessionMiddlewareProvider::__invoke() must implement interface Psr\Container\ContainerInterface, instance of Slim\Http\Request given

mnapoli commented 6 years ago

In PHP-DI closures are shortcuts to the factory() definition. But if you use any other kind of callable you need to use factory() explicitly, else PHP-DI will not consider it as a factory.

What happens here instead is that the NotFoundProvider class is considered as the service, and so Twig calls it as the "not found handler" with a $request parameter.

Have a look here for more details: http://php-di.org/doc/php-definitions.html#factories

carc1n0gen commented 6 years ago

Awesome thanks, I'll use this approach