PHP Classes

PHP Manager Classes: Manage the access to multiple driver classes

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 88 All time: 9,980 This week: 560Up
Version License PHP version Categories
manager 1.0The PHP License7Language, Design Patterns, PHP 7
Description 

Author

This package can be used to manage the access to multiple driver classes

It provides an abstract class that can take as parameter the name of a given driver class to be accessed.

Implementation classes should used this manager class to extend it so they provide different functions that create all types of supported driver objects.

The implementation classes can also provide a function to return the name of the default driver class in case no driver name was previously specified.

The manager class returns the object of the selected driver class object, so it an be called directly by the application.

Innovation Award
PHP Programming Innovation award nominee
March 2020
Number 3
Many types of components implement an abstraction that allows applications to use the same interface functions to execute operations that can be implemented by different driver classes.

One common example of this type of abstraction is the access to SQL based databases. It allows accessing databases from many vendors using the same interface functions.

This package implements a class manager component that can be used to implement this type of abstraction by the means of different driver classes.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 21 packages by
Country: France France
Age: ???
All time rank: 289780 in France France
Week rank: 4 Up1 in France France Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Documentation

<p align="center"> <img src="https://i.ibb.co/vq97y2t/laravel-manager.jpg" width="300"> </p>

<p align="center"> <a href="https://travis-ci.org/DeGraciaMathieu/manager"><img src="https://travis-ci.org/DeGraciaMathieu/manager.svg?branch=master" alt="Build Status"></a> <a href="https://scrutinizer-ci.com/g/DeGraciaMathieu/manager/?branch=master"><img src="https://scrutinizer-ci.com/g/DeGraciaMathieu/manager/badges/coverage.png?b=master" alt="Code Coverage"></a> <a href="https://packagist.org/packages/degraciamathieu/manager"><img src="https://img.shields.io/packagist/v/degraciamathieu/manager.svg?style=flat-square" alt="Latest Version on Packagist"></a> <a href='https://packagist.org/packages/degraciamathieu/manager'><img src='https://img.shields.io/packagist/dt/degraciamathieu/manager.svg?style=flat-square' /></a> </p>

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.

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.

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.

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:

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

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

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

Example with Laravel:

Usage example of the pattern manager in a Laravel project.


  Files folder image Files (6)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file Manager.php Class Class source

  Files folder image Files (6)  /  tests  
File Role Description
  Plain text file ManagerTests.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:88
This week:0
All time:9,980
This week:560Up