



Build Status

Canteen is a simple dependency injection container concept. It originated as a gist on github following a late night discussion and some beer

Many of the DI implementations currently in use in the PHP community look more like service locators then dependency injection containers. There is a nice little piece that discusses this conundrum here.

So the goals of this experiment are simple...

  1. Lazily load objects
  2. Encourage legitimate injection over and above location, ie. avoid a registry object

Example usages

Basic loading of services and retrieving of them

// Create the Canteen Service container
$canteen = new Canteen();
// Fill it with something...
$canteen->fill('foo', new Foo());
// or like this...
// or like this...
$canteen['foo'] = new Foo();
// or like this... (great for lazy loading!)
$canteen->fill('foo', function(){
    return new Foo();

// Next 'pour' or fetch an item from the service container
$foo = $canteen->pour('foo');
// or like this...
$foo = $canteen['foo'];

Basic autowiring of a class and it's service(s)

// Standard class 'Foo' with service 'Bar'
class Foo {
    public $bar;

    public function __construct(\Bar $bar) {
        $this->bar = $bar;

// Create the Canteen Service container
$canteen = new Canteen();

// Use this instead of $foo = new Foo();
$foo = $canteen->autowire('\Foo');

// Notice that $foo->bar has been filled for us

Recent activity

Stan Lemon

Commits by Stan Lemon were pushed to stanlemon/canteen

6275741 - Merge branch 'feature/hotwire' into develop
cc6c2db - Added a way to not autowire by type hints and then added a separate method for short handing the hotwire process since we are going ...
ce8d0dd - I feel like I was in a dark and sad spot when I wrote this example originally.
5b16fe4 - Added a filtering capability to autowiring and a test for doing it with an interface.
f6a88be - Restructured test mock objects, this seems a little more clean. Got rid of the ones not being used as well.
e0aaa9e - And this was the actual bootstrap file...
c118cef - Added a different bootstrap that has a simple autoloader for the mock objects I am going to make. Stole the autoloader from
9799165 - Made makeNameFromClass public and wrote a series of tests for it.
6c44887 - Merge branch 'feature/reorganizing' into develop
67fca3b - Missed an instance
Tip: Use camelCasing e.g. ProjME to search for