| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the LineMob package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Ishmael Doss <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace LineMob\Core\Workflow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Workflow\DefinitionBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Workflow\Registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Workflow\Transition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Workflow\Validator\StateMachineValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Workflow\Validator\WorkflowValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Workflow\Workflow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author phakpoom <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | class WorkflowRegistry implements WorkflowRegistryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var Registry | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private $registry; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var EventDispatcherInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected $dispatcher; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @var boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     protected $isDevMode; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param EventDispatcherInterface|null $dispatcher | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param bool $isDevMode | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function __construct(EventDispatcherInterface $dispatcher = null, $isDevMode = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->registry = new Registry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->dispatcher = $dispatcher; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |         $this->isDevMode = $isDevMode; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @return DefinitionValidatorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     private function getDefinitionValidator(array $config) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         if ('state_machine' === $config['type']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             return new StateMachineValidator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return new WorkflowValidator('single_state' === $config['marking_store']); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function register(array $config) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $config = array_replace_recursive( | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 'name' => get_class($this), | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 'places' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 'supports' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 'transitions' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 'type' => 'workflow', | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 'marking_store' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                     'type' => 'single_state', | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                     'arguments' => ['state'], | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             $config | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         if ($this->isDevMode && empty($config['supports'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             throw new \InvalidArgumentException("The `supports` can't be empty, Please provide entity classes."); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $transitions = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $definitionBuilder = new DefinitionBuilder(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $definitionBuilder->addPlaces((array)$config['places']); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         foreach ((array)$config['transitions'] as $name => $transition) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             if ('workflow' === $config['type']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 $transitions[] = new Transition($name, $transition['from'], $transition['to']); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             } elseif ('state_machine' === $config['type']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 foreach ((array)$transition['from'] as $from) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                     foreach ((array)$transition['to'] as $to) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                         $transitions[] = new Transition($name, $from, $to); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         $definitionBuilder->addTransitions($transitions); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $definition = $definitionBuilder->build(); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         $markingStoreArguments = $config['marking_store']['arguments']; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         $marking = new SingleStateMarkingStore(...$markingStoreArguments); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         if ('multiple_state' === $config['marking_store']['type']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             $marking = new MultipleStateMarkingStore(...$markingStoreArguments); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         if ($this->isDevMode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             $this->getDefinitionValidator($config)->validate($definition, $config['name']); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         $workflow = new Workflow($definition, $marking, $this->dispatcher, $config['name']); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         foreach ($config['supports'] as $class) { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             $this->registry->add($workflow, $class); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function get($subject, $workflowName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |         return $this->registry->get($subject, $workflowName); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 139 |  |  |  |