3
3
use InvalidArgumentException ;
4
4
use System \Classes \PluginBase ;
5
5
use System \Classes \PluginManager ;
6
+ use Throwable ;
7
+ use Winter \Storm \Support \Arr ;
8
+
9
+ use function Laravel \Prompts \search ;
10
+ use function Laravel \Prompts \select ;
6
11
7
12
/**
8
13
* Console Command Trait that provides autocompletion for the "plugin" argument
@@ -22,6 +27,8 @@ trait HasPluginArgument
22
27
*/
23
28
// protected $validatePluginInput = true;
24
29
30
+ protected ?PluginBase $ plugin = null ;
31
+
25
32
/**
26
33
* Return available plugins for autocompletion of the "plugin" argument
27
34
*/
@@ -45,7 +52,33 @@ public function suggestPluginValues()
45
52
}
46
53
}
47
54
48
- return $ plugins ;
55
+ return Arr::sort ($ plugins );
56
+ }
57
+
58
+ /**
59
+ * Prompt for which provider or tag to publish.
60
+ */
61
+ protected function promptForPlugin (): ?string
62
+ {
63
+ $ choices = $ this ->suggestPluginValues ();
64
+
65
+ $ choice = windows_os ()
66
+ ? select (
67
+ "Which plugin would you like to select? " ,
68
+ $ choices ,
69
+ scroll: 15 ,
70
+ )
71
+ : search (
72
+ label: "Which plugin would you like to select? " ,
73
+ placeholder: 'Search... ' ,
74
+ options: fn ($ search ) => array_values (array_filter (
75
+ $ choices ,
76
+ fn ($ choice ) => str_contains (strtolower ($ choice ), strtolower ($ search ))
77
+ )),
78
+ scroll: 15 ,
79
+ );
80
+
81
+ return $ choice ;
49
82
}
50
83
51
84
/**
@@ -55,7 +88,7 @@ public function suggestPluginValues()
55
88
public function getPluginIdentifier ($ identifier = null ): string
56
89
{
57
90
$ pluginManager = PluginManager::instance ();
58
- $ pluginName = $ identifier ?? $ this ->argument ('plugin ' );
91
+ $ pluginName = $ identifier ?? $ this ->argument ('plugin ' ) ?? $ this -> promptForPlugin () ;
59
92
$ pluginName = $ pluginManager ->normalizeIdentifier ($ pluginName );
60
93
61
94
if (
@@ -76,4 +109,24 @@ public function getPlugin($identifier = null): ?PluginBase
76
109
{
77
110
return PluginManager::instance ()->findByIdentifier ($ this ->getPluginIdentifier ($ identifier ));
78
111
}
112
+
113
+ /**
114
+ * Validates that the provided plugin can be accessed
115
+ *
116
+ * @throws InvalidArgumentException if it cannot be
117
+ */
118
+ public function validateProvidedPlugin ()
119
+ {
120
+ if (!$ this ->plugin ) {
121
+ try {
122
+ $ this ->plugin = $ this ->getPlugin ();
123
+ } catch (Throwable $ e ) {
124
+ throw new InvalidArgumentException (sprintf (
125
+ 'Provided plugin "%s" failed to load: %s ' ,
126
+ $ this ->argument ('plugin ' ),
127
+ $ e ->getMessage ()
128
+ ));
129
+ }
130
+ }
131
+ }
79
132
}
0 commit comments