1
|
|
|
<?php namespace Cerbero\Workflow\Console\Commands; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputOption; |
4
|
|
|
|
5
|
|
|
class CreateWorkflowCommand extends WorkflowGeneratorCommand { |
6
|
|
|
|
7
|
|
|
use AttachesPipesTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The console command name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name = 'workflow:create'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Create a new workflow'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Andrea Marco Sartori |
25
|
|
|
* @var string $type Type of class to generate. |
26
|
|
|
*/ |
27
|
|
|
protected $type = 'Pipe'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function fire() |
35
|
|
|
{ |
36
|
|
|
$this->inflector->of($name = $this->getWorkflowName()); |
37
|
|
|
|
38
|
|
|
if($this->pipelines->exists($name)) |
39
|
|
|
{ |
40
|
|
|
return $this->error("The workflow [$name] already exists."); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->generateAllNeededFiles(); |
44
|
|
|
|
45
|
|
|
$this->pipelines->store($name, $this->getNamespacedPipesByOption('attach')); |
46
|
|
|
|
47
|
|
|
$this->info('Workflow created successfully.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generate all the needed files. |
52
|
|
|
* |
53
|
|
|
* @author Andrea Marco Sartori |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
protected function generateAllNeededFiles() |
57
|
|
|
{ |
58
|
|
|
$this->settleRepositoryIfNotExists(); |
59
|
|
|
|
60
|
|
|
$this->generateJob(); |
61
|
|
|
|
62
|
|
|
$this->generateRequestIfGuarded(); |
63
|
|
|
|
64
|
|
|
$this->generatePipes(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Settle the pipeline repository if it does not exist. |
69
|
|
|
* |
70
|
|
|
* @author Andrea Marco Sartori |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function settleRepositoryIfNotExists() |
74
|
|
|
{ |
75
|
|
|
$source = $this->pipelines->getSource(); |
76
|
|
|
|
77
|
|
|
if( ! $this->files->exists($source)) |
78
|
|
|
{ |
79
|
|
|
$this->pipelines->settle(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create the job to handle. |
85
|
|
|
* |
86
|
|
|
* @author Andrea Marco Sartori |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function generateJob() |
90
|
|
|
{ |
91
|
|
|
$name = $this->inflector->getJob(); |
92
|
|
|
|
93
|
|
|
$this->call('make:job', compact('name')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create the request if unguard is not set. |
98
|
|
|
* |
99
|
|
|
* @author Andrea Marco Sartori |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function generateRequestIfGuarded() |
103
|
|
|
{ |
104
|
|
|
if( ! $this->option('unguard')) |
105
|
|
|
{ |
106
|
|
|
$name = $this->inflector->getRequest(); |
107
|
|
|
|
108
|
|
|
$this->call('make:request', compact('name')); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the console command options. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function getOptions() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
['attach', '-a', InputOption::VALUE_OPTIONAL, 'The pipes to attach to the workflow.', null], |
121
|
|
|
['unguard', '-u', InputOption::VALUE_NONE, 'Do not make this workflow validate data.', null], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|