定制 degraciamathieu/manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

degraciamathieu/manager

Composer 安装命令:

composer require degraciamathieu/manager

包简介

Implementation of the Manager pattern existing in Laravel framework.

README 文档

README

Build Status Code Coverage Latest Version on Packagist

DeGraciaMathieu/Manager

Implementation of the Manager pattern existing in Laravel framework.

Installation

Manager 3.* 2.* 1.*
php ^8.1
php 8.0.*
php 7.*
composer require degraciamathieu/manager

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 :

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 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

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

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

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 :

<?php

$weatherManager = new WeatherManager(singleton: true);

$weatherManager->driver('openweathermap')->itsRainingNow('Paris');
$weatherManager->driver('openweathermap')->itsRainingNow('Paris');
$weatherManager->driver('openweathermap')->itsRainingNow('Paris');

by default, singleton property value is False

Example with Laravel

Usage example of the pattern manager in a Laravel project.

degraciamathieu/manager 适用场景与选型建议

degraciamathieu/manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 87.35k 次下载、GitHub Stars 达 92, 最近一次更新时间为 2020 年 02 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「manager」 「laravel」 「DesignPattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 degraciamathieu/manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 degraciamathieu/manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 degraciamathieu/manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 87.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 92
  • 点击次数: 20
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 92
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-29