1. Go to this page and download the library: Download chevere/workflow library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chevere / workflow example snippets
use Chevere\Action\Action;
class MyAction extends Action
{
protected function main(string $foo): string
{
return "Hello, {$foo}";
}
}
use function Chevere\Workflow\{workflow,sync,variable,response};
$workflow = workflow(
greet: sync(
new MyAction(),
foo: variable('super'),
),
capo: sync(
new MyAction(),
foo: response('greet'),
),
);
use function Chevere\Workflow\variable;
// Basic variable declaration
variable('myVar');
// Usage in a job
sync(
new MyAction(),
parameter: variable('myVar')
);
use function Chevere\Workflow\run;
run($workflow, myVar: 'some value');
use function Chevere\Workflow\response;
// Basic response declaration
response('job1');
// Usage in a Workflow
workflow(
job1: sync(
new SomeAction(),
),
job2: sync(
new MyAction(),
parameter: response('job1')
);
);
use function Chevere\Workflow\response;
response('job1', 'id');
class SomeAction extends Action
{
protected function main(
string $context,
int $userId,
mixed ...$bag,
): void
{
// On runtime:
// $context = 'public'
// $userId = (( user.id response ))
// $bag = ['group' => 'admin', 'mask' => 1024]
}
}
$workflow = workflow(
user: sync(
new GetUser(),
request: variable('userId')
),
job1: sync(
new SomeAction(),
context: 'public', // As-is value
userId: variable('userId'), // Variable
group: response('user', 'group'),// Response
mask: 1024, // As-is value
);
);
run($workflow, userId: 123);
use function Chevere\Workflow\{sync,async,response,variable,workflow};
workflow(
thumb: async(
new ImageResize(),
image: variable('image'),
width: 100,
height: 100,
fit: 'thumb'
),
medium: async(
new ImageResize(),
image: variable('image'),
width: 500,
fit: 'resizeByW'
),
store: sync(
new StoreFiles(),
response('thumb', 'filename'),
response('medium', 'filename'),
),
);
use function Chevere\Workflow\run;
$run = run($workflow, ...$variables);
$thumbFile = $run->response('thumb')->string();
use function Chevere\Parameter\cast;
$id = $run->response('user', 'id')->int();
use Chevere\Workflow\WorkflowTrait;
class Something
{
use WorkflowTrait;
public function __construct()
{
$workflow = workflow(
job1: sync(
new MyAction(),
foo: variable('bar')
)
);
// Use execute to run the Workflow
$this->execute($workflow, bar: 'baz');
}
}
$some = new Something();
$bar = $some->run()->response('job1')->string();
try {
$run = run($workflow, ...$variables);
} catch (WorkflowException $e) {
// Job that thrown the exception
$e->name;
// Job instance that thrown the exception
$e->job;
// The exception thrown by the Job
$e->throwable;
}
// If using WorkflowTrait
try {
$this->execute($workflow, ...$variables);
$run = $this->run();
} catch (WorkflowException $e) {
// ...
}
use PHPUnit\Framework\TestCase;
class MyActionTest extends TestCase
{
public function testAction(): void
{
$action = new MyAction();
// 🪄 Chevere automatically validates Action I/O
$response = $action(foo: 'bar');
$this->assertSame('expected', $response);
}
}
public function testWorkflowOrder(): void
{
$expectedGraph = [
['job1', 'job2'], // parallel jobs
['job3'], // depends on job1, job2
];
$this->assertSame(
$expectedGraph,
$workflow->jobs()->graph()->toArray()
);
}
use Chevere\Workflow\Traits\ExpectWorkflowExceptionTrait;
use function Chevere\Workflow\run;
class WorkflowTest extends TestCase
{
use ExpectWorkflowExceptionTrait;
public function testFailingJob(): void
{
$this->expectWorkflowException(
closure: fn () => run($workflow, input: 'invalid'),
exception: LogicException::class,
job: 'validation',
message: 'Invalid input'
);
}
}
mermaid
graph TD;
user-->validate-->meta-->store;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.