alicemajere/wonderland-container

A small simple service container to include in projects

1.1.0 2018-10-21 15:03 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Code Intelligence Status License

Wonderland Container

A small simple service container to include in projects

Recent Update

Update 1.1.0 available. Check the release page for the changelog

Installation

composer require alicemajere/wonderland-container

Usage

Initializing the container

Create a new instance of the container

$container = new Wonderland\Container\Container();

Create a new service definition and register it in the container with the following parameters

  • [STRING] service name
  • [STRING] class name
  • [ARRAY] constructor parameters
  • [ARRAY] methods calls with their arguments. The index is the method name, the value is the array of the method parameters
// creating a service definition
$definition = new Wonderland\Container\Service\ServiceDefinition(
    'service.name',
    MyClass::class,
    ['args1', '@service.name2', ['array_args']],
    ['methodCall1' => ['args1', '@service.name3']]
);

// register the definition into the container
$container->addService($definition);

You can also create a new service by directly injecting an object instance

$instance = new MyClass();
$serviceInstance = new Wonderland\Container\Service\InstanceDefinition(
    'service.name2',
    $instance
);

// register the instance definition into the container
$container->addServiceInstance(serviceInstance);

Using the container

To check if a service exists into the container

if ($container->has('service.name')) {
    // code here
}

To retrieve the service instance

// retrieve the instance. Will create a new one if not created yet. Shared by default
$instance = $container->get('service.name');

// retrieve a new instance of the service. Setting the second parameters to true will not retrieve the shared service
 instance
$newInstance = $container->get('service.name', true);

// retrieve the instance of the instance service definition. Setting the second parameter to true will do nothing if 
// the service is not a definition service but a instance definition service; only the shared instance will be retrieved
$instance = $container->get('service.name2');

Loading configuration with YAML files

$container = new \Wonderland\Container\ServiceContainer();
$serviceLoader = new \Wonderland\Container\Configuration\ServiceLoader(new YamlParser());
// load every yml in a folder
$container->loadServices($serviceLoader->loadDirectory(__DIR__ . '/Resource/'));
// or a single file
$container->loadServices($serviceLoader->loadFile(__DIR__ . '/Resource/test.yml'));

Yaml file examples

services:
    service.name:
        class: Wonderland\Container\Example\Yml\SampleClass
        arguments:
            - 'param1'
            - 'param2'
        calls:
            callMethod:
                - 'param11'
                - '@service.name3'
    service.name2:
        class: Wonderland\Container\Example\Yml\SampleClass
        arguments:
            - 'param3'
            - 'param4'
        calls:
            callMethod:
                - '@service.name'
                - 'param44'

    service.name3:
        class: \DateTime             

Examples

You can check out the examples folder for more running examples of the library.

Prerequisites

PHP >= 7.2

Getting help

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

Authors