Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Container.php
README.md
composer.json

README.md

Container

The Container adds handy methods to most common Pimple functions and then adds automatic injection of dependencies from container to class constructor.

Installation

composer require webiik/container

Example

$container = new \Webiik\Container\Container();
$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});
$array = $container->get('\Webiik\Arr\Arr');

Adding

addService

addService(string $name, callable $factory): void

addService() ads service factory to container. It returns always same instance of service.

$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});

If you need to access container inside $factory:

$container->addService('Service', function ($container) {
    // $container - Container    
});

addServiceFactory

addServiceFactory(string $name, callable $factory): void

addServiceFactory() ads service factory to container. It returns always new instance of service.

$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});

addParam

addParam(string $name, $val): void

addParam() ads parameter to container.

$container->addParam('foo', 'bar');

addFunction

addFunction(string $name, callable $function): void

addFunction() ads function to container.

$container->addFunction('myFn', function ($a, $b) {
    return $a * $b;
});

Check

isIn

isIn(string $name): bool

isIn() checks if service, parameter or function is stored in container.

$container->isIn('\Webiik\Arr\Arr');

Getting

get

get(string $name)

get() returns service, parameter or function from container.

$array = $container->get('\Webiik\Arr\Arr');

Dependency Injection

Container provides automatic dependency injection from Container to class controller using the method injectTo(string $className): array. However it requires to follow these naming conventions:

Inject Service by Class Name

  1. Add service with same name as full name of underlying class:
    $container->addService('\Webiik\Arr\Arr', function () {
       return new \Webiik\Arr\Arr();   
    });
  2. Use full class name as type parameter in controller in your class:
    public function __construct(\Webiik\Arr\Arr $array)
    {
        $this->array = $array;
    }

    Container will search for service with name \Webiik\Arr\Arr.

  3. Inject dependencies to class:
    $myClass = new MyClass(...$container->injectTo('MyClass'));

Inject Service by Service Name

  1. Add service with name matching the following regex ws[A-Z]:
    $container->addService('wsArray', function () {
       return new \Webiik\Arr\Arr();   
    });
  2. Add class name alias to your class:
    use Webiik\Arr\Arr as wsArray;
  3. Add doc block with parameter type to controller of your class:
    /**
    * @param wsArray $array
    */   
    public function __construct(wsArray $array)
    {
        $this->array = $array;
    }

    Container will search for service with name wsArray.

  4. Inject dependencies from container to your class:
    $myClass = new MyClass(...$container->injectTo('MyClass'));
    

Inject Function or Parameter

  1. Add parameter with any name:
    $container->addFunction('myFnName', function() {
        echo 'Hello!';
    });
  2. Use parameter name in constructor in your class:
    public function __construct($myFnName)
    {
        $myFnName(); // Hello
    }

    Container will search for parameter with name myParamName.

  3. Inject dependencies from container to your class:
    $myClass = new MyClass(...$container->injectTo('MyClass'));

Inject Container Itself

  1. Use full Container class name as type parameter in constructor in your class:
    public function __construct(\Webiik\Container\Container $container)
    {
        $this->container = $container;
    }
  2. Inject dependencies from container to your class:
    $myClass = new MyClass(...$container->injectTo('MyClass'));

Resources

You can’t perform that action at this time.