Workflow procedures for PHP
🔔 Subscribe to the newsletter to don't miss any update regarding Chevere.
A Workflow is a configurable stored procedure that will run one or more jobs. Jobs are independent from each other, but interconnected as you can pass response references between jobs. Jobs supports conditional running based on variables and previous job responses.
Install with Composer.
composer require chevere/workflow
Workflow provides the following functions at the
||Create workflow made of named jobs|
||Create synchronous blocking job|
||Create asynchronous non-blocking job|
||Define workflow-level variable|
||Define a job response reference|
- A Job is defined by its Action
- Jobs are independent from each other, define shared variables using function
- Reference [job A response] -> [job B input] by using function
Run live example:
The basic example Workflow defines a greet for a given username. The job
greet is a named argument and it takes the
GreetAction plus its run arguments.
use function Chevere\Workflow\workflow; use function Chevere\Workflow\sync; $workflow = workflow( greet: sync( new GreetAction(), username: variable('username'), ), ); $variables = [ 'username' => $argv ?? 'Walala', ]; $run = run($workflow, $variables); echo $run->getResponse('greet')->string(); // Hello, Walala!
Run live example:
For this example Workflow defines an image resize procedure in two sizes. All jobs are defined as async, but as there are dependencies between jobs (see
response) the system resolves a suitable run strategy.
use function Chevere\Workflow\workflow; use function Chevere\Workflow\async; use function Chevere\Workflow\response; use function Chevere\Workflow\variable; $workflow = workflow( thumb: async( new ImageResize(), file: variable('image'), fit: 'thumbnail', ), poster: async( new ImageResize(), file: variable('file'), fit: 'poster', ), storeThumb: async( new StoreFile(), file: response('thumb'), path: variable('savePath'), ), storePoster: async( new StoreFile(), file: response('poster'), path: variable('savePath'), ) );
The graph for the Workflow above shows that
poster run async, just like
storePoster but the storage jobs run after the first dependency level gets resolved.
graph TD; thumb-->storeThumb; poster-->storePoster;
run to run the Workflow, variables are passed as named arguments.
use function Chevere\Workflow\run; $run = run( $workflow, image: '/path/to/image-to-upload.png', savePath: '/path/to/storage/' ); // Alternative syntax $variables = [ 'image' => '/path/to/image-to-upload.png', 'savePath' => '/path/to/storage/' ]; $run = run($workflow, ...$variables);
getResponse to retrieve a job response as a
CastArgument object which can be used to get a typed response.
$thumbFile = $run->getResponse('thumb')->string();
Actions must support serialization for being used on
async jobs. For not serializable Actions as these interacting with connections (namely streams, database clients, etc.) you should use
Documentation is available at chevere.org.
Copyright 2023 Rodolfo Berrios A.
This software is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.