5
5
use App \InitializesCommands ;
6
6
use App \Services ;
7
7
use App \Shell \Environment ;
8
- use Illuminate \Support \Arr ;
8
+ use Illuminate \Support \Collection ;
9
9
use Illuminate \Support \Facades \App ;
10
- use Illuminate \Support \Str ;
11
10
use LaravelZero \Framework \Commands \Command ;
12
11
12
+ use function Laravel \Prompts \search ;
13
+
13
14
class EnableCommand extends Command
14
15
{
15
16
use InitializesCommands;
@@ -46,21 +47,17 @@ public function handle(Environment $environment, Services $services): void
46
47
return ;
47
48
}
48
49
49
- $ option = $ this ->selectService ();
50
+ $ service = $ this ->selectService ($ this -> availableServices () );
50
51
51
- if (! $ option ) {
52
- return ;
53
- }
54
-
55
- $ this ->enable ($ option , $ useDefaults , $ passthroughOptions );
52
+ $ this ->enable ($ service , $ useDefaults , $ passthroughOptions );
56
53
}
57
54
58
55
/**
59
56
* Since we're pulling the *full* list of server arguments, not just relying on
60
57
* $this->argument, we have to do our own manual overriding for testing scenarios,
61
58
* because pulling $_SERVER['argv'] won't give the right results in testing.
62
59
*/
63
- public function serverArguments (): array
60
+ private function serverArguments (): array
64
61
{
65
62
if (App::environment () === 'testing ' ) {
66
63
$ string = array_merge (['takeout ' , 'enable ' ], $ this ->argument ('serviceNames ' ));
@@ -104,7 +101,7 @@ public function extractPassthroughOptions(array $arguments): array
104
101
public function removeOptions (array $ arguments ): array
105
102
{
106
103
$ arguments = collect ($ arguments )
107
- ->reject (fn ($ argument ) => str_starts_with ($ argument , '-- ' ) && strlen ($ argument ) > 2 )
104
+ ->reject (fn ($ argument ) => str_starts_with ($ argument , '-- ' ) && strlen ($ argument ) > 2 )
108
105
->values ()
109
106
->toArray ();
110
107
@@ -119,91 +116,36 @@ public function removeOptions(array $arguments): array
119
116
return array_slice ($ arguments , $ start );
120
117
}
121
118
122
- private function selectService (): ? string
119
+ private function availableServices (): Collection
123
120
{
124
- if ( $ this ->environment -> isWindowsOs () ) {
125
- return $ this ->windowsMenu ();
126
- }
127
-
128
- return $ this -> defaultMenu ( );
121
+ return $ this ->enableableServicesByCategory ()-> flatMap ( function ( $ services , $ category ) {
122
+ return $ this ->menuItemsForServices ( $ services )-> mapWithKeys ( function ( $ row , $ key ) use ( $ category ) {
123
+ return [ $ key => "{ $ category } : { $ row }" ];
124
+ })-> toArray ();
125
+ } );
129
126
}
130
127
131
- private function defaultMenu ( ): ?string
128
+ private function selectService ( Collection $ servicesList ): ?string
132
129
{
133
- $ option = $ this ->menu (self ::MENU_TITLE )->setTitleSeparator ('= ' );
134
-
135
- foreach ($ this ->enableableServicesByCategory () as $ category => $ services ) {
136
- $ separator = str_repeat ('- ' , 1 + Str::length ($ category ));
137
-
138
- $ option ->addStaticItem ("{$ category }: " )
139
- ->addStaticItem ($ separator )
140
- ->addOptions ($ this ->menuItemsForServices ($ services ))
141
- ->addLineBreak ('' , 1 );
142
- }
143
-
144
- return $ option ->open ();
130
+ return search (
131
+ label: self ::MENU_TITLE ,
132
+ options: fn (string $ value ) => strlen ($ value ) > 0
133
+ ? $ servicesList ->filter (function ($ row ) use ($ value ) {
134
+ return str ($ row )->lower ()->contains (str ($ value )->lower ());
135
+ })->toArray ()
136
+ : $ servicesList ->toArray (),
137
+ scroll: 10
138
+ );
145
139
}
146
140
147
- private function windowsMenu ($ category = null ): ?string
148
- {
149
- $ choices = [];
150
- $ groupedServices = $ this ->enableableServicesByCategory ();
151
-
152
- if ($ category ) {
153
- $ groupedServices = Arr::where ($ groupedServices , function ($ value , $ key ) use ($ category ) {
154
- return Str::contains ($ category , strtoupper ($ key ));
155
- });
156
- }
157
-
158
- foreach ($ groupedServices as $ serviceCategory => $ services ) {
159
- $ serviceCategoryMenuItem = '<fg=white;bg=blue;options=bold> ' . (Str::upper ($ serviceCategory )) . ' </> ' ;
160
- array_push ($ choices , $ serviceCategoryMenuItem );
161
-
162
- foreach ($ this ->menuItemsForServices ($ services ) as $ menuItemKey => $ menuItemName ) {
163
- array_push ($ choices , $ menuItemName );
164
- }
165
- }
166
-
167
- if ($ category ) {
168
- array_push ($ choices , '<info>Back</> ' );
169
- }
170
-
171
- array_push ($ choices , '<info>Exit</> ' );
172
-
173
- $ choice = $ this ->choice (self ::MENU_TITLE , $ choices );
174
-
175
- if (Str::contains ($ choice , 'Back ' )) {
176
- return $ this ->windowsMenu ();
177
- }
178
-
179
- if (Str::contains ($ choice , 'Exit ' )) {
180
- return null ;
181
- }
182
-
183
- foreach ($ this ->enableableServices () as $ shortName => $ fqcn ) {
184
- if ($ choice === $ fqcn ) {
185
- return $ shortName ;
186
- }
187
- }
188
-
189
- return $ this ->windowsMenu ($ choice );
190
- }
191
-
192
- private function menuItemsForServices ($ services ): array
141
+ private function menuItemsForServices ($ services ): Collection
193
142
{
194
143
return collect ($ services )->mapWithKeys (function ($ service ) {
195
144
return [$ service ['shortName ' ] => $ service ['name ' ]];
196
- })-> toArray () ;
145
+ });
197
146
}
198
147
199
- public function enableableServices (): array
200
- {
201
- return collect ($ this ->services ->all ())->mapWithKeys (function ($ fqcn , $ shortName ) {
202
- return [$ shortName => $ fqcn ::name ()];
203
- })->toArray ();
204
- }
205
-
206
- public function enableableServicesByCategory (): array
148
+ private function enableableServicesByCategory (): Collection
207
149
{
208
150
return collect ($ this ->services ->all ())
209
151
->mapToGroups (function ($ fqcn , $ shortName ) {
@@ -214,11 +156,10 @@ public function enableableServicesByCategory(): array
214
156
],
215
157
];
216
158
})
217
- ->sortKeys ()
218
- ->toArray ();
159
+ ->sortKeys ();
219
160
}
220
161
221
- public function enable (
162
+ private function enable (
222
163
string $ service ,
223
164
bool $ useDefaults = false ,
224
165
array $ passthroughOptions = [],
0 commit comments