Skip to content

phillipsdata/minphp-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minphp/Container

Build Status Coverage Status

A standards compliant Pimple-based container.

Installation

Install via composer:

composer require minphp/container:~2.0

Basic Usage

<?php
use Minphp\Container\Container;

$container = new Container();

// Add/Update
$container->set('queue', function($c) {
    return new \SplQueue();
});

// Verify
$queue_exists = $container->has('queue');

// Fetch
$queue = $container->get('queue');

// Remove
$container->remove('queue');

You can also use the container as an array, as per Pimple. Though, using the ContainerInterface defined methods is preferable as it eases switching to a different container if you ever need to.

<?php
use Minphp\Container\Container;

$container = new Container();
$container['queue'] = function($c) {
    return new \SplQueue();
};

$queue = $container['queue'];

ContainerInterface

interface ContainerInterface
{
    // inherited from Interop\Container\ContainerInterface
    public function get($id);

    // inherited from Interop\Container\ContainerInterface
    public function has($id);

    public function set($id, $value);

    public function remove($id);
}

ContainerAwareInterface

Also provided is the ContainerAwareInterface, which allows your classes to declare that they support container injection. Nifty for granting objects access to your container (you weren't thinking of using a static class, were you?).

interface ContainerAwareInterface
{
    public function setContainer(Minphp\Container\ContainerInterface $container = null);

    public function getContainer();
}

Usage

<?php
namespace myApp\Controller;

use Minphp\Container\ContainerAwareInterface;
use Minphp\Container\ContainerInterface;

class MyController implements ContainerAwareInterface
{
    private $container = null;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function getContainer()
    {
        return $this->container;
    }
}

About

A Dependency Injection Container based on Pimple

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages