annotate vendor/jcalderonzumba/gastonjs/src/Browser/BrowserBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Zumba\GastonJS\Browser;
Chris@0 4
Chris@0 5 use Zumba\GastonJS\Exception\BrowserError;
Chris@0 6 use Zumba\GastonJS\Exception\DeadClient;
Chris@0 7 use GuzzleHttp\Client;
Chris@0 8 use GuzzleHttp\Exception\ConnectException;
Chris@0 9 use GuzzleHttp\Exception\ServerException;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Class BrowserBase
Chris@0 13 * @package Zumba\GastonJS\Browser
Chris@0 14 */
Chris@0 15 class BrowserBase {
Chris@0 16 /** @var mixed */
Chris@0 17 protected $logger;
Chris@0 18 /** @var bool */
Chris@0 19 protected $debug;
Chris@0 20 /** @var string */
Chris@0 21 protected $phantomJSHost;
Chris@0 22 /** @var Client */
Chris@0 23 protected $apiClient;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Creates an http client to consume the phantomjs API
Chris@0 27 */
Chris@0 28 protected function createApiClient() {
Chris@0 29 // Provide a BC switch between guzzle 5 and guzzle 6.
Chris@0 30 if (class_exists('GuzzleHttp\Psr7\Response')) {
Chris@0 31 $this->apiClient = new Client(array("base_uri" => $this->getPhantomJSHost()));
Chris@0 32 }
Chris@0 33 else {
Chris@0 34 $this->apiClient = new Client(array("base_url" => $this->getPhantomJSHost()));
Chris@0 35 }
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * TODO: not sure how to do the normalizeKeys stuff fix when needed
Chris@0 40 * @param $keys
Chris@0 41 * @return mixed
Chris@0 42 */
Chris@0 43 protected function normalizeKeys($keys) {
Chris@0 44 return $keys;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * @return Client
Chris@0 49 */
Chris@0 50 public function getApiClient() {
Chris@0 51 return $this->apiClient;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * @return string
Chris@0 56 */
Chris@0 57 public function getPhantomJSHost() {
Chris@0 58 return $this->phantomJSHost;
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * @return mixed
Chris@0 63 */
Chris@0 64 public function getLogger() {
Chris@0 65 return $this->logger;
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Restarts the browser
Chris@0 70 */
Chris@0 71 public function restart() {
Chris@0 72 //TODO: Do we really need to do this?, we are just a client
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Sends a command to the browser
Chris@0 77 * @throws BrowserError
Chris@0 78 * @throws \Exception
Chris@0 79 * @return mixed
Chris@0 80 */
Chris@0 81 public function command() {
Chris@0 82 try {
Chris@0 83 $args = func_get_args();
Chris@0 84 $commandName = $args[0];
Chris@0 85 array_shift($args);
Chris@0 86 $messageToSend = json_encode(array('name' => $commandName, 'args' => $args));
Chris@0 87 /** @var $commandResponse \GuzzleHttp\Psr7\Response|\GuzzleHttp\Message\Response */
Chris@0 88 $commandResponse = $this->getApiClient()->post("/api", array("body" => $messageToSend));
Chris@0 89 $jsonResponse = json_decode($commandResponse->getBody(), TRUE);
Chris@0 90 } catch (ServerException $e) {
Chris@0 91 $jsonResponse = json_decode($e->getResponse()->getBody()->getContents(), true);
Chris@0 92 } catch (ConnectException $e) {
Chris@0 93 throw new DeadClient($e->getMessage(), $e->getCode(), $e);
Chris@0 94 } catch (\Exception $e) {
Chris@0 95 throw $e;
Chris@0 96 }
Chris@0 97
Chris@0 98 if (isset($jsonResponse['error'])) {
Chris@0 99 throw $this->getErrorClass($jsonResponse);
Chris@0 100 }
Chris@0 101
Chris@0 102 return $jsonResponse['response'];
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * @param $error
Chris@0 107 * @return BrowserError
Chris@0 108 */
Chris@0 109 protected function getErrorClass($error) {
Chris@0 110 $errorClassMap = array(
Chris@0 111 'Poltergeist.JavascriptError' => "Zumba\\GastonJS\\Exception\\JavascriptError",
Chris@0 112 'Poltergeist.FrameNotFound' => "Zumba\\GastonJS\\Exception\\FrameNotFound",
Chris@0 113 'Poltergeist.InvalidSelector' => "Zumba\\GastonJS\\Exception\\InvalidSelector",
Chris@0 114 'Poltergeist.StatusFailError' => "Zumba\\GastonJS\\Exception\\StatusFailError",
Chris@0 115 'Poltergeist.NoSuchWindowError' => "Zumba\\GastonJS\\Exception\\NoSuchWindowError",
Chris@0 116 'Poltergeist.ObsoleteNode' => "Zumba\\GastonJS\\Exception\\ObsoleteNode"
Chris@0 117 );
Chris@0 118 if (isset($error['error']['name']) && isset($errorClassMap[$error["error"]["name"]])) {
Chris@0 119 return new $errorClassMap[$error["error"]["name"]]($error);
Chris@0 120 }
Chris@0 121
Chris@0 122 return new BrowserError($error);
Chris@0 123 }
Chris@0 124 }