-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
156 lines (126 loc) · 4.35 KB
/
Copy pathParser.php
File metadata and controls
156 lines (126 loc) · 4.35 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Parser class file
*
* phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows
*
* @package Mantle
*/
declare(strict_types=1);
namespace Mantle\Console;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
/**
* Command Signature Parser
*/
class Parser {
/**
* Parse the given console command definition into an array.
*
* @param string $expression
* @return array<mixed>
*
* @throws \InvalidArgumentException
*/
public static function parse( $expression ): array {
$name = static::name( $expression );
if ( preg_match_all( '/\{\s*(.*?)\s*\}/', $expression, $matches ) ) {
return array_merge( [ $name ], static::parameters( $matches[1] ) ); // @phpstan-ignore-line argument.type
}
return [ $name, [], [] ];
}
/**
* Extract the name of the command from the expression.
*
* @param string $expression
*
* @throws \InvalidArgumentException
*/
protected static function name( $expression ): string {
if ( ! preg_match( '/[^\s]+/', $expression, $matches ) ) {
throw new InvalidArgumentException( 'Unable to determine command name from signature.' );
}
return $matches[0];
}
/**
* Extract all of the parameters from the tokens.
*
* @param array<string, string> $tokens
* @return array<mixed>
*/
protected static function parameters( array $tokens ): array {
$arguments = [];
$options = [];
foreach ( $tokens as $token ) {
if ( preg_match( '/-{2,}(.*)/', (string) $token, $matches ) ) {
$options[] = static::parse_option( $matches[1] );
} else {
$arguments[] = static::parse_argument( $token );
}
}
return [ $arguments, $options ];
}
/**
* Parse an argument expression.
*
* @param string $token
*/
protected static function parse_argument( $token ): \Symfony\Component\Console\Input\InputArgument {
[ $token, $description ] = static::extract_description( $token );
if ( str_ends_with( $token, '?*' ) ) {
return new InputArgument( trim( $token, '?*' ), InputArgument::IS_ARRAY, $description );
}
if ( str_ends_with( $token, '*' ) ) {
return new InputArgument( trim( $token, '*' ), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description );
}
if ( str_ends_with( $token, '?' ) ) {
return new InputArgument( trim( $token, '?' ), InputArgument::OPTIONAL, $description );
}
if ( preg_match( '/(.+)\=\*(.+)/', $token, $matches ) ) {
return new InputArgument( $matches[1], InputArgument::IS_ARRAY, $description, preg_split( '/,\s?/', $matches[2] ) );
}
if ( preg_match( '/(.+)\=(.+)/', $token, $matches ) ) {
return new InputArgument( $matches[1], InputArgument::OPTIONAL, $description, $matches[2] );
}
return new InputArgument( $token, InputArgument::REQUIRED, $description );
}
/**
* Parse an option expression.
*
* @param string $token
*/
protected static function parse_option( $token ): \Symfony\Component\Console\Input\InputOption {
[$token, $description] = static::extract_description( $token );
$matches = preg_split( '/\s*\|\s*/', $token, 2 );
if ( isset( $matches[1] ) ) {
$shortcut = $matches[0];
$token = $matches[1];
} else {
$shortcut = null;
}
if ( str_ends_with( (string) $token, '=' ) ) {
return new InputOption( trim( (string) $token, '=' ), $shortcut, InputOption::VALUE_OPTIONAL, $description );
}
if ( str_ends_with( (string) $token, '=*' ) ) {
return new InputOption( trim( (string) $token, '=*' ), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description );
}
if ( preg_match( '/(.+)\=\*(.+)/', (string) $token, $matches ) ) {
return new InputOption( $matches[1], $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description, preg_split( '/,\s?/', $matches[2] ) );
}
if ( preg_match( '/(.+)\=(.+)/', (string) $token, $matches ) ) {
return new InputOption( $matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2] );
}
return new InputOption( $token, $shortcut, InputOption::VALUE_NONE, $description );
}
/**
* Parse the token into its token and description segments.
*
* @param string $token
* @return array<string>
*/
protected static function extract_description( $token ): array {
$parts = preg_split( '/\s+:\s+/', trim( $token ), 2 );
return $parts && count( $parts ) === 2 ? $parts : [ $token, '' ];
}
}