jd297/psr-container

Simple implementation of PSR-11 (Container Interface)

1.0.0 2024-02-06 01:47 UTC

This package is auto-updated.

Last update: 2024-04-06 02:11:04 UTC


README

Simple implementation of PSR-11 (Container Interface).

Requirements

The following versions of PHP are supported by this version.

  • PHP ^8.1

Usage

use Acme\Service\ExampleService;
use Jd297\Psr\Clock\SystemClock;
use Jd297\Psr\Container\Container;
use Jd297\Psr\Logger\Handler\FileHandler;
use Jd297\Psr\Logger\Logger;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;

$container = new Container();

$container->add('projectDir', fn () => __DIR__.'/..');

$container->add(ClockInterface::class, fn () => new SystemClock());

$container->add(LoggerInterface::class, function (Container $container) {
    return new Logger(
        $container->get(ClockInterface::class),
        [
            new FileHandler(sprintf('%s/var/log/dev.log', $container->get('projectDir')))
        ]
    );
});

/** @var ExampleService $exampleService */
$exampleService = $container->add(ExampleService::class)->get(ExampleService::class);
$exampleService->setLogger($container->get(LoggerInterface::class)); // using the LoggerAwareTrait

$exampleService->execute();

Composer

Scripts

Reformat code with PHP CS Fixer.

$ composer reformat

Test source code with PHPUnit.

$ composer unit

Analyse files with PHPStan (Level 9).

$ composer analyse

License

The BSD 2-Clause "Simplified" License (BSD-2-Clause). Please see License File for more information.