### Full Workflow Configuration Example Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md An example of a comprehensive workflow configuration in PHP, including metadata, event dispatching, places, and transitions. ```php [ 'type' => 'workflow', // or 'state_machine', defaults to 'workflow' if omitted // The marking store can be omitted, and will default to 'multiple_state' // for workflow and 'single_state' for state_machine if the type is omitted 'marking_store' => [ 'property' => 'marking', // this is the property on the model, defaults to 'marking' 'class' => MethodMarkingStore::class, // optional, uses EloquentMethodMarkingStore by default (for Eloquent models) ], // optional top-level metadata 'metadata' => [ // any data ], 'supports' => ['App\BlogPost'], // objects this workflow supports // Specifies events to dispatch (only in 'workflow', not 'state_machine') // - set `null` to dispatch all events (default, if omitted) // - set to empty array (`[]`) to dispatch no events // - set to array of events to dispatch only specific events // Note that announce will dispatch a guard event on the next transition // (if announce isn't dispatched the next transition won't guard until checked/applied) 'events_to_dispatch' => [ Symfony\Component\Workflow\WorkflowEvents::ENTER, Symfony\Component\Workflow\WorkflowEvents::LEAVE, Symfony\Component\Workflow\WorkflowEvents::TRANSITION, Symfony\Component\Workflow\WorkflowEvents::ENTERED, Symfony\Component\Workflow\WorkflowEvents::COMPLETED, Symfony\Component\Workflow\WorkflowEvents::ANNOUNCE, ], 'places' => ['draft', 'review', 'rejected', 'published'], 'initial_places' => ['draft'], // defaults to the first place if omitted 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => 'review', // optional transition-level metadata 'metadata' => [ // any data ] ], 'publish' => [ 'from' => 'review', 'to' => 'published' ], 'reject' => [ 'from' => 'review', 'to' => 'rejected' ] ], ] ]; ``` -------------------------------- ### Simple Workflow Configuration Example Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md A minimal PHP configuration for a workflow, suitable for Eloquent models, defining supports, places, and transitions. ```php [ 'supports' => ['App\BlogPost'], // objects this workflow supports 'places' => ['draft', 'review', 'rejected', 'published'], 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => 'review' ], 'publish' => [ 'from' => 'review', 'to' => 'published' ], 'reject' => [ 'from' => 'review', 'to' => 'rejected' ] ], ] ]; ``` -------------------------------- ### Install Laravel Workflow via Composer Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This command installs the Laravel Workflow package using Composer, the dependency manager for PHP. Ensure you have Composer installed and configured for your Laravel project. ```bash composer require zerodahero/laravel-workflow ``` -------------------------------- ### Basic Laravel Workflow Operations Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Shows how to retrieve a workflow for a model, check transition capabilities, get enabled transitions, and apply a transition to change the model's state. It also demonstrates using the WorkflowTrait for direct access. ```php workflow_get(); // if more than one workflow is defined for the BlogPost class $workflow = $post->workflow_get($workflowName); $workflow->can($post, 'publish'); // False $workflow->can($post, 'to_review'); // True $transitions = $workflow->getEnabledTransitions($post); // Apply a transition $workflow->apply($post, 'to_review'); $post->save(); // Don't forget to persist the state // Get the post transitions foreach ($post->workflow_transitions() as $transition) { echo $transition->getName(); } // Apply a transition $post->workflow_apply('publish'); $post->save(); ``` -------------------------------- ### Dynamically Load Workflow Definition Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This PHP code demonstrates how to dynamically load a workflow definition into the registry using the `addFromArray` method. It retrieves the workflow registry instance, defines a workflow name and its definition array, and then registers it. The example also includes a try-catch block to handle potential `DuplicateWorkflowException` if a workflow with the same name is already registered. ```PHP make('workflow'); $workflowName = 'straight'; $workflowDefinition = [ // Workflow definition here // (same format as config/symfony docs) // This should be the definition only, // not including the key for the name. // See note below on initial_places for an example. ]; $registry->addFromArray($workflowName, $workflowDefinition); // or if catching duplicates try { $registry->addFromArray($workflowName, $workflowDefinition); } catch (DuplicateWorkflowException $e) { // already loaded } } ``` -------------------------------- ### Handling Workflow Guard Events Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Provides an example of a listener for the 'guard' event in the Laravel Workflow package. This listener checks if a blog post has a title and blocks the transition if it's empty. ```PHP getOriginalEvent(); /** @var App\BlogPost $post */ $post = $originalEvent->getSubject(); $title = $post->title; if (empty($title)) { // Posts with no title should not be allowed $originalEvent->setBlocked(true); } } /** * Handle workflow leave event. */ public function onLeave($event) { // The event can also proxy to the original event $subject = $event->getSubject(); // is the same as: $subject = $event->getOriginalEvent()->getSubject(); } /** * Handle workflow transition event. */ public function onTransition($event) {} /** * Handle workflow enter event. */ public function onEnter($event) {} /** * Handle workflow entered event. */ public function onEntered($event) {} /** * Register the listeners for the subscriber. * * @param Illuminate\Events\Dispatcher $events */ public function subscribe($events) { $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\GuardEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onGuard' ); $events->listen( ``` -------------------------------- ### Symfony Workflow Component Integration Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Illustrates how to leverage the underlying Symfony Workflow component directly within the Laravel context. This includes getting current places, definitions, and various types of metadata (workflow, place, transition). ```php workflow_get(); // Get the current places $places = $workflow->getMarking($post)->getPlaces(); // Get the definition $definition = $workflow->getDefinition(); // Get the metadata $metadata = $workflow->getMetadataStore(); // or get a specific piece of metadata $workflowMetadata = $workflow->getMetadataStore()->getWorkflowMetadata(); $placeMetadata = $workflow->getMetadataStore()->getPlaceMetadata($place); // string place name $transitionMetadata = $workflow->getMetadataStore()->getTransitionMetadata($transition); // transition object // or by key $otherPlaceMetadata = $workflow->getMetadataStore()->getMetadata('max_num_of_words', 'draft'); ``` -------------------------------- ### Using WorkflowTrait in Eloquent Model Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Example of integrating the WorkflowTrait into an Eloquent model class to enable workflow functionality. ```php 'workflow', // or 'state_machine' 'metadata' => [ 'title' => 'Blog Publishing Workflow', ], 'marking_store' => [ 'property' => 'currentPlace' ], 'supports' => ['App\BlogPost'], 'places' => [ 'review', 'rejected', 'published', 'draft' => [ 'metadata' => [ 'max_num_of_words' => 500, ] ] ], 'initial_places' => 'draft', // or set to an array if multiple initial places 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => 'review', 'metadata' => [ 'priority' => 0.5, ] ], 'publish' => [ 'from' => 'review', 'to' => 'published' ], 'reject' => [ 'from' => 'review', 'to' => 'rejected' ] ], ]; ``` -------------------------------- ### Publish Configuration File Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Command to publish the Laravel Workflow configuration file using Artisan. ```bash php artisan vendor:publish --provider="ZeroDaHero\LaravelWorkflow\WorkflowServiceProvider" ``` -------------------------------- ### Laravel Workflow Configuration: Either of Two Places Transition to One Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This PHP configuration defines a 'workflow' type for a blog publishing process. It demonstrates a scenario where a draft can transition to 'published' from either 'content_approved' OR 'legal_approved'. The 'from' property uses a simple array to represent these alternative transition sources. ```php [ 'type' => 'workflow', 'metadata' => [ 'title' => 'Blog Publishing Workflow', ], 'marking_store' => [ 'property' => 'currentPlace' ], 'supports' => ['App\BlogPost'], 'places' => [ 'draft', 'content_review', 'content_approved', 'legal_review', 'legal_approved', 'published' ], 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => ['content_review', 'legal_review'], ], // ... transitions to "approved" states here 'publish' => [ 'from' => [ 'content_review', 'legal_review' ], 'to' => 'published' ], // ... ], ] ]; ``` -------------------------------- ### Dump Laravel Workflow Diagram to Custom Storage Path Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This bash command shows how to dump a Laravel workflow diagram to a specific storage location. It utilizes the `--disk` and `--path` options to specify the storage disk (e.g., 's3') and the directory path for the output diagram. ```bash php artisan workflow:dump workflow-name --class=App\BlogPost --disk=s3 --path="workflows/diagrams/" ``` -------------------------------- ### Dump Laravel Workflow Diagram (PNG) Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This bash command uses the Laravel artisan CLI to dump a visual representation of a workflow. The command specifies the workflow name and the class it supports, defaulting to a PNG format for the output image. ```bash php artisan workflow:dump workflow_name --class App\BlogPost ``` -------------------------------- ### Workflow Configuration with Metadata Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md PHP configuration demonstrating how to add metadata to workflow places and transitions, enhancing workflow definitions. ```php [ 'type' => 'workflow', // or 'state_machine' 'metadata' => [ 'title' => 'Blog Publishing Workflow', ], 'supports' => ['App\BlogPost'], 'places' => [ 'draft' => [ 'metadata' => [ 'max_num_of_words' => 500, ] ], 'review', 'rejected', 'published' ], 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => 'review', 'metadata' => [ 'priority' => 0.5, ] ], 'publish' => [ 'from' => 'review', 'to' => 'published' ], 'reject' => [ 'from' => 'review', 'to' => 'rejected' ] ], ] ]; ``` -------------------------------- ### Dump Laravel Workflow Diagram with Metadata Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This bash command generates a workflow diagram including its associated metadata. It uses the `--with-metadata` flag with the artisan CLI to embed metadata information into the diagram. ```bash php artisan workflow:dump workflow_name --with-metadata ``` -------------------------------- ### Registering Workflow Event Listeners (Dot Syntax) Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Demonstrates how to register listeners for various workflow events using Symfony's dot syntax style. This method allows for precise event targeting based on workflow name and transition name. ```PHP listen( 'workflow.straight.guard', 'App\Listeners\BlogPostWorkflowSubscriber@onGuard' ); // workflow.leave // workflow.[workflow name].leave // workflow.[workflow name].leave.[place name] $events->listen( 'workflow.straight.leave', 'App\Listeners\BlogPostWorkflowSubscriber@onLeave' ); // workflow.transition // workflow.[workflow name].transition // workflow.[workflow name].transition.[transition name] $events->listen( 'workflow.straight.transition', 'App\Listeners\BlogPostWorkflowSubscriber@onTransition' ); // workflow.enter // workflow.[workflow name].enter // workflow.[workflow name].enter.[place name] $events->listen( 'workflow.straight.enter', 'App\Listeners\BlogPostWorkflowSubscriber@onEnter' ); // workflow.entered // workflow.[workflow name].entered // workflow.[workflow name].entered.[place name] $events->listen( 'workflow.straight.entered', 'App\Listeners\BlogPostWorkflowSubscriber@onEntered' ); // workflow.completed // workflow.[workflow name].completed // workflow.[workflow name].completed.[transition name] $events->listen( 'workflow.straight.completed', 'App\Listeners\BlogPostWorkflowSubscriber@onCompleted' ); // workflow.announce // workflow.[workflow name].announce // workflow.[workflow name].announce.[transition name] $events->listen( 'workflow.straight.announce', 'App\Listeners\BlogPostWorkflowSubscriber@onAnnounce' ); } } ``` -------------------------------- ### Registering Workflow Event Listeners (Laravel Style) Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Demonstrates an alternative method for registering workflow event listeners using the traditional Laravel event dispatcher. While functional, this approach is noted as less recommended due to potential duplicate event listening. ```PHP getOriginalEvent(); /** @var App\BlogPost $post */ $post = $originalEvent->getSubject(); $title = $post->title; if (empty($title)) { // Posts with no title should not be allowed $originalEvent->setBlocked(true); } } /** * Handle workflow leave event. */ public function onLeave($event) { // The event can also proxy to the original event $subject = $event->getSubject(); // is the same as: $subject = $event->getOriginalEvent()->getSubject(); } /** * Handle workflow transition event. */ public function onTransition($event) {} /** * Handle workflow enter event. */ public function onEnter($event) {} /** * Handle workflow entered event. */ public function onEntered($event) {} /** * Register the listeners for the subscriber. * * @param Illuminate\Events\Dispatcher $events */ public function subscribe($events) { $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\GuardEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onGuard' ); $events->listen( ``` -------------------------------- ### Register Laravel Workflow Facade Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This PHP code snippet demonstrates how to register the `Workflow` facade in your Laravel application's `config/app.php` file. This allows for easier access to the package's functionality. ```php ZeroDaHero\LaravelWorkflow\Facades\WorkflowFacade::class, ``` -------------------------------- ### Dump Laravel Workflow Diagram (JPG) Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This bash command demonstrates how to dump a Laravel workflow diagram in JPG format using the artisan CLI. It includes the workflow name, the supported class, and explicitly sets the output format to 'jpg'. ```bash php artisan workflow:dump workflow_name --format=jpg ``` -------------------------------- ### Laravel Workflow Configuration: Exactly Two Places Transition to One Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This PHP configuration defines a 'workflow' type in Laravel, specifically for a blog publishing process. It illustrates a scenario where a draft must be in both 'content_approved' and 'legal_approved' states simultaneously before transitioning to 'published'. The nested array in the 'from' property signifies this requirement. ```php [ 'type' => 'workflow', 'metadata' => [ 'title' => 'Blog Publishing Workflow', ], 'marking_store' => [ 'property' => 'currentPlace' ], 'supports' => ['App\BlogPost'], 'places' => [ 'draft', 'content_review', 'content_approved', 'legal_review', 'legal_approved', 'published' ], 'transitions' => [ 'to_review' => [ 'from' => 'draft', 'to' => ['content_review', 'legal_review'], ], // ... transitions to "approved" states here 'publish' => [ 'from' => [ // note array in array ['content_review', 'legal_review'] ], 'to' => 'published' ], // ... ], ] ]; ``` -------------------------------- ### Laravel Workflow Event Classes Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md Lists the event classes provided by the ZeroDaHero Laravel Workflow package that are fired during workflow transitions. These events can be listened to for custom logic. ```PHP ZeroDaHero\LaravelWorkflow\Events\Guard ZeroDaHero\LaravelWorkflow\Events\Leave ZeroDaHero\LaravelWorkflow\Events\Transition ZeroDaHero\LaravelWorkflow\Events\Enter ZeroDaHero\LaravelWorkflow\Events\Entered ``` -------------------------------- ### Configure Workflow Tracking Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This snippet shows how to configure the laravel-workflow package to track loaded workflows. By setting `track_loaded` to `true` in the `workflow_registry.php` configuration file, the registry will monitor which workflows have been loaded. The `ignore_duplicates` option can also be set to either ignore or throw an exception when registering duplicate workflows. ```PHP false, /** * Only used when track_loaded = true * * When set to true, a registering a duplicate workflow will be ignored (will not load the new definition) * When set to false, a duplicate workflow will throw a DuplicateWorkflowException */ 'ignore_duplicates' => false, ]; ``` -------------------------------- ### Registering Workflow Event Listeners in Laravel Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This snippet shows how to register event listeners for various workflow events within a Laravel application's EventServiceProvider. It covers events like LeaveEvent, TransitionEvent, EnterEvent, and EnteredEvent. ```php $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\LeaveEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onLeave' ); $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\TransitionEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onTransition' ); $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\EnterEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onEnter' ); $events->listen( 'ZeroDaHero\LaravelWorkflow\Events\EnteredEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onEntered' ); ``` -------------------------------- ### Register Laravel Workflow Service Provider Source: https://github.com/zerodahero/laravel-workflow/blob/develop/README.md This PHP code snippet shows how to manually register the `WorkflowServiceProvider` in your Laravel application's `config/app.php` file. This is necessary if package discovery is disabled. ```php [ ... ZeroDaHero\LaravelWorkflow\WorkflowServiceProvider::class, ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.