Chris@0: apiClient = new Client(array("base_uri" => $this->getPhantomJSHost())); Chris@0: } Chris@0: else { Chris@0: $this->apiClient = new Client(array("base_url" => $this->getPhantomJSHost())); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * TODO: not sure how to do the normalizeKeys stuff fix when needed Chris@0: * @param $keys Chris@0: * @return mixed Chris@0: */ Chris@0: protected function normalizeKeys($keys) { Chris@0: return $keys; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return Client Chris@0: */ Chris@0: public function getApiClient() { Chris@0: return $this->apiClient; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getPhantomJSHost() { Chris@0: return $this->phantomJSHost; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return mixed Chris@0: */ Chris@0: public function getLogger() { Chris@0: return $this->logger; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Restarts the browser Chris@0: */ Chris@0: public function restart() { Chris@0: //TODO: Do we really need to do this?, we are just a client Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sends a command to the browser Chris@0: * @throws BrowserError Chris@0: * @throws \Exception Chris@0: * @return mixed Chris@0: */ Chris@0: public function command() { Chris@0: try { Chris@0: $args = func_get_args(); Chris@0: $commandName = $args[0]; Chris@0: array_shift($args); Chris@0: $messageToSend = json_encode(array('name' => $commandName, 'args' => $args)); Chris@0: /** @var $commandResponse \GuzzleHttp\Psr7\Response|\GuzzleHttp\Message\Response */ Chris@0: $commandResponse = $this->getApiClient()->post("/api", array("body" => $messageToSend)); Chris@0: $jsonResponse = json_decode($commandResponse->getBody(), TRUE); Chris@0: } catch (ServerException $e) { Chris@0: $jsonResponse = json_decode($e->getResponse()->getBody()->getContents(), true); Chris@0: } catch (ConnectException $e) { Chris@0: throw new DeadClient($e->getMessage(), $e->getCode(), $e); Chris@0: } catch (\Exception $e) { Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: if (isset($jsonResponse['error'])) { Chris@0: throw $this->getErrorClass($jsonResponse); Chris@0: } Chris@0: Chris@0: return $jsonResponse['response']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param $error Chris@0: * @return BrowserError Chris@0: */ Chris@0: protected function getErrorClass($error) { Chris@0: $errorClassMap = array( Chris@0: 'Poltergeist.JavascriptError' => "Zumba\\GastonJS\\Exception\\JavascriptError", Chris@0: 'Poltergeist.FrameNotFound' => "Zumba\\GastonJS\\Exception\\FrameNotFound", Chris@0: 'Poltergeist.InvalidSelector' => "Zumba\\GastonJS\\Exception\\InvalidSelector", Chris@0: 'Poltergeist.StatusFailError' => "Zumba\\GastonJS\\Exception\\StatusFailError", Chris@0: 'Poltergeist.NoSuchWindowError' => "Zumba\\GastonJS\\Exception\\NoSuchWindowError", Chris@0: 'Poltergeist.ObsoleteNode' => "Zumba\\GastonJS\\Exception\\ObsoleteNode" Chris@0: ); Chris@0: if (isset($error['error']['name']) && isset($errorClassMap[$error["error"]["name"]])) { Chris@0: return new $errorClassMap[$error["error"]["name"]]($error); Chris@0: } Chris@0: Chris@0: return new BrowserError($error); Chris@0: } Chris@0: }