raoul2000 / yii2-workflow

A simple workflow engine for Yii2
BSD 3-Clause "New" or "Revised" License
171 stars 48 forks source link

allowed to use fully namespaced workflowid to be declared in the behavior declaration #17

Closed juancrobles closed 8 years ago

juancrobles commented 8 years ago

Hi, I was testing your extension when I ran into a problem, your extension only allows me to have defined the workflow in "app\models", but my setup requires to locate the definition elsewhere, so I made a couple of changes, to allow to define the right place where my workflow is located; this allow me to have a library of workflows and access them in any place... of course if no declaration is placed the behavior is unchanged

expample of declaration:

public function behaviors()
{
    return [
        'workflow' => [
            'class' => SimpleWorkFlowBehavior::className(),
            'defaultWorkflowId' => 'app\modules\workflow\models\AwesomeWorkflow',
        ],
    ];
}

cheers

P.S. great work with this extension

raoul2000 commented 8 years ago

hi @juancrobles ... and thanks for your contribution. The use case you're facing should already be handled by yii2-workflow as it is a common case to load the worlfow from another location than the default app\modelsnamespace.

To load the workflow from a custom namespace you have to declare and initialize the workflow source component (i.e. without relying on the default initialization settings). This is described in the documentation as the standard way.

$config = [
    'components' => [
        'workflowSource' => [
          'class' => 'raoul2000\workflow\source\file\WorkflowFileSource',
          'definitionLoader' => [
              'class' => 'raoul2000\workflow\source\file\PhpClassLoader',
              'namespace'  => '@app/models/workflows'
           ]
        ],

Another option is to make use of a special alias : @workflowDefinitionNamespace ... in the documentation this is called the magic alias.

Yii::setAlias('@workflowDefinitionNamespace','app\\modules\\workflow\\models');

Check the PHP Class Loader chapter in the doc.

Your PR introduces a dependency between the SimpleWorkflowBehavior and the underlying workflowSource component, between the id of your workflow and the way it is stored. For instance, today my workflow is defined as a PHP class, but maybe tomorrow I want to store it in DB and in this case, having a workflow Id equals to app\modules\workflow\models\AwesomeWorkflowis not appropriate.

Try to use the standard or the magic alias way and hopefully it will reply to your requirements with minimal effort.

:sunglasses: