承接 geekcell/container-facade 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

geekcell/container-facade

最新稳定版本:1.0.0

Composer 安装命令:

composer require geekcell/container-facade

包简介

A simple library for creating static facades for PSR-11 compatible container services.

README 文档

README

Unit tests workflow status Coverage Bugs Maintainability Rating Quality Gate Status

A standalone PHP library heavily inspired by Laravel's Facade implementation, which can be used with any PSR-11 compatible dependency injection container (DIC) such as (the ones used by) PHP-DI, Symfony, Pimple, or Slim.

Installation

To use this package, require it with Composer.

composer install geekcell/container-facade

Motivation

Although rare, there are situations when you want to obtain a container service without dependency injection. An example would be the AggregateRoot pattern, which allows dispatching domain events directly from the aggregate, which is usually created directly and not via a DIC. In such a case, a corresponding (static) service facade can provide a comparable convenience as a singleton, but without the inherent disadvantages of the singleton pattern.

Usage

Let's imagine you have a Logger service inside your DIC of choice that logs a message into a file.

<?php

namespace App\Service;

// ...

class Logger
{
    public function __construct(
        private readonly FileWriter $writer,
    ) {
    }

    public function log(string $message, LogLevel $level = LogLevel::INFO): void
    {
        $line = sprintf(
            '%s (%s): %s', 
            (new \DateTime)->format('c'),
            $level->value,
            $message,
        );

        $this->writer->writeLine($line);
    }
}

If you want to "facade" this service, just create a class which extends GeekCell\Facade\Facade.

<?php

namespace App\Support\Facade;

use App\Service\Logger as LoggerRoot;
use GeekCell\Facade\Facade;

class Logger extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return 'app.logger';
    }
}

You'll have to implement the getFacadeAccessor() method, which returns the identifier for service inside your DIC.

Additionally, you have to "introduce" your DIC to the Facade. How to do this really depends in the framework you're using. In Symfony, a good opportunity to do so is to override the boot() method within src/Kernel.php.

<?php

namespace App;

use GeekCell\Facade\Facade;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    public function boot()
    {
        parent::boot();

        // This is where the magic happens!
        Facade::setContainer($this->container);
    }
}

To use the facade within any part of your application, just call the service as you would a static method. Behind the scenes, the call is delegated to the actual container service via __callStatic.

<?php

// ...

use App\Support\Facade\Logger;

class SomeClass
{
    public function doStuff()
    {
        Logger::log('Calling ' __CLASS__ . '::doStuff()', LogLevel::DEBUG);

        // The acutal method logic ...
    }
}

Testing

Although the above looks like an anti pattern, it's actually very testing friendly. During unit testing, you can use the swapMock() method to literally swap the real service with a Mockery mock.

<?php

// ...

use App\Support\Facade\Logger;
use PHPUnit\Framework\TestCase;

class SomeClassTest extends TestCase
{
    public function tearDown(): void
    {
        Logger::clear();
    }

    // ...

    public function testDoStuff(): void
    {
        // Swap real service with mock
        $loggerMock = Logger::swapMock();

        // Set expectations for mock
        $loggerMock->shouldReceive('log')->once();

        $out = new SomeClass();
        $result = $out->doStuff(); // This will now call the mock!

        // Test assertions ...
    }
}

Hint: You must call the clear() method to clear out the internally cached mock instance. For PHPUnit, you could use the tearDown() method to do so.

A Word of Caution

With great power comes great responsibility.

While there are valid use cases, and even though service facades offer a high level of convenience, you should still use them only sparingly and revert to standard dependency injection whenever possible, because all facades interally rely on PHP's __callStatic magic method, which can make debugging more cumbersome/difficult.

Examples

See the examples directory for various sample projects with a minimal integration of this package.

Framwork Sample project
Slim examples/slim
Symfony examples/symfony

geekcell/container-facade 适用场景与选型建议

geekcell/container-facade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.36k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 geekcell/container-facade 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-18