### Install State Machine Bundle via Composer Source: https://github.com/winzou/statemachinebundle/blob/master/README.md This snippet shows the Composer command to install the state machine bundle into your PHP project. It ensures all dependencies are managed correctly. ```sh composer require winzou/state-machine-bundle ``` -------------------------------- ### State Machine Operations Source: https://github.com/winzou/statemachinebundle/blob/master/README.md Illustrates common operations performed on a state machine instance, such as checking if a transition is possible, applying a transition, getting the current state of the object, and listing all available transitions. ```php // Check if a transition can be applied: returns true or false $articleSM->can('a_transition_name'); // Apply a transition $articleSM->apply('a_transition_name'); // Get the actual state of the object $articleSM->getState(); // Get all available transitions $articleSM->getPossibleTransitions(); ``` -------------------------------- ### Get State Machine Instance with Factory Source: https://github.com/winzou/statemachinebundle/blob/master/README.md Shows how to retrieve a state machine instance for a specific domain object and graph name using the `SM\Factory\Factory`. This is the initial step to interact with the state machine for a given entity. ```php public function myAwesomeAction($id, \SM\Factory\Factory $factory) { // Get your domain object $article = $this->getRepository('MyAwesomeBundle:Article')->find($id); // Get the state machine for this object, and graph called "simple" $articleSM = $factory->get($article, 'simple'); } ``` -------------------------------- ### Configure State Machine Graph Source: https://github.com/winzou/statemachinebundle/blob/master/README.md This YAML configuration defines a state machine graph for a domain object, specifying states, transitions between states, and associated callbacks (guard, before, after). It maps the state machine logic to a specific class and property. ```APIDOC winzou_state_machine: my_bundle_article: class: My\Bundle\Entity\Article # class of your domain object property_path: state # property of your object holding the actual state (default is "state") graph: simple # name of the graph (default is "default") # list of all possible states: states: - new - pending_review - awaiting_changes - accepted - published - rejected # list of all possible transitions: transitions: create: from: [new] to: pending_review ask_for_changes: from: [pending_review, accepted] to: awaiting_changes submit_changes: from: [awaiting_changes] to: pending_review approve: from: [pending_review, rejected] to: accepted publish: from: [accepted] to: published # list of all callbacks callbacks: # will be called when testing a transition guard: guard_on_submitting: on: 'submit_changes' # call the callback on a specific transition do: ['@my.awesome.service', 'isSubmittable'] # will call the method of this Symfony service args: ['object'] # arguments for the callback # will be called before applying a transition before: update_reviewer: on: 'create' do: ['@my.awesome.service', 'update'] args: ['object'] # will be called after applying a transition after: email_on_publish: on: 'publish' do: ['@my.awesome.service', 'sendEmail'] args: ['object', '"Email title"'] ``` -------------------------------- ### Register State Machine Bundle in Symfony Source: https://github.com/winzou/statemachinebundle/blob/master/README.md This PHP code demonstrates how to register the State Machine Bundle within your Symfony application's kernel. This step is crucial for the bundle to be recognized and utilized. ```php // app/AppKernel.php public function registerBundles() { return array( // ... new winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(), ); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.