2017 © Pedro Peláez
 

library injector

InjectorServiceProvider

image

gonzalo123/injector

InjectorServiceProvider

  • Monday, October 19, 2015
  • by gonzalo123
  • Repository
  • 1 Watchers
  • 7 Stars
  • 21 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

InjectorServiceProvider

Build Status, (*1)

Alternative way to define service providers in Silex, (*2)

Let's say we've got this Math class, (*3)

namespace Foo

class Math
{
    public function sum($i, $j)
    {
        return $i+$j;
    }
}

And we want to use it within a Silex application, (*4)

include __DIR__ . "/../vendor/autoload.php";

use Silex\Application;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app['math'] = function () {
    return new Math();
};

$app->get("/", function () use ($app) {
    return $app['math']->sum(1, 2);
});

$app->run();

We have one Service available in $app['math'], but, what's the type of the class? We need to inspect Math class to figure out what public functions are available. Sometimes I'm a bit lazy to do that, and because of that I've developed this small service provider to allow us to use a different approach to define our service providers., (*5)

include __DIR__ . "/../vendor/autoload.php";

use Silex\Application;
use Injector\InjectorServiceProvider;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app->register(new InjectorServiceProvider([
    'Foo\Math' => 'math',
]));

$app['math'] = function () {
    return new Math();
};

$app->get("/", function (Math $math) {
    return $math->sum(1, 2);
});

$app->run();

And that's all. Our InjectorServiceProvider allows us to define the class provided by the service provider, and its Silex/Pimple key name in the Dependency Injection Container, (*6)

The Versions

19/10 2015

dev-master

9999999-dev

InjectorServiceProvider

  Sources   Download

MIT

The Requires

 

silex service provider

20/09 2015

1.0

1.0.0.0

InjectorServiceProvider

  Sources   Download

MIT

The Requires

 

silex service provider