1
|
|
|
<?php |
2
|
|
|
namespace Robo\Symfony; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* IOStorage insures that a single common style object is |
10
|
|
|
* used by all IOAware classes. |
11
|
|
|
*/ |
12
|
|
|
class IOStorage |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Symfony\Component\Console\Style\SymfonyStyle |
16
|
|
|
*/ |
17
|
|
|
protected $io; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
21
|
|
|
*/ |
22
|
|
|
protected $input; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
26
|
|
|
*/ |
27
|
|
|
protected $output; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $styleClass = '\Symfony\Component\Console\Style\SymfonyStyle'; |
33
|
|
|
|
34
|
|
|
public function clear() |
35
|
|
|
{ |
36
|
|
|
$this->input = null; |
37
|
|
|
$this->output = null; |
38
|
|
|
$this->io = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* setStyleClass sets a new style class to use. |
43
|
|
|
* |
44
|
|
|
* @param string $styleClass Name of class to instantiate when style object |
45
|
|
|
* is requested. Must be a subclass of SymfonyStyle. |
46
|
|
|
*/ |
47
|
|
|
public function setStyleClass($styleClass) |
48
|
|
|
{ |
49
|
|
|
$this->styleClass = $styleClass; |
50
|
|
|
$this->recreate(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* hasStyle indicates whether there is a cached style available |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function hasStyle() |
59
|
|
|
{ |
60
|
|
|
return !empty($this->io); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* create will instantiate a new style instance, replacing what was |
65
|
|
|
* there before. |
66
|
|
|
* |
67
|
|
|
* @param InputInterface $input |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @return SymfonyStyle |
70
|
|
|
*/ |
71
|
|
|
public function create(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
|
|
$this->input = $input; |
74
|
|
|
$this->output = $output; |
75
|
|
|
return $this->instantiate(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* recreate will make a new style object iff we have cached $input |
80
|
|
|
* and $output objects. Otherwise it clears the cached style object. |
81
|
|
|
*/ |
82
|
|
|
protected function recreate() |
83
|
|
|
{ |
84
|
|
|
if (!empty($this->input) && !empty($this->output)) { |
85
|
|
|
return $this->instantiate(); |
86
|
|
|
} |
87
|
|
|
$this->io = null; |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* instantiate will make a new style object from the cached input and |
93
|
|
|
* output objects. |
94
|
|
|
*/ |
95
|
|
|
protected function instantiate() |
96
|
|
|
{ |
97
|
|
|
$this->io = new $this->styleClass($this->input, $this->output); |
98
|
|
|
return $this->cached(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* get will return the cached style object, if it exists; otherwise, |
103
|
|
|
* it will create and cache a new style object using the provided |
104
|
|
|
* input and output objects. |
105
|
|
|
* |
106
|
|
|
* @param InputInterface $input |
107
|
|
|
* @param OutputInterface $output |
108
|
|
|
* @return SymfonyStyle |
109
|
|
|
*/ |
110
|
|
|
public function get(InputInterface $input, OutputInterface $output) |
111
|
|
|
{ |
112
|
|
|
if (!$this->hasStyle()) { |
113
|
|
|
$this->create($input, $output); |
114
|
|
|
} |
115
|
|
|
return $this->cached(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* cached returns the cached style object, or null if none is available. |
120
|
|
|
* |
121
|
|
|
* @return SymfonyStyle|null |
122
|
|
|
*/ |
123
|
|
|
public function cached() |
124
|
|
|
{ |
125
|
|
|
return $this->io; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|