igniphp / framework

Swoole, PSR-15, PSR-7, PSR-11 lightweight modular anti-framework for REST micro-services.
MIT License
265 stars 14 forks source link

How I can register my additional services in the service provider? #30

Closed roquie closed 6 years ago

roquie commented 6 years ago

Igni\Application\Providers\ServiceProvider has one method with PSR ContainerInterface and I can't register additional there services because has and get methods not enough for this.

My goal is a create the re-usable module for PDO connection:

// ...
$serviceLocator = new ServiceLocator();
$serviceLocator->share(PDO::class, function () use ($conf) {
    $dsn = "pgsql:dbname={$conf->get('db.name')};host={$conf->get('db.host')}";
    $pdo = new PDO($dsn, $conf->get('db.user'), $conf->get('db.pwd'));
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $pdo;
});

// Setup application and routes
$app = new HttpApplication($serviceLocator);

// ...

But it's impossible with the current ServiceProvider interface. Or I'm wrong?

dkraczkowski commented 6 years ago

@roquie If you are using default service locator provided with the library you can safely use all the methods that exist within it. Just add docblock and typehint property accordingly:

<?php
class MyModule implements ServiceProvider 
{
    /**
     * @param ServiceLocator $container
     */
    public function provideServices(ContainerInterface $container): void
    {
        $container->share(...);
    }
}

Sadly php is not supporting generics yet, thats why I had to use PSR container interface.

dkraczkowski commented 6 years ago

I had no better idea for this interface, if you have any better I will be glad if you can share it here :)

roquie commented 6 years ago

Yes, I came back to tell about docblock and close issue :)

Thanks!

roquie commented 6 years ago

Maybe change docblock in the interface code of ServiceProvider? For a better understanding of course.

roquie commented 6 years ago

https://github.com/igniphp/framework/pull/31