@@ -10,6 +10,10 @@ abstract class Terminus_Command {
1010 public $ cache ;
1111 public $ session ;
1212 public $ sites ;
13+
14+ protected $ _func ;
15+ protected $ _siteInfo ;
16+ protected $ _bindings ;
1317
1418 public function __construct () {
1519 # Load commonly used data from cache.
@@ -156,6 +160,7 @@ public function terminus_request($realm, $uuid, $path = FALSE, $method = 'GET',
156160
157161 $ info = curl_getinfo ($ ch );
158162 if ($ info ['http_code ' ] > 399 ) {
163+ $ this ->_debug (get_defined_vars ());
159164 \Terminus::error ('Request failed ' );
160165 // Expired session. Really don't like the string comparison.
161166 if ($ info ['http_code ' ] == 403 && $ json == '"Session not found." ' ) {
@@ -166,10 +171,131 @@ public function terminus_request($realm, $uuid, $path = FALSE, $method = 'GET',
166171 }
167172
168173 return array (
174+ 'info ' => $ info ,
169175 'headers ' => $ headers_text ,
170176 'json ' => $ json ,
171177 'data ' => json_decode ($ json )
172178 );
173179 }
180+
181+ protected function _validateSiteUuid ($ site ) {
182+ if (\Terminus \Utils \is_valid_uuid ($ site ) && property_exists ($ this ->sites , $ site )){
183+ $ this ->_siteInfo =& $ this ->sites [$ site ];
184+ $ this ->_siteInfo ->site_uuid = $ site ;
185+ } elseif ($ this ->_siteInfo = $ this ->fetch_site ($ site )) {
186+ $ site = $ this ->_siteInfo ->site_uuid ;
187+ } else {
188+ Terminus::error ("Unable to locate the requested site. " );
189+ }
190+ return $ site ;
191+ }
192+
193+ protected function _constructTableForResponse ($ data ) {
194+ $ table = new \cli \Table ();
195+ if (is_object ($ data )) {
196+ $ data = (array )$ data ;
197+ }
198+ if (property_exists ($ this , "_headers " ) && array_key_exists ($ this ->_func , $ this ->_headers )) {
199+ $ table ->setHeaders ($ this ->_headers [$ this ->_func ]);
200+ } else {
201+ $ table ->setHeaders (array_keys ($ data ));
202+ }
203+ foreach ($ data as $ row => $ row_data ) {
204+ $ row = array ();
205+ foreach ($ row_data as $ key => $ value ) {
206+ $ row [] = $ value ;
207+ }
208+ $ table ->addRow ($ row );
209+ }
210+ $ table ->display ();
211+ }
212+
213+ protected function _handleFuncArg (array &$ args = array () , array $ assoc_args = array ()) {
214+ // backups-delete should execute backups_delete function
215+ if (!empty ($ args )){
216+ $ this ->_func = str_replace ("- " , "_ " , array_shift ($ args ));
217+ if (!is_callable (array ($ this , $ this ->_func ), false , $ static )) {
218+ if (array_key_exists ("debug " , $ assoc_args )){
219+ $ this ->_debug (get_defined_vars ());
220+ }
221+ Terminus::error ("I cannot find the requested task to perform it. " );
222+ }
223+ }
224+ }
225+
226+ protected function _handleSiteArg (&$ args , $ assoc_args = array ()) {
227+ $ uuid = null ;
228+ if (array_key_exists ("site " , $ assoc_args )) {
229+ $ uuid = $ this ->_validateSiteUuid ($ assoc_args ["site " ]);
230+ } else {
231+ Terminus::error ("Please specify the site with --site=<sitename> option. " );
232+ }
233+ if (!empty ($ uuid ) && property_exists ($ this ->sites , $ uuid )) {
234+ $ this ->_siteInfo = $ this ->sites ->$ uuid ;
235+ $ this ->_siteInfo ->site_uuid = $ uuid ;
236+ } else {
237+ if (array_key_exists ("debug " , $ assoc_args )){
238+ $ this ->_debug (get_defined_vars ());
239+ }
240+ Terminus::error ("Please specify the site with --site=<sitename> option. " );
241+ }
242+ }
243+
244+ protected function _handleEnvArg (&$ args , $ assoc_args = array ()) {
245+ if (array_key_exists ("env " , $ assoc_args )) {
246+ $ this ->_getEnvBindings ($ args , $ assoc_args );
247+ } else {
248+ Terminus::error ("Please specify the site => environment with --env=<environment> option. " );
249+ }
250+
251+ if (!is_object ($ this ->_bindings )) {
252+ if (array_key_exists ("debug " , $ assoc_args )){
253+ $ this ->_debug (get_defined_vars ());
254+ }
255+ Terminus::error ("Unable to obtain the bindings for the requested environment. \n\n" );
256+ } else {
257+ if (property_exists ($ this ->_bindings , $ assoc_args ['env ' ])) {
258+ $ this ->_env = $ assoc_args ['env ' ];
259+ } else {
260+ Terminus::error ("The requested environment either does not exist or you don't have access to it. " );
261+ }
262+ }
263+ }
264+
265+ protected function _getEnvBindings (&$ args , $ assoc_args ) {
266+ $ b = $ this ->terminus_request ("site " , $ this ->_siteInfo ->site_uuid , 'environments/ ' . $ this ->_env .'/bindings ' , "GET " );
267+ if (!empty ($ b ) && is_array ($ b ) && array_key_exists ("data " , $ b )) {
268+ $ this ->_bindings = $ b ['data ' ];
269+ }
270+ }
271+
272+ protected function _execute ( array $ args = array () , array $ assoc_args = array () ){
273+ $ success = $ this ->{$ this ->_func }( $ args , $ assoc_args );
274+ if (array_key_exists ("debug " , $ assoc_args )){
275+ $ this ->_debug (get_defined_vars ());
276+ }
277+ if (!empty ($ success )){
278+ if (is_array ($ success ) && array_key_exists ("data " , $ success )) {
279+ if (array_key_exists ("json " , $ assoc_args )) {
280+ echo \Terminus \Utils \json_dump ($ success ["data " ]);
281+ } else {
282+ $ this ->_constructTableForResponse ($ success ['data ' ]);
283+ }
284+ } elseif (is_string ($ success )) {
285+ echo Terminus::line ($ success );
286+ }
287+ } else {
288+ if (array_key_exists ("debug " , $ assoc_args )){
289+ $ this ->_debug (get_defined_vars ());
290+ }
291+ Terminus::error ("There was an error attempting to execute the requested task. \n\n" );
292+ }
293+ }
294+
295+ protected function _debug ($ vars ) {
296+ Terminus::line (print_r ($ this , true ));
297+ Terminus::line (print_r ($ vars , true ));
298+ }
299+
174300}
175301
0 commit comments