Skip to content

Simple, extensible and configurable dependency injection container.

License

Notifications You must be signed in to change notification settings

keulinho/container

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHPBench Service Container

Build Status

Simple, extensible dependency injection container with parameters and service tagging. Implements container interop.

Simple usage

$container = new Container();
$container->register('foobar', function (Container $container) {
    return new \stdClass();
});

Extending and Tagging

Extension classes should be passed as the first argument to the container (the user configuration is the second argumnet).

$container = new Container(
    [
        MyExtension::class
    ],
    [
        'foo.bar' => 'my_new_value',
    ]
);
$container->init(); // will trigger loading of the extensions.
class MyExtension implements ExtensionInterface
{
    public function load(Container $container)
    {
        $container->register('my_service', function (Container $container) {
            $service = new MyService(
                $container->getParameter('foo_bar'),
                $container->get('some_other_service')
            );

            foreach ($container->getServiceIdsForTag('tag') as $serviceId => $params) {
                $service->add($container->get($serviceId));
            }

            return $service;
        });

        $container->register('tagged_service', function (Container $container) {
            return new MyService(
                $container->getParameter('foo_bar'),
                $container->get('some_other_service')
            );
        }, [ 'tag' => [ 'param1' => 'foobar' ]);
    }

    /**
     * Return the default parameters for the container.
     *
     * @return array
     */
    public function getDefaultConfig()
    {
        return [
            'foo_bar' => 'this is foo'
        ];
    }
}

About

Simple, extensible and configurable dependency injection container.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%