1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Task; |
12
|
|
|
|
13
|
|
|
use Deployer\Configuration; |
14
|
|
|
use Deployer\Exception\Exception; |
15
|
|
|
use Deployer\Host\Host; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class Context |
20
|
|
|
{ |
21
|
|
|
private Host $host; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Context[] |
25
|
|
|
*/ |
26
|
|
|
private static array $contexts = []; |
27
|
|
|
|
28
|
|
|
public function __construct(Host $host) |
29
|
|
|
{ |
30
|
|
|
$this->host = $host; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function push(Context $context): void |
34
|
|
|
{ |
35
|
|
|
self::$contexts[] = $context; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function has(): bool |
39
|
|
|
{ |
40
|
|
|
return !empty(self::$contexts); |
41
|
|
|
} |
42
|
|
|
|
43
|
17 |
|
public static function get(): Context |
44
|
|
|
{ |
45
|
17 |
|
if (empty(self::$contexts)) { |
46
|
17 |
|
throw new Exception("Context was requested but was not available."); |
47
|
17 |
|
} |
48
|
17 |
|
return end(self::$contexts); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function pop(): ?Context |
52
|
|
|
{ |
53
|
19 |
|
return array_pop(self::$contexts); |
54
|
|
|
} |
55
|
19 |
|
|
56
|
19 |
|
/** |
57
|
|
|
* Throws a Exception when not called within a task-context and therefore no Context is available. |
58
|
|
|
* |
59
|
|
|
* This method provides a useful error to the end-user to make him/her aware |
60
|
|
|
* to use a function in the required task-context. |
61
|
14 |
|
* |
62
|
|
|
* @throws Exception |
63
|
14 |
|
*/ |
64
|
|
|
public static function required(string $callerName): void |
65
|
|
|
{ |
66
|
|
|
if (empty(self::$contexts)) { |
67
|
|
|
throw new Exception("'$callerName' can only be used within a task."); |
68
|
|
|
} |
69
|
|
|
} |
70
|
12 |
|
|
71
|
|
|
public function getConfig(): Configuration |
72
|
12 |
|
{ |
73
|
|
|
return $this->host->config(); |
74
|
|
|
} |
75
|
12 |
|
|
76
|
|
|
public function getHost(): Host |
77
|
|
|
{ |
78
|
|
|
return $this->host; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|