phespro/container

Super simple dependency injection container for php

1.2.4 2023-11-01 19:37 UTC

This package is auto-updated.

Last update: 2024-11-30 02:02:29 UTC


README

Build Status

phespro/container

Super simple dependency injection container for php.

  • Only 121 lines of code
  • No cache required
  • Includes tagging and decorating
  • 100% line coverage & 100% mutation coverage
  • Implements PSR-11

Usage

Install it:

composer require phespro/container

Create it:

<?php

require __DIR__ . '/vendor/autoload.php';
$container = new Container;

Use it!!!

Adding Services

$container->add('some_id', fn(Container $c) => new MyService); // register singleton
$container->addFactory('other_id', fn(Container $c) => new OtherService); // register factory
$container->add('tagged_service', fn(Container $c) => new TaggedService,  ['console_command']);

Get Services

$container->has('some_id'); // does the service exist?
$container->get('some_id'); // get the service
$container->getByTag('console_command'); // get all services, tagged with 'console_command'

Decorating Services

You can decorate (or overwrite) services:

$container->decorate('some_id', fn(Container $c, callable $prev) => new Decorator($prev());

// or decorate it with factory
$container->decorateWithFactory('some_id', fn(Container $c, callable $prev) => new Decorator($prev()));