1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer\Task; |
||
9 | |||
10 | use Deployer\Selector\Selector; |
||
11 | |||
12 | class Task |
||
13 | { |
||
14 | private $name; |
||
15 | private $callback; |
||
16 | private $description; |
||
17 | |||
18 | /** |
||
19 | * Task source file location. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private $sourceLocation = ''; |
||
24 | |||
25 | /** |
||
26 | * Should we run this task locally? |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $local = false; |
||
31 | |||
32 | /** |
||
33 | * List of task names to run before. |
||
34 | * |
||
35 | * @var string[] |
||
36 | */ |
||
37 | private $before = []; |
||
38 | |||
39 | /** |
||
40 | * List of task names to run after. |
||
41 | * |
||
42 | * @var string[] |
||
43 | */ |
||
44 | private $after = []; |
||
45 | |||
46 | /** |
||
47 | * Make task internal and not visible in CLI. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $hidden = false; |
||
52 | |||
53 | /** |
||
54 | * Run task only once on one of hosts. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $once = false; |
||
59 | |||
60 | /** |
||
61 | * Shallow task will not print execution message/finish messages. |
||
62 | * Useful for success messages and info printing. |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | private $shallow = false; |
||
67 | |||
68 | /** |
||
69 | * Limit parallel execution of the task. |
||
70 | * |
||
71 | * @var int|null |
||
72 | */ |
||
73 | private $limit = null; |
||
74 | |||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | private $selector; |
||
79 | |||
80 | 19 | public function __construct($name, callable $callback = null) |
|
81 | { |
||
82 | 19 | $this->name = $name; |
|
83 | 19 | $this->callback = $callback; |
|
84 | 19 | $this->selector = Selector::parse('all'); |
|
85 | 19 | } |
|
86 | |||
87 | 10 | public function run(Context $context) |
|
88 | { |
||
89 | 10 | Context::push($context); |
|
90 | |||
91 | try { |
||
92 | 10 | call_user_func($this->callback); // call task |
|
93 | 10 | } finally { |
|
94 | 10 | if ($context->getConfig() !== null) { |
|
95 | 8 | $context->getConfig()->set('working_path', null); |
|
96 | } |
||
97 | |||
98 | 10 | Context::pop(); |
|
99 | } |
||
100 | 10 | } |
|
101 | |||
102 | 16 | public function getName() |
|
103 | { |
||
104 | 16 | return $this->name; |
|
105 | } |
||
106 | |||
107 | 5 | public function __toString() |
|
108 | { |
||
109 | 5 | return $this->getName(); |
|
110 | } |
||
111 | |||
112 | 13 | public function getDescription() |
|
113 | { |
||
114 | 13 | return $this->description; |
|
115 | } |
||
116 | |||
117 | 9 | public function desc(string $description) |
|
118 | { |
||
119 | 9 | $this->description = $description; |
|
120 | 9 | return $this; |
|
121 | } |
||
122 | |||
123 | 8 | public function getSourceLocation(): string |
|
124 | { |
||
125 | 8 | return $this->sourceLocation; |
|
126 | } |
||
127 | |||
128 | 15 | public function saveSourceLocation() |
|
129 | { |
||
130 | 15 | if (function_exists('debug_backtrace')) { |
|
131 | 15 | $trace = debug_backtrace(); |
|
132 | 15 | $this->sourceLocation = $trace[1]['file']; |
|
133 | } |
||
134 | 15 | } |
|
135 | |||
136 | /** |
||
137 | * Mark this task local. |
||
138 | */ |
||
139 | 1 | public function local() |
|
140 | { |
||
141 | 1 | $this->local = true; |
|
142 | 1 | return $this; |
|
143 | } |
||
144 | |||
145 | 13 | public function isLocal() |
|
146 | { |
||
147 | 13 | return $this->local; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Mark this task to run only once on one of hosts. |
||
152 | */ |
||
153 | 9 | public function once() |
|
154 | { |
||
155 | 9 | $this->once = true; |
|
156 | 9 | return $this; |
|
157 | } |
||
158 | |||
159 | 13 | public function isOnce() |
|
160 | { |
||
161 | 13 | return $this->once; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Mark task as hidden and not accessible from CLI. |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | 9 | public function hidden() |
|
170 | { |
||
171 | 9 | $this->hidden = true; |
|
172 | 9 | return $this; |
|
173 | } |
||
174 | |||
175 | 13 | public function isHidden() |
|
176 | { |
||
177 | 13 | return $this->hidden; |
|
178 | } |
||
179 | |||
180 | 2 | public function addBefore(string $task) |
|
181 | { |
||
182 | 2 | array_unshift($this->before, $task); |
|
183 | 2 | return $this; |
|
184 | } |
||
185 | |||
186 | 13 | public function addAfter(string $task) |
|
187 | { |
||
188 | 13 | array_push($this->after, $task); |
|
189 | 13 | return $this; |
|
190 | } |
||
191 | |||
192 | 15 | public function getBefore() |
|
193 | { |
||
194 | 15 | return $this->before; |
|
195 | } |
||
196 | |||
197 | 15 | public function getAfter() |
|
198 | { |
||
199 | 15 | return $this->after; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Sets task as shallow. Shallow task will not print execution message/finish messages. |
||
204 | */ |
||
205 | 8 | public function shallow() |
|
206 | { |
||
207 | 8 | $this->shallow = true; |
|
208 | 8 | return $this; |
|
209 | } |
||
210 | |||
211 | 12 | public function isShallow() |
|
212 | { |
||
213 | 12 | return $this->shallow; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return int|null |
||
218 | */ |
||
219 | 12 | public function getLimit(): ?int |
|
220 | { |
||
221 | 12 | return $this->limit; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param int|null $limit |
||
226 | * @return Task |
||
227 | */ |
||
228 | public function limit(?int $limit) |
||
229 | { |
||
230 | $this->limit = $limit; |
||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $selector |
||
236 | * @return Task |
||
237 | */ |
||
238 | public function select(string $selector) |
||
239 | { |
||
240 | $this->selector = Selector::parse($selector); |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | 13 | public function getSelector(): ?array |
|
248 | { |
||
249 | 13 | return $this->selector; |
|
250 | } |
||
251 | |||
252 | 5 | public function addSelector(?array $newSelector) |
|
253 | { |
||
254 | 5 | if ($newSelector !== null) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
255 | 5 | if ($this->selector === null) { |
|
256 | $this->selector = $newSelector; |
||
257 | } else { |
||
258 | 5 | $this->selector = array_merge($this->selector, $newSelector); |
|
259 | } |
||
260 | } |
||
261 | 5 | } |
|
262 | } |
||
263 |