1
|
|
|
<?php namespace Cerbero\Workflow; |
2
|
|
|
|
3
|
|
|
use Cerbero\Workflow\Repositories\PipelineRepositoryInterface; |
4
|
|
|
use Cerbero\Workflow\Inflectors\InflectorInterface; |
5
|
|
|
use Cerbero\Workflow\Wrappers\DispatcherInterface; |
6
|
|
|
use Illuminate\Contracts\Container\Container; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Hub of the pipelines. |
10
|
|
|
* |
11
|
|
|
* @author Andrea Marco Sartori |
12
|
|
|
*/ |
13
|
|
|
class Workflow { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Andrea Marco Sartori |
17
|
|
|
* @var Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines Pipelines repository. |
18
|
|
|
*/ |
19
|
|
|
protected $pipelines; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Andrea Marco Sartori |
23
|
|
|
* @var Cerbero\Workflow\Inflectors\InflectorInterface $inflector Inflector. |
24
|
|
|
*/ |
25
|
|
|
protected $inflector; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Andrea Marco Sartori |
29
|
|
|
* @var Illuminate\Contracts\Container\Container $container Service container. |
30
|
|
|
*/ |
31
|
|
|
protected $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @author Andrea Marco Sartori |
35
|
|
|
* @var Cerbero\Workflow\Wrappers\DispatcherInterface $dispatcher Bus dispatcher. |
36
|
|
|
*/ |
37
|
|
|
protected $dispatcher; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the dependencies. |
41
|
|
|
* |
42
|
|
|
* @author Andrea Marco Sartori |
43
|
|
|
* @param Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines |
44
|
|
|
* @param Cerbero\Workflow\Wrappers\DispatcherInterface $dispatcher |
45
|
|
|
* @param Cerbero\Workflow\Inflectors\InflectorInterface $inflector |
46
|
|
|
* @param Illuminate\Contracts\Container\Container $container |
47
|
|
|
* @return void |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
PipelineRepositoryInterface $pipelines, |
51
|
|
|
InflectorInterface $inflector, |
52
|
|
|
Container $container, |
53
|
|
|
DispatcherInterface $dispatcher |
54
|
|
|
) { |
55
|
|
|
$this->pipelines = $pipelines; |
|
|
|
|
56
|
|
|
$this->inflector = $inflector; |
|
|
|
|
57
|
|
|
$this->container = $container; |
|
|
|
|
58
|
|
|
$this->dispatcher = $dispatcher; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Dynamically call pipelines. |
63
|
|
|
* |
64
|
|
|
* @author Andrea Marco Sartori |
65
|
|
|
* @param string $name |
66
|
|
|
* @param array $arguments |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function __call($name, $arguments) |
70
|
|
|
{ |
71
|
|
|
$this->failIfNotExists($name); |
72
|
|
|
|
73
|
|
|
$this->inflector->of($name); |
74
|
|
|
|
75
|
|
|
return $this->dispatchWorkflow($name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Throw an exception if the given workflow does not exist. |
80
|
|
|
* |
81
|
|
|
* @author Andrea Marco Sartori |
82
|
|
|
* @param string $workflow |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function failIfNotExists($workflow) |
86
|
|
|
{ |
87
|
|
|
if( ! $this->pipelines->exists($workflow)) |
88
|
|
|
{ |
89
|
|
|
$error = "The workflow [$workflow] does not exist."; |
90
|
|
|
|
91
|
|
|
throw new \BadFunctionCallException($error); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Dispatch the given workflow. |
97
|
|
|
* |
98
|
|
|
* @author Andrea Marco Sartori |
99
|
|
|
* @param string $workflow |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
protected function dispatchWorkflow($workflow) |
103
|
|
|
{ |
104
|
|
|
$job = $this->inflector->getJob(); |
105
|
|
|
|
106
|
|
|
$request = $this->resolveRequest(); |
107
|
|
|
|
108
|
|
|
$pipes = $this->pipelines->getPipesByPipeline($workflow); |
109
|
|
|
|
110
|
|
|
$parameters = $this->container->make('router')->current()->parameters(); |
111
|
|
|
|
112
|
|
|
return $this->dispatcher->pipeThrough($pipes)->dispatchFrom($job, $request, $parameters); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Resolve the apter request. |
117
|
|
|
* |
118
|
|
|
* @author Andrea Marco Sartori |
119
|
|
|
* @return Illuminate\Http\Request |
120
|
|
|
*/ |
121
|
|
|
protected function resolveRequest() |
122
|
|
|
{ |
123
|
|
|
if(class_exists($request = $this->inflector->getRequest())) |
124
|
|
|
{ |
125
|
|
|
return $this->container->make($request); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->container->make('Illuminate\Http\Request'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.