Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\Concerns;
4
 
5
use Closure;
6
use Illuminate\Console\OutputStyle;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
10
use Symfony\Component\Console\Helper\Table;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ChoiceQuestion;
14
use Symfony\Component\Console\Question\Question;
15
 
16
trait InteractsWithIO
17
{
18
    /**
19
     * The console components factory.
20
     *
21
     * @var \Illuminate\Console\View\Components\Factory
22
     *
23
     * @internal This property is not meant to be used or overwritten outside the framework.
24
     */
25
    protected $components;
26
 
27
    /**
28
     * The input interface implementation.
29
     *
30
     * @var \Symfony\Component\Console\Input\InputInterface
31
     */
32
    protected $input;
33
 
34
    /**
35
     * The output interface implementation.
36
     *
37
     * @var \Illuminate\Console\OutputStyle
38
     */
39
    protected $output;
40
 
41
    /**
42
     * The default verbosity of output commands.
43
     *
44
     * @var int
45
     */
46
    protected $verbosity = OutputInterface::VERBOSITY_NORMAL;
47
 
48
    /**
49
     * The mapping between human readable verbosity levels and Symfony's OutputInterface.
50
     *
51
     * @var array
52
     */
53
    protected $verbosityMap = [
54
        'v' => OutputInterface::VERBOSITY_VERBOSE,
55
        'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
56
        'vvv' => OutputInterface::VERBOSITY_DEBUG,
57
        'quiet' => OutputInterface::VERBOSITY_QUIET,
58
        'normal' => OutputInterface::VERBOSITY_NORMAL,
59
    ];
60
 
61
    /**
62
     * Determine if the given argument is present.
63
     *
64
     * @param  string|int  $name
65
     * @return bool
66
     */
67
    public function hasArgument($name)
68
    {
69
        return $this->input->hasArgument($name);
70
    }
71
 
72
    /**
73
     * Get the value of a command argument.
74
     *
75
     * @param  string|null  $key
76
     * @return array|string|bool|null
77
     */
78
    public function argument($key = null)
79
    {
80
        if (is_null($key)) {
81
            return $this->input->getArguments();
82
        }
83
 
84
        return $this->input->getArgument($key);
85
    }
86
 
87
    /**
88
     * Get all of the arguments passed to the command.
89
     *
90
     * @return array
91
     */
92
    public function arguments()
93
    {
94
        return $this->argument();
95
    }
96
 
97
    /**
98
     * Determine if the given option is present.
99
     *
100
     * @param  string  $name
101
     * @return bool
102
     */
103
    public function hasOption($name)
104
    {
105
        return $this->input->hasOption($name);
106
    }
107
 
108
    /**
109
     * Get the value of a command option.
110
     *
111
     * @param  string|null  $key
112
     * @return string|array|bool|null
113
     */
114
    public function option($key = null)
115
    {
116
        if (is_null($key)) {
117
            return $this->input->getOptions();
118
        }
119
 
120
        return $this->input->getOption($key);
121
    }
122
 
123
    /**
124
     * Get all of the options passed to the command.
125
     *
126
     * @return array
127
     */
128
    public function options()
129
    {
130
        return $this->option();
131
    }
132
 
133
    /**
134
     * Confirm a question with the user.
135
     *
136
     * @param  string  $question
137
     * @param  bool  $default
138
     * @return bool
139
     */
140
    public function confirm($question, $default = false)
141
    {
142
        return $this->output->confirm($question, $default);
143
    }
144
 
145
    /**
146
     * Prompt the user for input.
147
     *
148
     * @param  string  $question
149
     * @param  string|null  $default
150
     * @return mixed
151
     */
152
    public function ask($question, $default = null)
153
    {
154
        return $this->output->ask($question, $default);
155
    }
156
 
157
    /**
158
     * Prompt the user for input with auto completion.
159
     *
160
     * @param  string  $question
161
     * @param  array|callable  $choices
162
     * @param  string|null  $default
163
     * @return mixed
164
     */
165
    public function anticipate($question, $choices, $default = null)
166
    {
167
        return $this->askWithCompletion($question, $choices, $default);
168
    }
169
 
170
    /**
171
     * Prompt the user for input with auto completion.
172
     *
173
     * @param  string  $question
174
     * @param  array|callable  $choices
175
     * @param  string|null  $default
176
     * @return mixed
177
     */
178
    public function askWithCompletion($question, $choices, $default = null)
179
    {
180
        $question = new Question($question, $default);
181
 
182
        is_callable($choices)
183
            ? $question->setAutocompleterCallback($choices)
184
            : $question->setAutocompleterValues($choices);
185
 
186
        return $this->output->askQuestion($question);
187
    }
188
 
189
    /**
190
     * Prompt the user for input but hide the answer from the console.
191
     *
192
     * @param  string  $question
193
     * @param  bool  $fallback
194
     * @return mixed
195
     */
196
    public function secret($question, $fallback = true)
197
    {
198
        $question = new Question($question);
199
 
200
        $question->setHidden(true)->setHiddenFallback($fallback);
201
 
202
        return $this->output->askQuestion($question);
203
    }
204
 
205
    /**
206
     * Give the user a single choice from an array of answers.
207
     *
208
     * @param  string  $question
209
     * @param  array  $choices
210
     * @param  string|int|null  $default
211
     * @param  mixed|null  $attempts
212
     * @param  bool  $multiple
213
     * @return string|array
214
     */
215
    public function choice($question, array $choices, $default = null, $attempts = null, $multiple = false)
216
    {
217
        $question = new ChoiceQuestion($question, $choices, $default);
218
 
219
        $question->setMaxAttempts($attempts)->setMultiselect($multiple);
220
 
221
        return $this->output->askQuestion($question);
222
    }
223
 
224
    /**
225
     * Format input to textual table.
226
     *
227
     * @param  array  $headers
228
     * @param  \Illuminate\Contracts\Support\Arrayable|array  $rows
229
     * @param  \Symfony\Component\Console\Helper\TableStyle|string  $tableStyle
230
     * @param  array  $columnStyles
231
     * @return void
232
     */
233
    public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
234
    {
235
        $table = new Table($this->output);
236
 
237
        if ($rows instanceof Arrayable) {
238
            $rows = $rows->toArray();
239
        }
240
 
241
        $table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle);
242
 
243
        foreach ($columnStyles as $columnIndex => $columnStyle) {
244
            $table->setColumnStyle($columnIndex, $columnStyle);
245
        }
246
 
247
        $table->render();
248
    }
