flawlol/facade

Symfony 的 Facade 逻辑

v1.1.4 2024-08-27 12:23 UTC

This package is auto-updated.

Last update: 2024-09-28 07:34:30 UTC


README

Scrutinizer Code Quality Build Status Code Intelligence Status StyleCI

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Symfony - Facade

作者

概述

此项目为 Symfony 应用程序提供了一个 facade 实现方案。Facade 模式用于提供对复杂子系统的简化接口。在这种情况下,Facade 与容器交互以管理依赖关系。

安装

要安装此包,请使用 Composer

composer require flawlol/facade

用法

定义 Facade

要定义一个 facade,创建一个扩展 Facade 抽象类并实现 getFacadeAccessor 方法的类。此方法应返回 facade 将与之交互的容器中服务的名称。

<?php

namespace App\Facade;

use Flawlol\Facade\Abstract\Facade;

class MyFacade extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return 'my_service';
    }
}

使用 Facade

一旦定义了 facade,就可以使用它来调用底层服务的相关方法。

use App\Facade\MyFacade;

$result = MyFacade::someMethod($arg1, $arg2);

设置容器

容器在捆绑包启动过程中自动设置。确保您的捆绑包扩展 FacadeBundle

<?php

namespace Flawlol\Facade;

use Flawlol\Facade\Abstract\Facade;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class FacadeBundle extends Bundle
{
    public function boot(): void
    {
        parent::boot();

        $container = $this->container;

        Facade::setContainer($container);
    }
}

异常

此包定义了以下异常

  • ContainerIsAlreadySetException:在尝试多次设置容器时抛出。
  • ContainerIsNotSetException:在尝试使用 facade 而未设置容器时抛出。

IDE 帮助器

flawlol/facade-ide-helper 包提供了一个用于为 Symfony 中的 facade 生成 IDE 帮助文件的命令。建议使用此包来改进 IDE 自动完成和静态分析。

还建议将 flawlol/facade-ide-helper 包用作开发依赖项。

composer require --dev flawlol/facade-ide-helper

_ide-helper.php 文件提供了帮助类,以改进 IDE 自动完成和静态分析。这些帮助器充当实际服务方法的代理,使您在 IDE 中处理 facade 更加容易。

要生成 facade 帮助器,请运行以下命令:php bin/console app:generate-facade-helpers

示例

以下示例演示了为名为 Arr 的 facade 生成的帮助类

<?php

namespace App\Facade {
    class Arr
    {
        /**
         * @param array $array
         * @param string $keyPath
         * @param mixed $defaultValue
         * @return mixed
         */
        public static function get(array $array, string $keyPath, mixed $defaultValue = NULL): mixed
        {
            /** @var \App\Service\Common\Array\ArrayHelper $instance */
            return $instance->get($array, $keyPath, $defaultValue);
        }
    }
}
  • 命名空间:App\Facade
  • 类:Arr
  • 方法:get(array $array, string $keyPath, mixed $defaultValue = null)

Arr 类提供了一个静态方法 get,用于使用键路径从数组中检索值。此方法充当 ArrayHelper 服务的 get 方法的代理,允许您使用 facade 编写更干净、更易读的代码。

实际示例

如果您有如下服务

<?php

namespace App\Service\Common\Array;

class ArrayHelper
{
    public function get(array $array, string $keyPath, mixed $defaultValue = null): mixed
    {
        // implementation
    }
}

您可以使用 facade 如此,并且 IDE 将提供自动完成和类型提示

use App\Facade\Arr;

$result = Arr::get($array, 'key.path', 'default');

在 Facade 类内部

确保在 Facade 类的 getFacadeAccessor 方法中注册了服务。

<?php

namespace App\Facade;

use Flawlol\Facade\Abstract\Facade;

class Arr extends
{
    protected static function getFacadeAccessor(): string
    {
        return ArrayHelper::class;
    }
}

许可协议

本项目是开源软件,采用 MIT 许可协议