ride/wba-task

Ride框架耗时的任务UI

0.1.2 2016-08-24 12:31 UTC

This package is auto-updated.

Last update: 2024-09-13 01:48:09 UTC


README

本模块为运行不同耗时任务添加了UI。

任务

对于您想要提供的每个任务,您需要实现Task接口。

您可以继承AbstractTask来省略明显的函数。

定义您的任务

为了让应用程序知道您的任务,请在您的dependencies.json中添加对它的依赖定义

{
    "dependencies": [
        {
            "interfaces": "ride\\web\\base\\task\\Task",
            "class": "my\\TestTask",
            "id": "test"
        }
    ]
}

代码示例

查看以下代码示例以开始您的旅程

<?php

namespace my;

use ride\library\i18n\translator\Translator;
use ride\library\form\FormBuilder;
use ride\library\system\file\browser\FileBrowser;

use ride\web\base\task\AbstractTask;

class TestTask extends AbstractTask {

    /**
     * Name of this task, should be the same as your dependency id
     * @var string
     */
    const NAME = 'test';

    /**
     * Constructs a new test task
     * @param \ride\library\system\file\browser\FileBrowser $fileBrowser Let's 
     * use the file browser to retrieve the job result
     */
    public function __construct(FileBrowser $fileBrowser) {
        $this->fileBrowser = $fileBrowser;
    }

    /**
     * Hook to prepare the form to ask for extra arguments
     * @param \ride\library\form\FormBuilder $form
     * @param \ride\library\i18n\translator\Translator $translator
     * @return null
     */
    public function prepareForm(FormBuilder $form, Translator $translator) {
        // if you need extra information for your task, you can use this hook
        // to prepare a form which asks for these arguments
        $form->addRow('test', 'string', array(
            'validators' => array('required' => array()),
        ));
    }

    /**
     * Gets the queue job of this task
     * @param array $data Extra arguments from the form
     * @return \ride\library\queue\job\QueueJob Job to invoke
     */
    public function getQueueJob(array $data) {
        // your extra arguments, as defined in prepareForm, will be passed on to 
        // this method
        
        // you should return a QueueJob which holds the logic of your task
        return new TestQueueJob();
    }

    /**
     * Gets the result of this task
     * @param string $queueJobId Id of the invoked queue job
     * @return mixed
     */
    public function getResult($jobId) {
        // extract the result 
        $application = $this->fileBrowser->getApplicationDirectory();

        return $application->getChild('data/test-' . $jobId . '.txt');
    }

}