Skip to content

Implementation of the Manager pattern existing in Laravel framework

Notifications You must be signed in to change notification settings

DeGraciaMathieu/Manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Code Coverage PHP range Latest Version on Packagist

DeGraciaMathieu/Manager

Implementation of the Manager pattern existing in Laravel framework.

Installation

composer require degraciamathieu/manager

Usage

This package offers an abstract class Manager which needs to be extended to implement the creation of various Driver classes.

<?php

use DeGraciaMathieu/Manager/Manager;

class LoggerManager extends Manager {

    public function createMonologDriver(): LoggerDriver
    {
        return new MonologDriver();
    }

    public function createMockDriver(): LoggerDriver
    {
        return new MockDriver();
    }

    public function getDefaultDriver()
    {
        return 'monolog';
    }
}

The getDefaultDriver method should also be implemented in your class Manager, in order to determine which driver has to be created by default. It's also the right spot to determine the default driver from an environment variable, or a configuration.

<?php

public function getDefaultDriver()
{
    return env('MANAGER_LOGGER_DEFAULT_DRIVER');
}

In a matter of consistency, all Driver creations (createClientDriver, createMockDriver...) should return a class which itself implements the same interface, the LoggerDriver contract in this here case.

<?php

interface LoggerDriver {
    public function doAnything();
}

class MonologDriver implements LoggerDriver {

    public function doAnything()
    {
        echo 'i do anything from the monolog driver';
    }
}

class MockDriver implements LoggerDriver {

    public function doAnything()
    {
        echo 'i do anything from the mock driver';
    }
}

From now on, it's possible to use your Manager, either by using the default driver:

<?php

(new LoggerManager())->doAnything(); // i do anything from the monolog driver

Or by simply specify the driver which needs to be instantiated.

<?php

(new LoggerManager())->driver('monolog')->doAnything(); // i do anything from the monolog driver
(new LoggerManager())->driver('mock')->doAnything(); // i do anything from the mock driver

Work with singleton

You can also cache the creation of Drivers with the $singleton property

<?php

use DeGraciaMathieu/Manager/Manager;

class LoggerManager extends Manager {

    /**
     * @var boolean
     */
    protected $singleton = true;

    public function createMonologDriver(): LoggerDriver
    {
        return new MonologDriver();
    }    
}

With the singleton property you will only create one instance of MonologDriver

<?php

$loggerManager = new LoggerManager();

$loggerManager->driver('monolog')->doAnything();
$loggerManager->driver('monolog')->doAnything();
$loggerManager->driver('monolog')->doAnything();

Example with Laravel

Usage example of the pattern manager in a Laravel project.

About

Implementation of the Manager pattern existing in Laravel framework

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •