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\Server\Environment; |
11
|
|
|
use Deployer\Server\ServerInterface; |
12
|
|
|
use Deployer\Exception\Exception; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class Context |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ServerInterface|null |
20
|
|
|
*/ |
21
|
|
|
private $server; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Environment|null |
25
|
|
|
*/ |
26
|
|
|
private $env; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var InputInterface|null |
30
|
|
|
*/ |
31
|
|
|
private $input; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var OutputInterface|null |
35
|
|
|
*/ |
36
|
|
|
private $output; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Context[] |
40
|
|
|
*/ |
41
|
|
|
private static $contexts = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ServerInterface|null $server |
45
|
|
|
* @param Environment|null $env |
46
|
|
|
* @param InputInterface|null $input |
47
|
|
|
* @param OutputInterface|null $output |
48
|
|
|
*/ |
49
|
25 |
|
public function __construct($server, $env, $input, $output) |
50
|
|
|
{ |
51
|
25 |
|
$this->server = $server; |
52
|
25 |
|
$this->env = $env; |
53
|
25 |
|
$this->input = $input; |
54
|
25 |
|
$this->output = $output; |
55
|
25 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Context $context |
59
|
|
|
*/ |
60
|
28 |
|
public static function push(Context $context) |
61
|
|
|
{ |
62
|
28 |
|
self::$contexts[] = $context; |
63
|
28 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Context|false |
67
|
|
|
*/ |
68
|
24 |
|
public static function get() |
69
|
|
|
{ |
70
|
24 |
|
return end(self::$contexts); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the current context when available. |
75
|
|
|
* |
76
|
|
|
* Throws a Exception when not called within a task-context and therefore no Context is available. |
77
|
|
|
* |
78
|
|
|
* This method provides a usefull error to the end-user to make him/her aware |
79
|
|
|
* to use a function in the required task-context. |
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
|
|
public static function required($callerName) |
84
|
|
|
{ |
85
|
|
|
if (!self::get()) { |
86
|
|
|
throw new Exception("'$callerName' can only be used within a 'task()'-function!"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Context |
92
|
|
|
*/ |
93
|
28 |
|
public static function pop() |
94
|
|
|
{ |
95
|
28 |
|
return array_pop(self::$contexts); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Environment|null |
100
|
|
|
*/ |
101
|
20 |
|
public function getEnvironment() |
102
|
|
|
{ |
103
|
20 |
|
return $this->env; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return InputInterface|null |
108
|
|
|
*/ |
109
|
1 |
|
public function getInput() |
110
|
|
|
{ |
111
|
1 |
|
return $this->input; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return OutputInterface|null |
116
|
|
|
*/ |
117
|
17 |
|
public function getOutput() |
118
|
|
|
{ |
119
|
17 |
|
return $this->output; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return ServerInterface|null |
124
|
|
|
*/ |
125
|
13 |
|
public function getServer() |
126
|
|
|
{ |
127
|
13 |
|
return $this->server; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|