PHPackages                             degraciamathieu/manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. degraciamathieu/manager

ActiveLibrary[Framework](/categories/framework)

degraciamathieu/manager
=======================

Implementation of the Manager pattern existing in Laravel framework.

v3.1.0(5mo ago)9175.9k↓27.5%21MITPHPPHP ^8.0CI passing

Since Mar 2Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/DeGraciaMathieu/Manager)[ Packagist](https://packagist.org/packages/degraciamathieu/manager)[ Docs](https://github.com/degraciamathieu/manager)[ RSS](/packages/degraciamathieu-manager/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (11)Used By (1)

[![Build Status](https://camo.githubusercontent.com/e9c901897a374e0d0a3736f106662dc5c18a85772e9fa9035ccc9de400f151b2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44654772616369614d6174686965752f4d616e616765722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/DeGraciaMathieu/Manager/)[![Code Coverage](https://camo.githubusercontent.com/d5e0ce18636511d8e9390bae2cfb7be65626eeeceaa780078b378cb04687fee8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44654772616369614d6174686965752f6d616e616765722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/DeGraciaMathieu/manager/?branch=master)[![Latest Version on Packagist](https://camo.githubusercontent.com/c92556f9d08975b1cc783291a69574f5be34ba9be6f5dbb58c563bb36c93648f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656772616369616d6174686965752f6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/degraciamathieu/manager)[![](https://camo.githubusercontent.com/67a2ec1ac43af7e7bfd72700b3af2ce25e0ae741e8e4f1931f61ef3feb1bc07d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656772616369616d6174686965752f6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/degraciamathieu/manager)

DeGraciaMathieu/Manager
=======================

[](#degraciamathieumanager)

Implementation of the Manager pattern existing in Laravel framework.

- [Installation](#installation)
- [Usage](#usage)
- [Work with singleton](#work-with-singleton)
- [Example with Laravel](#example-with-laravel)

Installation
------------

[](#installation)

Manager3.\*2.\*1.\*php ^8.1✅❌❌php 8.0.\*✅✅❌php 7.\*❌❌✅```
composer require degraciamathieu/manager

```

Usage
-----

[](#usage)

This package offers an abstract class `DeGraciaMathieu\Manager\Manager` which needs to be extended to implement the creation of various Driver classes :

```
namespace App\Managers;

use DeGraciaMathieu\Manager\Manager;

class WeatherManager extends Manager {

    public function createOpenweathermapDriver()
    {
        return new Openweathermap();
    }

    public function getDefaultDriver(): string
    {
        return 'openweathermap';
    }
}
```

A driver is a class integrating all the logic of an implementation, in our examples the interactions with the APIs of [Openweathermap](https://openweathermap.org/api) :

```
namespace App\Managers;

class Openweathermap {

    public function itsRainingNow(string $city): bool
    {
        // call Openweathermap api to know if it is raining in this city

        return true;
    }
}
```

From now on, you can directly call the method of a driver directly from the manager :

```
(new WeatherManager())->itsRainingNow('Paris'); // true
```

The manager will call the `itsRainingNow` method of the default driver configured by the `getDefaultDriver` method.

You can also call any driver from the manager's `driver` method :

```
(new WeatherManager())->driver('openweathermap')->itsRainingNow('Paris');
```

Now if you want to create a new implementation, for example if you want to use [Aerisweather](https://www.aerisweather.com/develop/api/) APIs, you just have to create a new driver in your manager :

```
namespace App\Managers;

use DeGraciaMathieu\Manager\Manager;

class WeatherManager extends Manager {

    public function createOpenweathermapDriver()
    {
        return new Openweathermap();
    }

    public function createAerisweatherDriver()
    {
        return new Aerisweather();
    }

    public function getDefaultDriver(): string
    {
        return 'openweathermap';
    }
}
```

> Tip, the `getDefaultDriver` method is the perfect place to use a configuration or environment variable !

Add an interface to the drivers
-------------------------------

[](#add-an-interface-to-the-drivers)

For more consistency it is advisable to implement an interface to the different drivers :

```
namespace App\Managers;

interface Driver {
    public function itsRainingNow(string $city): bool;
}
```

You obviously need to add this interface to your drivers.

```
namespace App\Managers;

use DeGraciaMathieu\Manager\Manager;

class WeatherManager extends Manager {

    public function createOpenweathermapDriver(): Driver
    {
        return new Openweathermap();
    }

    public function createAerisweatherDriver(): Driver
    {
        return new Aerisweather();
    }

    public function getDefaultDriver(): string
    {
        return 'openweathermap';
    }
}
```

Now you will be assured that each driver instantiated by the manager will have the same interface.

Repository class
----------------

[](#repository-class)

To control side effects of drivers, it is advisable to create a class encapsulating the instance of a driver, this class is usually called `Repository` :

```
namespace App\Managers;

use DeGraciaMathieu\Manager\Manager;

class WeatherManager extends Manager {

    public function createOpenweathermapDriver(): Repository
    {
        $driver = new Openweathermap();

        return new Repository($driver);
    }

    public function createAerisweatherDriver(): Repository
    {
        $driver = new Aerisweather();

        return new Repository($driver);
    }

    public function getDefaultDriver(): string
    {
        return 'openweathermap';
    }
}
```

The repository is a class providing a bridge between your application and the driver :

```
namespace App\Managers;

class Repository {

    public function __construct(
        private Driver $driver,
    ){}

    public function itsRainingNow(string $city): bool
    {
        return $this->driver->itsRainingNow($city);
    }
}
```

> This repository class is an anti-corruption layer

Thus, your application will never be aware of which driver it is handling, because it will always be encapsulated in a class repository.

The repository is also a good place if you need to add specific logic for all drivers.

Work with singleton
-------------------

[](#work-with-singleton)

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

With the `singleton` property you will only create one instance of `Openweathermap` driver :

```
