定制 mortenscheel/task-flow 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mortenscheel/task-flow

最新稳定版本:0.2.0

Composer 安装命令:

composer require mortenscheel/task-flow

包简介

Pipeline for nested console tasks

README 文档

README

GitHub Workflow Status (master) Total Downloads Latest Version License

Task Flow

Build dynamic nested console workflows with ease.

Installation

You can install the package via composer:

composer require mortenscheel/task-flow

Getting started

Example flow

Example

Laravel example

use Scheel\TaskFlow\Context;
use Scheel\TaskFlow\Facades\TaskFlow;
use Scheel\TaskFlow\Task;

TaskFlow::run([
    Task::make('Closure', function (Context $context) {
        // Do stuff
        usleep(500000);
        // Set context data for future tasks
        $context->set('foo', true);
    }),
    Task::make('Conditional', function (Context $context) {
        // Read context data from previous tasks
        if ($context->get('foo')) {
            // Skip current task, including child tasks
            $context->skip();
        }
    }),

    // Task action can be an invokable class
    Task::make('Invokable', new SomeInvokable),
    Task::make('Nested tasks', children: [
        Task::make('Subtask 1', $this->classMethod(...)),
        Task::make('Subtask 2', children: [
            Task::make('Subtask 2.1', children: [
                Task::make('Subtask 2.1.1', fn () => sleep(1)),
                Task::make('Subtask 2.1.2', fn () => sleep(1)),
            ]),
            Task::make('Subtask 2.2', fn () => sleep(1)),
        ]),
    ]),
    Task::make('Final task', fn () => sleep(1)),
]);

Standalone example

use Scheel\TaskFlow\Renderer\ConsoleRenderer;
use Scheel\TaskFlow\Task;
use Scheel\TaskFlow\TaskManager;
use Symfony\Component\Console\Output\ConsoleOutput;

// The TaskManager has to be constructed manually
$manager = new TaskManager(new ConsoleRenderer(new ConsoleOutput()));
// But the rest of the API is the same, whether you use Laravel or not
$manager->run([
    Task::make('Task 1', fn (): null => null),
    Task::make('Task 2', children: [
        Task::make('Task 2.1', fn (): null => null),
        Task::make('Task 2.2', children: [
            Task::make('Task 2.2.1', fn () => fn(): null => null),
        ]),
    ]),
    Task::make('Task 3', fn (): null => null),
]);

Configuration

The default configuration is as follows:

[
    'indent' => 2,
    'symbols' => [
        'pending' => '',
        'running' => '',
        'completed' => '',
        'skipped' => '',
        'failed' => '',
    ],
    'colors' => [
        'pending' => 'gray',
        'running' => 'bright-white',
        'completed' => 'green',
        'skipped' => 'yellow',
        'failed' => 'red',
    ],
]

Laravel

You can publish the config file with:

php artisan vendor:publish --provider="Scheel\TaskFlow\TaskFlowServiceProvider" --tag="config"

Standalone

You can pass a custom configuration array to the ConsoleRenderer constructor:

$manager = new TaskManager(new ConsoleRenderer(new ConsoleOutput(), [
    'symbols' => [
        'pending' => '💤',
        'running' => '🏃‍♂️️',
        'completed' => '🎉',
        'failed' => '😭',
        'skipped' => '',
    ]
]));

Task Context

The Context is a shared object, which is passed to all task actions.

  • set(string $key, mixed $value): void - Set a value in the context for use by future tasks.
  • get(string $key): mixed - Get a value from the context, provided by previous tasks.
  • has(string $key): bool - Check if a key exists in the context.
  • increment(string $key, int $amount = 1): void - Increment a value in the context.
  • skip(): void - Skip the current task, including all child tasks.
  • updateTitle(string $title): void - Update the title of the current task.
  • abort(): void - Abort the entire task flow. This has the same effect as throwing an exception.
  • interactive(callable $callback): mixed - Allows for user interaction, without interfering with the task flow status output.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-29