2017 © Pedro Peláez
 

library container

simple service container

image

mooti/container

simple service container

  • Thursday, September 1, 2016
  • by lalobo
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Mooti Container

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A simple service container written in php, (*2)

Installation

You can install this through packagist, (*3)

$ composer require mooti/container

Run the tests

If you would like to run the tests. Use the following:, (*4)

$ ./vendor/bin/phpunit

Usage

Create the container and add a service, (*5)

use Mooti\Container\Container;

$container = new Container();

$container->set('logger', function () { return new Logger();});

//returns a new instance of Logger. Subsequent calls return the same object
$logger = $container->get('logger');

You also define multiple services by implementing ServiceProviderInterface and then implementing it's getServices method to return an associative array. If the element is a callable function it will be called and the result will be returned., (*6)

use Mooti\Container\ContainerAware;

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

To us the container you will need to use the ContainerAware trait in you class. You will then be able to use the get method to get any items from the container, (*7)

use Mooti\Container\ContainerAware;

class App
{
    use ContainerAware;

    public function run()
    {
        $config  = $this->get('config');
        $message = $this->get('message');

        $logger->alert($message);
    }
}

So, putting it all together you will get something like:, (*8)

use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;

class App
{
    use ContainerAware;

    public function run()
    {
        $config  = $this->get('config');
        $message = $this->get('message');

        $logger->alert($message);
    }
}

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

$container       = new Container();
$serviceProvider = new ServiceProvider::class;
$app             = new App();

$container->registerServices($serviceProvider);
$app->setContainer($container);

// Should log "Hello World"
$app->run();

The ContainerAware trait extends functionality from mooti/factory. You can instantiate an object using the method createNew and it will try to inject the container into the object if it users the ContainerAware trait., (*9)

use Mooti\Container\Container;
use Mooti\Container\ContainerAware;
use Mooti\Container\ServiceProvider\ServiceProviderInterface;

class FooBar
{
    use ContainerAware;

    private $foo;
    private $bar;

    public function __construct($foo, $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }

    public function logMessage()
    {
        $config  = $this->get('config');
        $message = $this->get('message');

        $logger->alert($this->foo . ' ' .  $this->bar . ' ' . $message);
    }
}

class App
{
    use ContainerAware;

    public function run()
    {
        $fooBar = $this->createNew(FooBar::class, ['test 1', 'test 2']);
        $fooBar->logMessage();
    }
}

class ServiceProvider implements ServiceProviderInterface
{
    public function getServices()
    {
        return [
            'logger'  => function () { return new Logger();},
            'message' => 'Hello World'},
        ];
    }
}

$container       = new Container();
$serviceProvider = new ServiceProvider::class;
$app             = new App();

$container->registerServices($serviceProvider);
$app->setContainer($container);

// Should log "test 1 test 2 Hello World"
$app->run();

The Versions

01/09 2016

dev-master

9999999-dev

simple service container

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

services container service container

01/09 2016

0.0.1

0.0.1.0

simple service container

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

services container service container