-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConsole.php
More file actions
123 lines (101 loc) · 2.86 KB
/
Console.php
File metadata and controls
123 lines (101 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\CLI;
use CodeIgniter\CodeIgniter;
use Config\App;
use Config\Services;
/**
* @see \CodeIgniter\CLI\ConsoleTest
*/
class Console
{
/**
* @internal
*/
public const DEFAULT_COMMAND = 'list';
private string $command = '';
/**
* @var array<string, string|null>
*/
private array $options = [];
/**
* Runs the current command discovered on the CLI.
*
* @param list<string> $tokens
*
* @return int|null Exit code or null for legacy commands that don't return an exit code.
*/
public function run(array $tokens = [])
{
if ($tokens === []) {
$tokens = service('superglobals')->server('argv', []);
}
$parser = new CommandLineParser($tokens);
$arguments = $parser->getArguments();
$this->options = $parser->getOptions();
$this->showHeader($this->hasParameterOption(['no-header']));
unset($this->options['no-header']);
if ($this->hasParameterOption(['help'])) {
unset($this->options['help']);
if ($arguments === []) {
$arguments = ['help', self::DEFAULT_COMMAND];
} elseif ($arguments[0] !== 'help') {
array_unshift($arguments, 'help');
}
}
$this->command = array_shift($arguments) ?? self::DEFAULT_COMMAND;
return service('commands')->run($this->command, array_merge($arguments, $this->options));
}
public function initialize(): static
{
Services::createRequest(config(App::class), true);
service('routes')->loadRoutes();
return $this;
}
/**
* Returns the command that is being executed.
*/
public function getCommand(): string
{
return $this->command;
}
/**
* Displays basic information about the Console.
*
* @return void
*/
public function showHeader(bool $suppress = false)
{
if ($suppress) {
return;
}
CLI::write(sprintf(
'CodeIgniter v%s Command Line Tool - Server Time: %s',
CodeIgniter::CI_VERSION,
date('Y-m-d H:i:s \\U\\T\\CP'),
), 'green');
CLI::newLine();
}
/**
* Checks whether any of the options are present in the command line.
*
* @param list<string> $options
*/
private function hasParameterOption(array $options): bool
{
foreach ($options as $option) {
if (array_key_exists($option, $this->options)) {
return true;
}
}
return false;
}
}