定制 symandy/progress-event 二次开发

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

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

symandy/progress-event

最新稳定版本:v0.1.0

Composer 安装命令:

composer require symandy/progress-event

包简介

Events used in Symfony services to handle console progress bar

README 文档

README

This package contains a set of events and subscribers to be used in Symfony services.

The aim is to externalize Symfony progress bar handle from services and use progress bar events instead.

Installation

composer require symandy/progress-event

Usage

  1. In your Symfony command, register one of available subscribers before calling the service where the task is executed.
<?php

use Symandy\Component\ProgressEvent\EventSubscriber\ProgressBarSubscriber;
use Symandy\Component\ProgressEvent\EventSubscriber\SymfonyStyleSubscriber;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class FooCommand extends Command
{
    private EventDispatcherInterface $eventDispatcher;

    private FooHandler $fooHandler;
 
    // Configure command 

    protected function execute(InputInterface $input, OutputInterface $output): int
    {    
        // Use the basic progress bar
        $this->eventDispatcher->addSubscriber(new ProgressBarSubscriber(new ProgressBar($output)));
     
        // Or use the Symfony style progress bar
        $io = new SymfonyStyle($input, $output);
        $this->eventDispatcher->addSubscriber(new SymfonyStyleSubscriber($io));

        $this->fooHandler->handleComplexTask();

        return Command::SUCCESS;
    }
}
  1. Dispatch the wanted events while handling some complex task
<?php

use Symandy\Component\ProgressEvent\Event\AdvanceEvent;
use Symandy\Component\ProgressEvent\Event\FinishEvent;
use Symandy\Component\ProgressEvent\Event\StartEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class FooHandler
{
    private EventDispatcherInterface $eventDispatcher;

    public function __construct(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }

    public function handleComplexTask(): void
    {
        $items = ['foo', 'bar', 'baz'];

        $this->eventDispatcher->dispatch(new StartEvent(count($items)));

        foreach ($items as $item) {
            // Handle some complex task

            $this->eventDispatcher->dispatch(new AdvanceEvent());
        }

        $this->eventDispatcher->dispatch(new FinishEvent());
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-11