249
 
250
    /**
251
     * Execute a given callback while advancing a progress bar.
252
     *
253
     * @param  iterable|int  $totalSteps
254
     * @param  \Closure  $callback
255
     * @return mixed|void
256
     */
257
    public function withProgressBar($totalSteps, Closure $callback)
258
    {
259
        $bar = $this->output->createProgressBar(
260
            is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
261
        );
262
 
263
        $bar->start();
264
 
265
        if (is_iterable($totalSteps)) {
266
            foreach ($totalSteps as $value) {
267
                $callback($value, $bar);
268
 
269
                $bar->advance();
270
            }
271
        } else {
272
            $callback($bar);
273
        }
274
 
275
        $bar->finish();
276
 
277
        if (is_iterable($totalSteps)) {
278
            return $totalSteps;
279
        }
280
    }
281
 
282
    /**
283
     * Write a string as information output.
284
     *
285
     * @param  string  $string
286
     * @param  int|string|null  $verbosity
287
     * @return void
288
     */
289
    public function info($string, $verbosity = null)
290
    {
291
        $this->line($string, 'info', $verbosity);
292
    }
293
 
294
    /**
295
     * Write a string as standard output.
296
     *
297
     * @param  string  $string
298
     * @param  string|null  $style
299
     * @param  int|string|null  $verbosity
300
     * @return void
301
     */
302
    public function line($string, $style = null, $verbosity = null)
