-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCommander.php
More file actions
172 lines (146 loc) · 3.29 KB
/
Commander.php
File metadata and controls
172 lines (146 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Statamic\Addons\Spock;
use Statamic\Contracts\Data\Users\User;
use Illuminate\Foundation\Bus\DispatchesJobs;
class Commander
{
use DispatchesJobs;
protected $user;
protected $event;
protected $config = [];
protected $environment;
protected $commands = [];
/**
* Handle execution of the commands.
*
* @return void
*/
public function handle()
{
if (! $this->shouldRunCommands()) {
return;
}
$this->dispatch(new RunProcesses($this->commands()));
}
/**
* Set the environment.
*
* @param string $environment
* @return self
*/
public function environment($environment)
{
$this->environment = $environment;
return $this;
}
/**
* Get or set the event.
*
* @param string $event
* @return self
*/
public function event($event = null)
{
if (! $event) {
return $this->event;
}
$this->event = $event;
return $this;
}
/**
* Set the user that triggered the event.
*
* @param User $user
* @return self
*/
public function user($user)
{
$this->user = $user;
return $this;
}
/**
* Set the Spock config.
*
* @param array $config
* @return self
*/
public function config($config)
{
$this->config = $config;
return $this;
}
/**
* Whether the commands should be run.
*
* @return bool
*/
public function shouldRunCommands()
{
return $this->isEnvironmentAllowed() && $this->isEventAllowed() && $this->isEventAffectingFiles();
}
/**
* Is environment allowed?
*
* @return bool
*/
protected function isEnvironmentAllowed()
{
return in_array($this->environment, array_get($this->config, 'environments', []));
}
/**
* Is event allowed?
*
* @return bool
*/
protected function isEventAllowed()
{
return !in_array(get_class($this->event), array_get($this->config, 'ignore_events', []));
}
/**
* Is event changing files?
*
* @return bool
*/
protected function isEventAffectingFiles()
{
return !empty($this->event->affectedPaths());
}
/**
* Set the commands to be run.
*
* @param array $commands
* @return self
*/
public function setCommands($commands)
{
$this->commands = $commands;
return $this;
}
/**
* Get the commands to be processed.
*
* @return Process[]
*/
public function commands()
{
$commands = $this->commands ?: $this->defaultCommands();
if ($commands instanceof \Closure) {
$commands = $commands($this);
}
if (is_string($commands)) {
$commands = [$commands];
}
return array_map(function ($command) {
return ($command instanceof Process) ? $command : new Process($command);
}, $commands);
}
/**
* Get the commands to be run if none have been specified.
*
* @return array
*/
protected function defaultCommands()
{
return (new Git($this->config, $this->event, $this->user))->commands();
}
}