/** * Instantiates object, sets cache and session * * @return [TerminusCommand] $this */ public function __construct() { //Load commonly used data from cache $this->cache = Terminus::get_cache(); $this->logger = Terminus::get_logger(); $this->outputter = Terminus::get_outputter(); $this->session = Session::instance(); if (!Terminus::is_test()) { $this->checkForUpdate(); } }
/** * Make a request to the Dashbord's internal API. * * @param $realm * Permissions realm for data request: currently "user" or "site" but in the * future this could also be "organization" or another high-level business * object (e.g. "product" for managing your app). Can also be "public" to * simply pull read-only data that is not privileged. * * @param $uuid * The UUID of the item in the realm you want to access. * * @param $method * HTTP method (verb) to use. * * @param $data * A native PHP data structure (int, string, arary or simple object) to be * sent along with the request. Will be encoded as JSON for you. */ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $options = NULL) { if (!in_array($realm, array('login', 'user', 'public')) and !Terminus::is_test()) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); // combine session realm uuid and path to get a unique key // @todo need cache "groups" $cachekey = md5(Session::getValue('user_uuid') . $uuid . $realm . $path); $data = $cache->get_data($cachekey); // check the request cache if ("GET" == $method and !Terminus::get_config('nocache') and !getenv('CLI_TEST_MODE') and !empty($data)) { if (Terminus::get_config('debug')) { Logger::debug('CacheKey: ' . $cachekey); } return (array) $data; } // for some methods we'll assume the cache should be invalidated if (in_array($method, array("POST", "PUT", "DELETE"))) { $cache->flush(null, 'session'); } if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Logger::debug('Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(TRUE); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); $cache->put_data($cachekey, $data); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); \Terminus::error("%s", $response->getBody(TRUE)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); //die(Terminus_Command::stripSensitiveData()); $sanitized_request = Terminus_Command::stripSensitiveData((string) $request, Terminus_Command::$_blacklist); \Terminus::error("Request %s had failed: %s", array($sanitized_request, $e->getMessage())); } catch (Exception $e) { \Terminus::error("Unrecognised request failure: %s", $e->getMessage()); } }
/** * Launch another Terminus command using the runtime arguments for the current process * * @param string Command to call * @param array $args Positional arguments to use * @param array $assoc_args Associative arguments to use * @param bool Whether to exit if the command returns an error status * * @return int The command exit status */ static function launch_self($command, $args = array(), $assoc_args = array(), $exit_on_error = true) { $reused_runtime_args = array('path', 'url', 'user', 'allow-root'); foreach ($reused_runtime_args as $key) { if (array_key_exists($key, self::get_runner()->config)) { $assoc_args[$key] = self::get_runner()->config[$key]; } } $php_bin = self::get_php_binary(); if (Terminus::is_test()) { $script_path = __DIR__ . '/boot-fs.php'; } else { $script_path = $GLOBALS['argv'][0]; } $args = implode(' ', array_map('escapeshellarg', $args)); $assoc_args = \Terminus\Utils\assoc_args_to_str($assoc_args); $full_command = "{$php_bin} {$script_path} {$command} {$args} {$assoc_args}"; return self::launch($full_command, $exit_on_error); }
function testIsTest() { $this->assertTrue(\Terminus::is_test()); }
/** * Execute the login based on email,password * * @param $email string (required) * @param $password string (required) * @package Terminus * @version 0.04-alpha * @return string */ private function doLogin($email, $password) { if (Terminus::is_test()) { $data = array('user_uuid' => '77629472-3050-457c-8c3d-32b2cabf992b', 'session' => '77629472-3050-457c-8c3d-32b2cabf992b:7dc42f40-65f8-11e4-b314-bc764e100eb1:ZHR0TgtQYsKcOOwMOd0tk', 'session_expire_time' => '1417727066', 'email' => '*****@*****.**'); return $data; } $options = array('body' => json_encode(array('email' => $email, 'password' => $password)), 'headers' => array('Content-type' => 'application/json')); $response = Terminus_Command::request('login', '', '', 'POST', $options); if (!$response or '200' != @$response['info']['http_code']) { \Terminus::error("[auth_error]: unsuccessful login"); } // Prepare credentials for storage. $data = array('user_uuid' => $response['data']->user_id, 'session' => $response['data']->session, 'session_expire_time' => $response['data']->expires_at, 'email' => $email); // creates a session instance Session::instance()->setData($data); return $data; }