22
33namespace EE ;
44
5+ use EE ;
6+ use EE \Dispatcher \CommandFactory ;
7+ use EE \Model \Site ;
8+
59class Completions {
610
711 private $ words ;
812 private $ opts = array ();
913
10- public function __construct ( $ line ) {
14+ public function __construct ( $ line, $ shell ) {
1115 // TODO: properly parse single and double quotes
1216 $ this ->words = explode ( ' ' , $ line );
1317
@@ -21,7 +25,7 @@ public function __construct( $line ) {
2125 }
2226
2327 $ is_alias = false ;
24- $ is_help = false ;
28+ $ is_help = false ;
2529 if ( ! empty ( $ this ->words [0 ] ) && preg_match ( '/^@/ ' , $ this ->words [0 ] ) ) {
2630 array_shift ( $ this ->words );
2731 // `ee @al` is false, but `ee @all ` is true.
@@ -48,21 +52,24 @@ public function __construct( $line ) {
4852 return ;
4953 }
5054 }
51-
5255 if ( $ command ->can_have_subcommands () ) {
5356 // add completion when command is `ee` and alias isn't set.
54- if ( 'ee ' === $ command ->get_name () && false === $ is_alias && false == $ is_help ) {
57+ if ( 'ee ' === $ command ->get_name () && false === $ is_alias && false === $ is_help ) {
5558 $ aliases = \EE ::get_configurator ()->get_aliases ();
5659 foreach ( $ aliases as $ name => $ _ ) {
5760 $ this ->add ( "$ name " );
5861 }
5962 }
60- foreach ( $ command ->get_subcommands () as $ name => $ _ ) {
61- $ this ->add ( "$ name " );
63+ foreach ( $ command ->get_subcommands () as $ name => $ subcommand ) {
64+ if ( $ shell === 'zsh ' ) {
65+ $ this ->add ( $ name . ': ' . $ subcommand ->get_shortdesc () );
66+ } else {
67+ $ this ->add ( $ name );
68+ }
6269 }
6370 } else {
6471 foreach ( $ spec as $ arg ) {
65- if ( in_array ( $ arg ['type ' ], array ( 'flag ' , 'assoc ' ) ) ) {
72+ if ( in_array ( $ arg ['type ' ], array ( 'flag ' , 'assoc ' ), true ) ) {
6673 if ( isset ( $ assoc_args [ $ arg ['name ' ] ] ) ) {
6774 continue ;
6875 }
@@ -78,24 +85,7 @@ public function __construct( $line ) {
7885 $ this ->add ( $ opt );
7986 }
8087 }
81-
82- foreach ( $ this ->get_global_parameters () as $ param => $ runtime ) {
83- if ( isset ( $ assoc_args [ $ param ] ) ) {
84- continue ;
85- }
86-
87- $ opt = "-- {$ param }" ;
88-
89- if ( '' === $ runtime || ! is_string ( $ runtime ) ) {
90- $ opt .= ' ' ;
91- } else {
92- $ opt .= '= ' ;
93- }
94-
95- $ this ->add ( $ opt );
96- }
9788 }
98-
9989 }
10090
10191 private function get_command ( $ words ) {
@@ -109,8 +99,10 @@ private function get_command( $words ) {
10999 }
110100 }
111101
102+ $ this ->maybe_add_site_command ( $ words );
103+
112104 $ r = \EE ::get_runner ()->find_command_to_run ( $ positional_args );
113- if ( ! is_array ( $ r ) && array_pop ( $ positional_args ) == $ this ->cur_word ) {
105+ if ( ! is_array ( $ r ) && array_pop ( $ positional_args ) === $ this ->cur_word ) {
114106 $ r = \EE ::get_runner ()->find_command_to_run ( $ positional_args );
115107 }
116108
@@ -123,29 +115,44 @@ private function get_command( $words ) {
123115 return array ( $ command , $ args , $ assoc_args );
124116 }
125117
126- private function get_global_parameters () {
127- $ params = array ();
128- foreach ( \EE ::get_configurator ()->get_spec () as $ key => $ details ) {
129- if ( false === $ details ['runtime ' ] ) {
130- continue ;
131- }
118+ /**
119+ * Adds correct site-type to EE runner if autocompletion of site command is required
120+ */
121+ private function maybe_add_site_command ( array $ words ) {
122+ if ( count ( $ words ) > 0 && 'site ' === $ words [0 ] ) {
123+ $ type = $ this ->get_site_type ( $ words , 'html ' );
124+ $ site_types = \Site_Command::get_site_types ();
125+
126+ $ command = EE ::get_root_command ();
127+ $ callback = $ site_types [ $ type ];
128+ $ leaf_command = CommandFactory::create ( 'site ' , $ callback , $ command );
129+ $ command ->add_subcommand ( 'site ' , $ leaf_command );
130+ }
131+ }
132132
133- if ( isset ( $ details ['deprecated ' ] ) ) {
134- continue ;
135- }
133+ /**
134+ * Returns correct site-type for completion. Only for `site create`, type is specified in command.
135+ * For other commands, it is fetched from EE db.comp
136+ */
137+ private function get_site_type ( $ words , $ default_site_type ) {
138+ $ type = $ default_site_type ;
136139
137- if ( isset ( $ details ['hidden ' ] ) ) {
138- continue ;
140+ foreach ( $ words as $ arg ) {
141+ if ( preg_match ( '|^--type=(\S+)| ' , $ arg , $ matches ) ) {
142+ $ type = $ matches [1 ];
139143 }
140- $ params [ $ key ] = $ details ['runtime ' ];
144+ }
145+
146+ if ( count ( $ words ) >= 3 && 'create ' === $ words [1 ] && ! preg_match ( '|^--| ' , $ words [2 ] ) ) {
147+ $ sitename = str_replace ( array ( 'https:// ' , 'http:// ' ), '' , $ words [2 ] );
148+ $ sitetype = Site::find ( $ sitename , array ( 'site_type ' ) );
141149
142- // Add additional option like `--[no-]color`.
143- if ( true === $ details ['runtime ' ] ) {
144- $ params [ 'no- ' . $ key ] = '' ;
150+ if ( $ sitetype ) {
151+ $ type = $ sitetype ->site_type ;
145152 }
146153 }
147154
148- return $ params ;
155+ return $ type ;
149156 }
150157
151158 private function add ( $ opt ) {
0 commit comments