Chris@0: phantomHost = $phantomHost; Chris@0: $this->browser = new Browser($phantomHost); Chris@0: $this->templateLoader = new \Twig_Loader_Filesystem(realpath(__DIR__ . '/Resources/Script')); Chris@0: $this->templateEnv = new \Twig_Environment($this->templateLoader, array('cache' => $this->templateCacheSetup($templateCache), 'strict_variables' => true)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets up the cache template location for the scripts we are going to create with the driver Chris@0: * @param $templateCache Chris@0: * @return string Chris@0: * @throws DriverException Chris@0: */ Chris@0: protected function templateCacheSetup($templateCache) { Chris@0: $cacheDir = $templateCache; Chris@0: if ($templateCache === null) { Chris@0: $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "jcalderonzumba" . DIRECTORY_SEPARATOR . "phantomjs"; Chris@0: if (!file_exists($cacheDir)) { Chris@0: mkdir($cacheDir, 0777, true); Chris@0: } Chris@0: } Chris@0: Chris@0: if (!file_exists($cacheDir)) { Chris@0: throw new DriverException("Template cache $cacheDir directory does not exist"); Chris@0: } Chris@0: return $cacheDir; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper to find a node element given an xpath Chris@0: * @param string $xpath Chris@0: * @param int $max Chris@0: * @returns int Chris@0: * @throws DriverException Chris@0: */ Chris@0: protected function findElement($xpath, $max = 1) { Chris@0: $elements = $this->browser->find("xpath", $xpath); Chris@0: if (!isset($elements["page_id"]) || !isset($elements["ids"]) || count($elements["ids"]) !== $max) { Chris@0: throw new DriverException("Failed to get elements with given $xpath"); Chris@0: } Chris@0: return $elements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return Browser Chris@0: */ Chris@0: public function getBrowser() { Chris@0: return $this->browser; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return \Twig_Environment Chris@0: */ Chris@0: public function getTemplateEnv() { Chris@0: return $this->templateEnv; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a javascript script via twig template engine Chris@0: * @param $templateName Chris@0: * @param $viewData Chris@0: * @return string Chris@0: */ Chris@0: public function javascriptTemplateRender($templateName, $viewData) { Chris@0: /** @var $templateEngine \Twig_Environment */ Chris@0: $templateEngine = $this->getTemplateEnv(); Chris@0: return $templateEngine->render($templateName, $viewData); Chris@0: } Chris@0: Chris@0: }