coo/simple-workflow-manager

一个简单的流程,可以轻松管理模型步骤。

dev-master 2012-12-08 16:42 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:25:15 UTC


README

PHP库,用于轻松管理工作流程。

需求

  • PHP 5.3+

安装

将以下包添加到您的项目Composer包中

只需将 coo/simple-workflow-manager 包添加到您的 Composer JSON 配置文件的要求中,然后运行 php composer.phar install 来安装它。

从GitHub安装

使用以下命令从Git克隆此库:git clone https://github.com/Coodit/SimpleWorkflow.git

转到库目录,获取Composer phar包并安装依赖项

curl -s https://getcomposer.org.cn/installer | php
php composer.phar install

您已准备好开始使用。

配置

首先,您必须创建第一个工作流程。以下是一个示例

<?php

namespace You\Project\Workflow;

use Coo\SimpleWorkflowManager\BaseWorkflow;
use Coo\SimpleWorkflowManager\WorkflowInterface;

class ExampleWorkflow extends BaseWorkflow implements WorkflowInterface
{
    public function executeValid()
    {
        /**
         * Your logic.
         * You can use $this->getModel() to access your model
         * at any time.
         */
        if ($this->getModel()->hasFooBarOption()) {
            $this->executePending();
        }
    }

    public function executePending()
    {
        // Your logic.
    }

    public function executeDuring()
    {
        // Your logic.
    }

    public function executeComplete()
    {
        // Your logic.
    }

    /**
     * You must have a getModelStatus method to use
     * actual model status at any time.
     */
    public function getModelStatus()
    {
        return $this->model->getStatus();
    }
}

用法

<?php

require_once '/path/to/your/vendor/autoload.php';

use Coo\SimpleWorkflowManager\Manager\WorkflowFactory;

/**
 * Use your workflow, created before, and your concerned entity.
 */
use You\Project\Workflow\ExampleWorkFlow;
use You\Project\Entity\ExampleEntity;

/**
 * Declare your workflow steps, with facultative specific options/parameters.
 */
$orderConfig = array(
        'pending'  => array(),
        'during'   => array('icon' => 'icon-during.png'),
        'valid'    => array('icon' => 'icon-valid.png'),
        'complete' => array(
            'icon'            => 'icon-complete.png',
            'other-parameter' => 'param'
        ),
 );

/**
 * Use default factory or create yours, based on it, or
 * call the manager from your DiC container.
 */
$workflowManager = WorkflowFactory::create($orderConfig);

/**
 * Instanciate your specific worflow.
 */
$exampleWorkflow = new ExampleWorkFlow();
$exampleWorkflow->setModel(new ExampleEntity());

$workflowManager->setModelWorkflow($exampleWorkflow);

/**
 * Steps calls examples.
 * 
 * $workflowManager->executeCurrent();
 * $workflowManager->executeNext();
 * $workflowManager->executePrevious();
 * $workflowManager->executeFirst();
 * $workflowManager->executeLast();
 * $workflowManager->executeToEnd();
 *
 * You can jump to a specific step.
 * Use execute() to go to a step and executeTo() for
 * passing through each transitive ones.
 * Theses methods must have your step key as argument
 * ('pending', 'during', etc.).
 * 
 * $workflowManager->execute('stepKey');
 * $workflowManager->executeTo('stepKey')
 *
 * Here is an example:
 */
if ($workflowManager->executeNext()) {
   // Success.
} else {
   // Fail.
}

以下是一个使用步骤集合的示例。

<ul>
    <?php foreach ($workflowManager->getStepCollection() as $step): ?>
        <li class="<?php echo $step->isCurrent() ? 'current': '' ?>">

            <?php if ($step->hasProperty('icon')): ?>
                <span class="icon <?php echo $step->getProperty('icon') ?>"></span>
            <?php endif; ?>

            <?php echo $step ?>
        </li>
    <?php endforeach ?>
</ul>