jagarsoft/laravel-tactician

Laravel 实现的 Tactician 命令总线

v1.0.9 2022-02-14 23:13 UTC

This package is auto-updated.

Last update: 2024-09-17 00:49:43 UTC


README

Ross Tuck 实现的 Command Bus Tactician 的 Laravel 实现。

Build Status Code Coverage Packagist Downloads Scrutinizer Code Quality

警告

原始包已被 废弃。作者已指定此存储库为官方替代品。当前标签已废弃 Joselfonseca 命名空间。下一个版本将被命名为 Jagarsoft。

安装

要安装此更新,请运行以下命令更新您的 composer.json:composer require jagarsoft/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', [], []);

您可以将 Command 的 构造函数 的输入数据数组映射到一个普通参数列表或数组本身。例如

    // 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'){
            //...
        }
    }
    
    // ... or receive array of input data itself 
        Class SomeCommand {
            public function __construct(array $data = [
                '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 自动生成 Commands 和 Handlers

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)。有关更多信息,请参阅 许可证文件