vestin/laravel-tactician

Laravel 实现的 Tactician 命令总线,源自 joselfonseca/laravel-tactician

1.0.2 2022-03-11 06:21 UTC

This package is auto-updated.

Last update: 2024-08-28 07:44:33 UTC


README

Ross Tuck 实现的 Laravel Tactician,命令总线 Tactician 的实现。

Build Status Code Coverage Total Downloads Scrutinizer Code Quality SensioLabsInsight

安装

要安装此更新,请在 composer.json 中运行 composer require joselfonseca/laravel-tactician

>= laravel5.5

ServiceProvider 将自动附加。

其他

一旦下载了依赖项,请将服务提供者添加到您的 config/app.php 文件中

    'providers' => [
        ...
        Joselfonseca\LaravelTactician\Providers\LaravelTacticianServiceProvider::class
        ...
    ]

安装完成!

使用方法

要使用命令总线,您可以从 Laravel 容器中解析总线,如下所示

    $bus = app('Joselfonseca\LaravelTactician\CommandBusInterface');

或者您可以将它注入到一个类的构造函数中

    use Joselfonseca\LaravelTactician\CommandBusInterface;

    class MyController extends BaseController
    {

        public function __construct(CommandBusInterface $bus)
        {
            $this->bus = $bus;
        }

    }

一旦您有了总线实例,您就可以添加要分发的命令的处理程序

    $bus->addHandler('SomeCommand', 'SomeHandler');

现在您可以使用中间件来分发命令

    // first parameter is the class name of the command
    // Second parameter is an array of input data to be mapped to the command
    // Third parameter is an array of middleware class names to be added to the stack
    $bus->dispatch('SomeCommand', [], []);

您可以将命令的构造函数的输入数据数组与一个简单的参数列表或数组本身进行映射。例如

    // Send parameters in an array of input data ...    
    $bus->dispatch('SomeCommand', [
        'propertyOne'   => 'One',
        'propertyTwo'   => 'Two',
        'propertyThree' => 'Three',
    ], []);
    
    // ... and receive them as individual parameters ... 
    Class SomeCommand {
        public function __construct($propertyOne = 'A', $propertyTwo = 'B', $propertyThree = 'C'){
            //...
        }
    }
    

当然,您可以使用默认值!

有关 tactician 命令总线的更多信息,请访问 http://tactician.thephpleague.com/

示例

查看这个示例,该示例展示了在简单的创建订单命令中实现的包 https://gist.github.com/joselfonseca/24ee0e96666a06b16f92

绑定

您可以通过以下方式发布配置文件来配置定位器、屈折、提取器和默认总线的绑定

    php artisan vendor:publish

然后您可以修改每个类名,它们将来自 Laravel 容器进行解析

    return [
        // The locator to bind
        'locator' => 'Joselfonseca\LaravelTactician\Locator\LaravelLocator',
        // The inflector to bind
        'inflector' => 'League\Tactician\Handler\MethodNameInflector\HandleInflector',
        // The extractor to bind
        'extractor' => 'League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor',
        // The bus to bind
        'bus' => 'Joselfonseca\LaravelTactician\Bus'
    ];

生成器

您可以使用 artisan 自动生成命令和处理程序

artisan make:tactician:command Foo
artisan make:tactician:handler Foo

这将创建 FooCommand 和 FooHandler 并将它们放置在 app/Commands 文件夹中,请注意,CommandHandler 将分别添加到类名中,所以在上面的示例中创建的类将是 FooCommandFooHandler

要同时运行它们

artisan make:tactician Foo

包含的中间件

Laravel tactician 包含了一些您可以在命令中使用的有用中间件。

  • 数据库事务:此中间件将在数据库事务中运行命令,如果抛出任何异常,则事务不会提交,数据库将保持完整,您可以在 Joselfonseca\LaravelTactician\Middleware\DatabaseTransactions 中找到此中间件。

变更日志

请参阅发布页面 https://github.com/joselfonseca/laravel-tactician/releases

测试

要运行此包中的测试,请导航到项目的根目录并运行

    composer install

然后

    vendor/bin/phpunit

贡献

请参阅 CONTRIBUTING 以获取详细信息。

安全

如果您发现任何安全问题,请通过 jose at ditecnologia dot com 发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件