unity/container

Unity framework dependency manager.

0.1 2018-11-12 14:00 UTC

This package is auto-updated.

Last update: 2024-04-13 03:11:12 UTC


README

An out of box dependency manager for PHP projects.

Introduction

Let's take a look at this class:

class Logger
{
    protected $fileLogger;

    public function __construct(FileLogger $fileLogger)
    {
        $this->fileLogger = $fileLogger;
    }

    public function log($message)
    {
        return $this->fileLogger->log($message);
    }
}

The problem with this class is that it's coupled to a specific Logger.

What if one day we change our mind and want to start sending our logs via email? We need to get back to this class and change the logger from FileLogger to EmailLogger

Refatoring:

class Logger
{
    protected $driver;

    public function __construct(LoggerDriverInterface $driver)
    {
        $this->driver = $driver;
    }

    public function log($message)
    {
        return $this->driver->load($message);
    }
}

That way our class can accept any kind of file drivers and is coupled to a contract instead of a concrete implememnation.

But what if you have a lot of classes to manage?

There's where the container comes.

Installation

composer require unity/container

Usage