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