BEAR.Sunday 的 AOP 工具

0.1.0 2019-12-15 13:03 UTC

This package is auto-updated.

Last update: 2024-08-23 15:10:06 UTC


README

Build Status

formal-bears/aop

BEAR.Sunday 的 AOP 工具

安装

composer require formal-bears/aop:^0.1

特性

  • 等于匹配器
  • HTTP 方法匹配器

用法

想要拦截资源的 HTTP 方法时的实现示例

// モジュールでの束縛
$this->bindInterceptor(
    $this->matcher->subclassesOf(ResourceObject::class),
    new IsHttpMethodMatcher(),  // HTTPメソッドマッチャー
    [FooInterceptor::class] // 束縛したいインターセプター
);

资源拦截器的用途主要是当想创建适合自己的框架(开发基础)时。虽然 BEAR 可以作为标准集合使用,但由于其开放性,也可以创建框架。

要求

>= PHP 7.2

实现介绍(代码片段)

// リソースのメソッドのためのカスタムマッチャー
class IsHttpMethodMatcher extends EqualsToMatcher
{
    public function __construct()
    {
        parent::__construct([
            'onGet',
            'onPost',
            'onPut',
            // ... (省略)
        ]);
    }
}
// 上記 `IsHttpMethodMatcher` のスーパークラス。
// 同一かどうかのカスタムマッチャー。
use Ray\Aop\AbstractMatcher;

class EqualsToMatcher extends AbstractMatcher
{
    /**
     * @var array
     */
    private $values;

    /**
     * @param string|array $values
     */
    public function __construct($values)
    {
        parent::__construct();

        $this->values = (array) $values;
    }

    /**
     * {@inheritdoc}
     */
    public function matchesClass(\ReflectionClass $class, array $arguments)
    {
        return in_array(strtolower($class->getName()), array_map('strtolower', $this->values));
    }

    /**
     * {@inheritdoc}
     */
    public function matchesMethod(\ReflectionMethod $method, array $arguments)
    {
        return in_array(strtolower($method->getShortName()), array_map('strtolower', $this->values));
    }
}

版权

版权 (c) 2019 Atsuhiro Kubo, Nana Yamane, 所有权利保留。

许可

BSD 2-Clause 许可证