symbid/chainlink-bundle

此包已被废弃,不再维护。作者建议使用 dms/chainlink-bundle 包。

Chainlink 的包装器,为 Symfony 提供了链式责任模式的即插即用实现。提供了标签以将处理程序注入到配置定义的上下文中。


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

此包包装了 Chainlink 库,并基于 Symfony 服务标签提供了一种即插即用的解决方案,以实现链式责任模式。它允许您通过配置设置多个上下文,并定义哪些标签为每个上下文提供处理程序。

安装

要获取 Bundle 代码,运行

composer require dms/chainlink-bundle

编辑您的 AppKernel.php 文件以实例化 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...

        new DMS\Chainlink\Bundle\DMSChainlinkBundle(),
    );
}

如果您正在寻找其他框架,请检查 Packagist 以获取包装器和适配器。

使用方法

要注册新的上下文并将处理程序分配给它们,只需在您的 config.yml 文件中添加配置条目。

dms_chainlink:
    contexts:
        my_new_context:
            tag: mycontext.handler

该包将查找任何带有上述定义的 tag 标记的服务,并将它们作为处理程序注入到您请求的上下文中。

要处理请求,从容器中检索上下文并将输入传递给它。

$this->container->get('dms_chainlink.context.my_new_context')->handle($input);

//or its also aliased at

$this->container->get('my_new_context')->handle($input);

用作处理程序的服务需要实现 Chainlink 的 HandlerInterface。处理程序的责任是识别它负责的输入,接口中包含一个用于此目的的 handles 方法。

链式处理顺序

从版本 0.3 开始,Chainlink 支持使用在 Symfony 中广泛使用的优先级系统来排序处理程序。处理程序将从高到低调用。

# src/Vendor/MyBundle/Resources/config/services.yml

my_service:
  class: MyHandler
  tag:
    - { name: my_new_context, priority: 1 }

my_other_service:
  class: OtherHandler
  tag:
    - { name: my_new_context, priority: 9001 }

在这种情况下,如果 OtherHandler 可以处理该用例,它将首先被调用,然后是 MyHandler