ClanCats Logo

Container

Default Implementation

The recommended implementation is using the container factory, builder and namespace. It will genreate an optimized PHP file containing our Container, include it and return an instnace.

Make sure your php can write into the given cache directory.

$factory = new \ClanCats\Container\ContainerFactory(__DIR__ . '/cache');

$container = $factory->create('AppContainer', function($builder)
{
    // create a new container file namespace and parse our `app.ctn` file.
    $namespace = new \ClanCats\Container\ContainerNamespace();
    $namespace->parse(__DIR__ . '/app.ctn');

    // import the namespace data into the builder
    $builder->importNamespace($namespace);
});

Debug mode

The container factory will not check for changes, it will only rebuild if the cache file is missing. In development always clearing the cache directory can be annoying so there is a debug mode which will ignore the existing cache file and always rebuild.

use \ClanCats\Container\ContainerFactory;
$factory = new ContainerFactory(__DIR__ . '/cache', true); // second argument turns debug mode on

 Container class name

The first argument you pass into the container factories create method is the containers name including the namespace.

$container = $factory->create('Acme\DI\AppContainer', function($builder) {});

Read more about this here: Container Factory


Container namespace

Note: continue here: Container Namepsace