annotate vendor/jcalderonzumba/mink-phantomjs-driver/src/BasePhantomJSDriver.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Zumba\Mink\Driver;
Chris@0 4
Chris@0 5 use Behat\Mink\Driver\CoreDriver;
Chris@0 6 use Behat\Mink\Exception\DriverException;
Chris@0 7 use Behat\Mink\Session;
Chris@0 8 use Zumba\GastonJS\Browser\Browser;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Class BasePhantomJSDriver
Chris@0 12 * @package Zumba\Mink\Driver
Chris@0 13 */
Chris@0 14 class BasePhantomJSDriver extends CoreDriver {
Chris@0 15
Chris@0 16 /** @var Session */
Chris@0 17 protected $session;
Chris@0 18 /** @var Browser */
Chris@0 19 protected $browser;
Chris@0 20 /** @var string */
Chris@0 21 protected $phantomHost;
Chris@0 22 /** @var \Twig_Loader_Filesystem */
Chris@0 23 protected $templateLoader;
Chris@0 24 /** @var \Twig_Environment */
Chris@0 25 protected $templateEnv;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Instantiates the driver
Chris@0 29 * @param string $phantomHost browser "api" oriented host
Chris@0 30 * @param string $templateCache where we are going to store the templates cache
Chris@0 31 */
Chris@0 32 public function __construct($phantomHost, $templateCache = null) {
Chris@0 33 $this->phantomHost = $phantomHost;
Chris@0 34 $this->browser = new Browser($phantomHost);
Chris@0 35 $this->templateLoader = new \Twig_Loader_Filesystem(realpath(__DIR__ . '/Resources/Script'));
Chris@0 36 $this->templateEnv = new \Twig_Environment($this->templateLoader, array('cache' => $this->templateCacheSetup($templateCache), 'strict_variables' => true));
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Sets up the cache template location for the scripts we are going to create with the driver
Chris@0 41 * @param $templateCache
Chris@0 42 * @return string
Chris@0 43 * @throws DriverException
Chris@0 44 */
Chris@0 45 protected function templateCacheSetup($templateCache) {
Chris@0 46 $cacheDir = $templateCache;
Chris@0 47 if ($templateCache === null) {
Chris@0 48 $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "jcalderonzumba" . DIRECTORY_SEPARATOR . "phantomjs";
Chris@0 49 if (!file_exists($cacheDir)) {
Chris@0 50 mkdir($cacheDir, 0777, true);
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 if (!file_exists($cacheDir)) {
Chris@0 55 throw new DriverException("Template cache $cacheDir directory does not exist");
Chris@0 56 }
Chris@0 57 return $cacheDir;
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Helper to find a node element given an xpath
Chris@0 62 * @param string $xpath
Chris@0 63 * @param int $max
Chris@0 64 * @returns int
Chris@0 65 * @throws DriverException
Chris@0 66 */
Chris@0 67 protected function findElement($xpath, $max = 1) {
Chris@0 68 $elements = $this->browser->find("xpath", $xpath);
Chris@0 69 if (!isset($elements["page_id"]) || !isset($elements["ids"]) || count($elements["ids"]) !== $max) {
Chris@0 70 throw new DriverException("Failed to get elements with given $xpath");
Chris@0 71 }
Chris@0 72 return $elements;
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 * @param Session $session
Chris@0 78 */
Chris@0 79 public function setSession(Session $session) {
Chris@0 80 $this->session = $session;
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * @return Browser
Chris@0 85 */
Chris@0 86 public function getBrowser() {
Chris@0 87 return $this->browser;
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * @return \Twig_Environment
Chris@0 92 */
Chris@0 93 public function getTemplateEnv() {
Chris@0 94 return $this->templateEnv;
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Returns a javascript script via twig template engine
Chris@0 99 * @param $templateName
Chris@0 100 * @param $viewData
Chris@0 101 * @return string
Chris@0 102 */
Chris@0 103 public function javascriptTemplateRender($templateName, $viewData) {
Chris@0 104 /** @var $templateEngine \Twig_Environment */
Chris@0 105 $templateEngine = $this->getTemplateEnv();
Chris@0 106 return $templateEngine->render($templateName, $viewData);
Chris@0 107 }
Chris@0 108
Chris@0 109 }