2017 © Pedro Peláez
 

library container

Simple universal Container & DIContainer

image

jakulov/container

Simple universal Container & DIContainer

  • Saturday, January 2, 2016
  • by jakulov
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Container

Simple universal PHP Container & DIContainer (Dependency Injection), (*1)

Can be installed with composer, (*2)

composer require jakulov/container

Implements Container Interoperability, (*3)

1. Container

Container could be used to store any array data (e.g. configuration or repository) and easy accessing it with dot notation., (*4)

$config = ['test' => [
        'key1' => 'value1',
    ]];
$container = \jakulov\Container\Container::getInstance($config);
echo $container->get('test.key1'); // value1

Container and DIContainer uses singleton pattern, so after first initialization of container you can access to it without passing config as argument, like:, (*5)

// second and other usages of container should not use config
$container = \jakuov\Container\Container::getInstance();

2. DIContainer

Dependency injection container should be used for manager dependencies between services in php application. This quite simple but agile implementation of DI Container., (*6)

$config = [
    'foo' => 'bar', 
    // you can use aware-interfaces to manage dependencies in app 
    'container' => [
      'di' => [
          'aware' => [
              // in any service class implements this interface and resolved
              // with DIContainer will be called setInterfaceTest method with argument
              // containing instance of service "service.interface_test"
              'Service\\InterfaceTestServiceAwareInterface' => [
                  'setInterfaceTest' => '@service.interface_test',
              ],
          ],
      ],
    ],
    // configuration of services
    'service' => [
      // service name will be "service.test"
      'test' => [
          'class' => 'Service\\TestService', // class of service
          // arguments of service __construct
          'args' => [
              'argument1',
              'argument2'
          ],
          // setters to call while service initialization
          'aware' => [
              // as dependency you can use:
              'setAnotherTestService' => '@service.another_test', // another service
              'setContainerValue' => ':foo', // container value
              'setScalarValue' => 'value', // or scalar value
          ],
      ],
      'another_test' => [
          'class' => 'Service\\AnotherTestService',
      ],
      'alias_test' => '@another_test',
      'interface_test' => [
          'class' => 'Service\\InterfaceTestService'
      ],
    ],
];

$dic = \jakulov\Container\DIContainer::getInstance($config);
$testService = $dic->get('service.test');
echo $testService->argument1; // 'argument1'
echo $testService->containerValue; // 'bar'
echo $testService->scalarValue; // 'value'
echo get_class( $testService->anotherTestService ); // Service\\AnotherTestService
echo get_class( $testService->anotherTestService->interfaceTestService ); // Service\\InterfaceTestService

Service can also resolve dependencies without declaration in config, if it's dependencies declared in class with help of aware interfaces., (*7)

$service = $dic->resolve(\Service\UnresolvedTestService::class);

If you want to make you services reusable with multiple requests, you should renew some dependencies on each request. For example it could be HTTP Request instance. If request service declared with id "request", you can provide new object like this:, (*8)

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$dic->provide('request', $request); // updated dependency in all services

Tests

Run: ./run_tests.sh, (*9)

Tests are also examples for usage library, (*10)

The Versions

02/01 2016

dev-master

9999999-dev

Simple universal Container & DIContainer

  Sources   Download

MIT

The Requires

 

The Development Requires