xiaoma404/laravel-tactician

Laravel的Tactician命令总线实现,基于vestin/laravel-tactician进行分支

1.0.4 2022-09-23 06:29 UTC

This package is auto-updated.

Last update: 2024-09-23 10:48:49 UTC


README

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

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