1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TechDivision\Import\Cli\Plugins\AbstractConsolePlugin |
||
5 | * |
||
6 | * PHP version 7 |
||
7 | * |
||
8 | * @author Marcus Döllerer <[email protected]> |
||
9 | * @copyright 2020 TechDivision GmbH <[email protected]> |
||
10 | * @license https://opensource.org/licenses/MIT |
||
11 | * @link https://github.com/techdivision/import-cli |
||
12 | * @link http://www.techdivision.com |
||
13 | */ |
||
14 | |||
15 | namespace TechDivision\Import\Cli\Plugins; |
||
16 | |||
17 | use Symfony\Component\Console\Application; |
||
18 | use Symfony\Component\Console\Exception\LogicException; |
||
19 | use Symfony\Component\Console\Helper\HelperSet; |
||
20 | use Symfony\Component\Console\Input\InputInterface; |
||
21 | use Symfony\Component\Console\Output\OutputInterface; |
||
22 | use TechDivision\Import\ApplicationInterface; |
||
23 | use TechDivision\Import\Plugins\AbstractPlugin; |
||
24 | |||
25 | /** |
||
26 | * Abstract console plugin implementation containing access to console commands and helpers. |
||
27 | * |
||
28 | * @author Marcus Döllerer <[email protected]> |
||
29 | * @copyright 2020 TechDivision GmbH <[email protected]> |
||
30 | * @license https://opensource.org/licenses/MIT |
||
31 | * @link https://github.com/techdivision/import-cli |
||
32 | * @link http://www.techdivision.com |
||
33 | */ |
||
34 | abstract class AbstractConsolePlugin extends AbstractPlugin |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * The M2IF console application instance. |
||
39 | * |
||
40 | * @var \Symfony\Component\Console\Application |
||
41 | */ |
||
42 | protected $cliApplication; |
||
43 | |||
44 | /** |
||
45 | * The console input instance. |
||
46 | * |
||
47 | * @var InputInterface |
||
48 | */ |
||
49 | protected $input; |
||
50 | |||
51 | /** |
||
52 | * The console output instance. |
||
53 | * |
||
54 | * @var OutputInterface |
||
55 | */ |
||
56 | protected $output; |
||
57 | |||
58 | /** |
||
59 | * The helper set. |
||
60 | * |
||
61 | * @var HelperSet |
||
62 | */ |
||
63 | protected $helperSet; |
||
64 | |||
65 | /** |
||
66 | * The constructor to initialize the plugin with. |
||
67 | * |
||
68 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
69 | * |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | public function __construct(ApplicationInterface $application) |
||
73 | { |
||
74 | |||
75 | // inject the cli application |
||
76 | $cliApplication = $application->getContainer()->get('application'); |
||
77 | if (!$cliApplication instanceof Application) { |
||
78 | throw new \Exception('No console application configured, please check your configuration.'); |
||
79 | } |
||
80 | $this->setCliApplication($cliApplication); |
||
81 | |||
82 | // set the console input |
||
83 | $input = $application->getContainer()->get('input'); |
||
84 | if (!$input instanceof InputInterface) { |
||
85 | throw new \Exception('No console input configured, please check your configuration.'); |
||
86 | } |
||
87 | $this->setInput($input); |
||
88 | |||
89 | // set the console output |
||
90 | $output = $application->getContainer()->get('output'); |
||
91 | if (!$output instanceof OutputInterface) { |
||
92 | throw new \Exception('No console output configured, please check your configuration.'); |
||
93 | } |
||
94 | $this->setOutput($output); |
||
95 | |||
96 | // inject the helper set |
||
97 | $helperSet = $this->getCliApplication()->getHelperSet(); |
||
98 | if (!$helperSet instanceof HelperSet) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
99 | throw new LogicException('No HelperSet is defined.'); |
||
100 | } |
||
101 | $this->setHelperSet($helperSet); |
||
102 | |||
103 | parent::__construct($application); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return's the console application instance. |
||
108 | * |
||
109 | * @return \Symfony\Component\Console\Application The console application instance |
||
110 | */ |
||
111 | public function getCliApplication() |
||
112 | { |
||
113 | return $this->cliApplication; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Set's the console application instance. |
||
118 | * |
||
119 | * @param \Symfony\Component\Console\Application $cliApplication The console application instance |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function setCliApplication(Application $cliApplication) |
||
124 | { |
||
125 | $this->cliApplication = $cliApplication; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Return's the console input instance. |
||
130 | * |
||
131 | * @return \Symfony\Component\Console\Input\InputInterface The console input instance |
||
132 | */ |
||
133 | public function getInput() |
||
134 | { |
||
135 | return $this->input; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Set's the console input instance. |
||
140 | * |
||
141 | * @param \Symfony\Component\Console\Input\InputInterface $input The console input instance |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function setInput(InputInterface $input) |
||
146 | { |
||
147 | $this->input = $input; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Return's the console output instance. |
||
152 | * |
||
153 | * @return \Symfony\Component\Console\Output\OutputInterface The console output instance |
||
154 | */ |
||
155 | public function getOutput() |
||
156 | { |
||
157 | return $this->output; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Set's the console output instance. |
||
162 | * |
||
163 | * @param \Symfony\Component\Console\Output\OutputInterface $output The console output instance |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function setOutput(OutputInterface $output) |
||
168 | { |
||
169 | $this->output = $output; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return's the helper set instance. |
||
174 | * |
||
175 | * @return \Symfony\Component\Console\Helper\HelperSet The helper set instance |
||
176 | */ |
||
177 | public function getHelperSet() |
||
178 | { |
||
179 | return $this->helperSet; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Set's the helper set instance. |
||
184 | * |
||
185 | * @param \Symfony\Component\Console\Helper\HelperSet $helperSet The helper set instance |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | public function setHelperSet(HelperSet $helperSet) |
||
190 | { |
||
191 | $this->helperSet = $helperSet; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Retrieve a helper by name. |
||
196 | * |
||
197 | * @param string $name The name of the helper to retrieve |
||
198 | * |
||
199 | * @return \Symfony\Component\Console\Helper\HelperInterface The helper instance |
||
200 | */ |
||
201 | public function getHelper($name) |
||
202 | { |
||
203 | return $this->getHelperSet()->get($name); |
||
204 | } |
||
205 | } |
||
206 |