定制 flawlol/facade 二次开发

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

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

flawlol/facade

最新稳定版本:v1.1.4

Composer 安装命令:

composer require flawlol/facade

包简介

Facade logic for Symfony

README 文档

README

Scrutinizer Code Quality Build Status Code Intelligence Status StyleCI

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

Symfony - Facade

Author

Overview

This project provides a facade implementation for Symfony applications. The facade pattern is used to provide a simplified interface to a complex subsystem. In this case, the facade interacts with a container to manage dependencies.

Installation

To install the package, use Composer:

composer require flawlol/facade

Usage

Defining a Facade

To define a facade, create a class that extends the Facade abstract class and implements the getFacadeAccessor method. This method should return the name of the service in the container that the facade will interact with.

<?php

namespace App\Facade;

use Flawlol\Facade\Abstract\Facade;

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

Using the Facade

Once the facade is defined, you can use it to call methods on the underlying service.

use App\Facade\MyFacade;

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

Setting the Container

The container is automatically set during the bundle boot process. Ensure that your bundle extends 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);
    }
}

Exceptions

The package defines the following exceptions:

  • ContainerIsAlreadySetException: Thrown when attempting to set the container more than once.
  • ContainerIsNotSetException: Thrown when attempting to use the facade without setting the container.

IDE Helper

The flawlol/facade-ide-helper package provides a command to generate IDE helper files for facades in Symfony. Its recommended to use this package to improve IDE autocompletion and static analysis.

Its also recommended to use the flawlol/facade-ide-helper package as a dev dependency.

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

The _ide-helper.php file provides helper classes to improve IDE autocompletion and static analysis. These helpers act as proxies to the actual service methods, making it easier to work with facades in your IDE.

To generate the facade helpers, run the following command: php bin/console app:generate-facade-helpers

Example

The following example demonstrates the helper class generated for a facade named Arr:

<?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);
        }
    }
}
  • Namespace: App\Facade
  • Class: Arr
  • Method: get(array $array, string $keyPath, mixed $defaultValue = null)

The Arr class provides a static method get to retrieve values from an array using a key path. This method acts as a proxy to the get method of the ArrayHelper service, allowing you to use the facade for cleaner and more readable code.

Real World Example

If you you have a service like this:

<?php

namespace App\Service\Common\Array;

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

You can use the facade like this, and the IDE will provide autocompletion and type hints:

use App\Facade\Arr;

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

Inside the Facade Class

Make sure the Service is register in getFacadeAccessor method in the Facade class.

<?php

namespace App\Facade;

use Flawlol\Facade\Abstract\Facade;

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

License

This project is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-24