gixx/worstpractice-dependency-injection

A simple dependency injection container

1.1.0 2022-11-10 12:46 UTC

This package is auto-updated.

Last update: 2024-11-06 19:54:12 UTC


README

DIY Dependency Injection

PHP Version Build Status Scrutinizer Code Quality PHStan Code Coverage Packagist Package Packagist Downloads

The complete source code for the series of articles DIY Dependency Injection Container part 1, 2 and 3.

Purpose

The only purpose is practicing:

  • PHP 7.4, 8.0, 8.1 and 8.2 features
  • keep coding standards
  • write clean code
  • write strict-typed code
  • Unit testing

Although, I believe it works like any other DIC, I don't recommend to use it in production. Unfortunately the code quality checks on the Scrutinizer CI side don't yet support PHP 8.2.

Installation

To add this package to your project, just get it via composer:

composer require gixx/worstpractice-dependency-injection

Usage

To use it, you will need only a configuration as in the example:

namespace MyNamespace;

use WorstPractice\Component\DependencyInjection\ConfigParser\ArrayParser;
use WorstPractice\Component\DependencyInjection\ServiceLibrary;
use WorstPractice\Component\DependencyInjection\Container;

$config = [
    'ServiceAlias' => [
        'class' => \Namespace\To\MyClass::class,
        'arguments' => [
            \Namespace\To\OtherClassInterface::class,
            'literalParameter' => 1234,
            'otherLiteralParameter' => false
        ],
        'shared' => false           
    ],
    \Namespace\To\OtherClassInterface::class => [
        'class' => \Namespace\To\OtherClass::class,
        'shared' => true
    ],
    \DateTimeZone::class => [
        'arguments' => [
            'param' => 'Europe/Berlin'
        ],
        'shared' => true
    ],
    \DateTime::class => [
        'calls' => [
            ['setTimezone', [\DateTimeZone::class]]
        ],
        'shared' => true
    ],
    'Auth' => [
        // empty, will be determined later
    ],
    'OtherServiceAlias' => [
        'inherits' => 'ServiceAlias',
        'calls' => [
            ['someMethod', ['parameter1' => 4543, 'parameter2' => [0, 1, 2], \DateTime::class]]
        ],
        'shared' => true       
    ],
    'LoginController' => [
        'class' => \Namespace\To\Controller\Login:class,
        'arguments' => [
            'Auth',
            'OtherServiceAlias'
        ]   
    ]
];

$container = new Container(new ServiceLibrary(new ArrayParser()), $config);

$authService = $_ENV['environment'] === 'dev'
    ? new \Namespace\To\DebugAuthService()
    : new \Namepace\To\Strict\AuthenticationService();
$isShared = true;

$container->set('Auth', $authService, $isShared);

$controller = $container->get('LoginController');

This DIC will instantiate any class only when it is requested or being referenced by a requested class. It also supports adding instance into the DIC. It comes in handy when the script needs an instance based on some calculation.

Testing

The package contains a simple Docker setup to be able to run tests. For this you need only run the following:

docker-compose up -d
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar install
docker exec -it worstpractice-dependency-injection-php-fpm php composer.phar check

The following tests will run:

  • PHP lint
  • PHP Mess Detector
  • PHP Unit
  • PHPStan (max level, no ignored Errors)

PHP Unit

PHPStan

The following tests form the PHP 7.4 version are now ignored, since they don't yet have PHP 8.2 support:

  • PHP-CS-Fixer
  • PHP Code Sniffer