@@ -173,32 +173,82 @@ You can provide customised implementations these methods by creating the followi
173
173
* ` mockWebSetBody `
174
174
* ` mockWebSetHeader `
175
175
176
- ## Colors for CLI Applications
176
+ ## Command Line Applications
177
+
178
+ The Joomla Framework provides an application class for making command line applications.
179
+
180
+ An example command line application skeleton:
181
+
182
+ ``` php
183
+ <?php
184
+
185
+ use Joomla\Application\AbstractCliApplication;
186
+
187
+ // Bootstrap the autoloader (adjust path as appropriate to your situation).
188
+ require_once __DIR__ . '/../vendor/autoload.php';
189
+
190
+ class MyCli extends AbstractCliApplication
191
+ {
192
+ protected function doExecute()
193
+ {
194
+ $this->out('It works');
195
+ }
196
+ }
197
+
198
+ $app = new MyCli;
199
+ $app->execute();
200
+
201
+ ```
202
+
203
+ ### Colors for CLI Applications
177
204
178
205
It is possible to use colors on an ANSI enabled terminal.
179
206
180
207
``` php
181
- // Green text
182
- $this->out('<info >foo</info >');
208
+ class MyCli extends AbstractCliApplication
209
+ {
210
+ protected function doExecute()
211
+ {
212
+ // Green text
213
+ $this->out('<info >foo</info >');
183
214
184
- // Yellow text
185
- $this->out('<comment >foo</comment >');
215
+ // Yellow text
216
+ $this->out('<comment >foo</comment >');
186
217
187
- // Black text on a cyan background
188
- $this->out('<question >foo</question >');
218
+ // Black text on a cyan background
219
+ $this->out('<question >foo</question >');
189
220
190
- // White text on a red background
191
- $this->out('<error >foo</error >');
221
+ // White text on a red background
222
+ $this->out('<error >foo</error >');
223
+ }
224
+ }
192
225
```
193
226
194
227
You can also create your own styles.
195
228
196
229
``` php
197
230
use Joomla\Application\Cli\Colorstyle;
198
231
199
- $style = new Colorstyle('yellow', 'red', array('bold', 'blink'));
200
- $this->getOutput()->addStyle('fire', $style);
201
- $this->out('<fire >foo</fire >');
232
+ class MyCli extends AbstractCliApplication
233
+ {
234
+ /**
235
+ * Override to initialise the colour styles.
236
+ *
237
+ * @return void
238
+ *
239
+ * @since 1.0
240
+ */
241
+ protected function initialise()
242
+ {
243
+ $style = new Colorstyle('yellow', 'red', array('bold', 'blink'));
244
+ $this->getOutput()->addStyle('fire', $style);
245
+ }
246
+
247
+ protected function doExecute()
248
+ {
249
+ $this->out('<fire >foo</fire >');
250
+ }
251
+ }
202
252
203
253
```
204
254
@@ -209,14 +259,20 @@ And available options are: bold, underscore, blink and reverse.
209
259
You can also set these colors and options inside the tagname:
210
260
211
261
``` php
212
- // Green text
213
- $this->out('<fg =green >foo</fg =green >');
262
+ class MyCli extends AbstractCliApplication
263
+ {
264
+ protected function doExecute()
265
+ {
266
+ // Green text
267
+ $this->out('<fg =green >foo</fg =green >');
214
268
215
- // Black text on a cyan background
216
- $this->out('<fg =black;bg =cyan >foo</fg =black;bg =cyan >');
269
+ // Black text on a cyan background
270
+ $this->out('<fg =black;bg =cyan >foo</fg =black;bg =cyan >');
217
271
218
- // Bold text on a yellow background
219
- $this->out('<bg =yellow;options =bold >foo</bg =yellow;options =bold >');
272
+ // Bold text on a yellow background
273
+ $this->out('<bg =yellow;options =bold >foo</bg =yellow;options =bold >');
274
+ }
275
+ }
220
276
```
221
277
222
278
## Installation via Composer
0 commit comments