/** * Get the API client object. * * @param bool $autoLogin Whether to log in, if the client is not already * authenticated (default: true). * * @return PlatformClient */ public function getClient($autoLogin = true) { if (!isset(self::$client)) { $connectorOptions = []; if (getenv('PLATFORMSH_CLI_ACCOUNTS_SITE')) { $connectorOptions['accounts'] = getenv('PLATFORMSH_CLI_ACCOUNTS_SITE'); } $connectorOptions['verify'] = !getenv('PLATFORMSH_CLI_SKIP_SSL'); $connectorOptions['debug'] = (bool) getenv('PLATFORMSH_CLI_DEBUG'); $connectorOptions['client_id'] = 'platform-cli'; $connectorOptions['user_agent'] = $this->getUserAgent(); // Proxy support with the http_proxy or https_proxy environment // variables. $proxies = []; foreach (['https', 'http'] as $scheme) { $proxies[$scheme] = str_replace('http://', 'tcp://', getenv($scheme . '_proxy')); } $proxies = array_filter($proxies); if (count($proxies)) { $connectorOptions['proxy'] = count($proxies) == 1 ? reset($proxies) : $proxies; } $connector = new Connector($connectorOptions); // If an API token is set, that's all we need to authenticate. if (isset(self::$apiToken)) { $connector->setApiToken(self::$apiToken); } else { $session = $connector->getSession(); $session->setId('cli-' . self::$sessionId); $session->setStorage(new File($this->getSessionsDir())); } $this->debug('Initializing API client'); self::$client = new PlatformClient($connector); if ($autoLogin && !$connector->isLoggedIn()) { $this->debug('The user is not logged in yet'); $this->login(); } } return self::$client; }