303
    {
304
        $styled = $style ? "<$style>$string</$style>" : $string;
305
 
306
        $this->output->writeln($styled, $this->parseVerbosity($verbosity));
307
    }
308
 
309
    /**
310
     * Write a string as comment output.
311
     *
312
     * @param  string  $string
313
     * @param  int|string|null  $verbosity
314
     * @return void
315
     */
316
    public function comment($string, $verbosity = null)
317
    {
318
        $this->line($string, 'comment', $verbosity);
319
    }
320
 
321
    /**
322
     * Write a string as question output.
323
     *
324
     * @param  string  $string
325
     * @param  int|string|null  $verbosity
326
     * @return void
327
     */
328
    public function question($string, $verbosity = null)
329
    {
330
        $this->line($string, 'question', $verbosity);
331
    }
332
 
333
    /**
334
     * Write a string as error output.
335
     *
336
     * @param  string  $string
337
     * @param  int|string|null  $verbosity
338
     * @return void
339
     */
340
    public function error($string, $verbosity = null)
341
    {
342
        $this->line($string, 'error', $verbosity);
343
    }
344
 
345
    /**
346
     * Write a string as warning output.
347
     *
348
     * @param  string  $string
349
     * @param  int|string|null  $verbosity
350
     * @return void
351
     */
352
    public function warn($string, $verbosity = null)
353
    {
354
        if (! $this->output->getFormatter()->hasStyle('warning')) {
355
            $style = new OutputFormatterStyle('yellow');
356
 
357
            $this->output->getFormatter()->setStyle('warning', $style);
358
        }
359
 
360
        $this->line($string, 'warning', $verbosity);
361
    }
362
 
363
    /**
364
     * Write a string in an alert box.
365
     *
366
     * @param  string  $string
367
     * @param  int|string|null  $verbosity
368
     * @return void
369
     */
370
    public function alert($string, $verbosity = null)
371
    {
372
        $length = Str::length(strip_tags($string)) + 12;
373
 
374
        $this->comment(str_repeat('*', $length), $verbosity);
375
        $this->comment('*     '.$string.'     *', $verbosity);
376
        $this->comment(str_repeat('*', $length), $verbosity);
377
 
378
        $this->comment('', $verbosity);
379
    }
380
 
381
    /**
382
     * Write a blank line.
383
     *
384
     * @param  int  $count
385
     * @return $this
386
     */
387
    public function newLine($count = 1)
388
    {
389
        $this->output->newLine($count);
390
 
391
        return $this;
392
    }
393
 
394
    /**
395
     * Set the input interface implementation.
396
     *
397
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
398
     * @return void
399
     */
400
    public function setInput(InputInterface $input)
401
    {
402
        $this->input = $input;
403
    }
404
 
405
    /**
406
     * Set the output interface implementation.
407
     *
408
     * @param  \Illuminate\Console\OutputStyle  $output
409
     * @return void
410
     */
411
    public function setOutput(OutputStyle $output)
412
    {
413
        $this->output = $output;
414
    }
415
 
416
    /**
417
     * Set the verbosity level.
418
     *
419
     * @param  string|int  $level
420
     * @return void
421
     */
422
    protected function setVerbosity($level)
423
    {
424
        $this->verbosity = $this->parseVerbosity($level);
425
    }
426
 
427
    /**
428
     * Get the verbosity level in terms of Symfony's OutputInterface level.
429
     *
430
     * @param  string|int|null  $level
431
     * @return int
432
     */
433
    protected function parseVerbosity($level = null)
434
    {
435
        if (isset($this->verbosityMap[$level])) {
436
            $level = $this->verbosityMap[$level];
437
        } elseif (! is_int($level)) {
438
            $level = $this->verbosity;
439
        }
440
 
441
        return $level;
442
    }
443
 
444
    /**
445
     * Get the output implementation.
446
     *
447
     * @return \Illuminate\Console\OutputStyle
448
     */
449
    public function getOutput()
450
    {
451
        return $this->output;
452
    }
453
}