Example #1
0
 /**
  * Run procedure.
  *
  * @access public
  * @param  \JonnyW\PhantomJs\ClientInterface                    $client
  * @param  \JonnyW\PhantomJs\Message\RequestInterface           $request
  * @param  \JonnyW\PhantomJs\Message\ResponseInterface          $response
  * @throws \JonnyW\PhantomJs\Exception\ProcedureFailedException
  * @throws \Exception
  * @throws \JonnyW\PhantomJs\Exception\NotWritableException
  * @return void
  */
 public function run(ClientInterface $client, RequestInterface $request, ResponseInterface $response)
 {
     try {
         $template = $this->getProcedure();
         $procedure = $this->renderer->render($template, array('request' => $request));
         $executable = $this->write($procedure);
         $descriptorspec = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
         $process = proc_open(escapeshellcmd(sprintf('%s %s', $client->getCommand(), $executable)), $descriptorspec, $pipes, null, null);
         if (!is_resource($process)) {
             throw new ProcedureFailedException('proc_open() did not return a resource');
         }
         $result = stream_get_contents($pipes[1]);
         $log = stream_get_contents($pipes[2]);
         fclose($pipes[0]);
         fclose($pipes[1]);
         fclose($pipes[2]);
         proc_close($process);
         $response->import($this->parser->parse($result));
         $client->setLog($log);
         $this->remove($executable);
     } catch (NotWritableException $e) {
         throw $e;
     } catch (\Exception $e) {
         if (isset($executable)) {
             $this->remove($executable);
         }
         throw new ProcedureFailedException(sprintf('Error when executing PhantomJs procedure "%s" - %s', $request->getType(), $e->getMessage()));
     }
 }
 /**
  * Compile partials into procedure.
  *
  * @access public
  * @param  \JonnyW\PhantomJs\Procedure\ProcedureInterface $procedure
  * @param  \JonnyW\PhantomJs\Procedure\InputInterface     $input
  * @return void
  */
 public function compile(ProcedureInterface $procedure, InputInterface $input)
 {
     $cacheKey = sprintf('phantomjs_%s_%s', $input->getType(), md5($procedure->getTemplate()));
     if ($this->cacheEnabled && $this->cacheHandler->exists($cacheKey)) {
         $template = $this->cacheHandler->fetch($cacheKey);
     }
     if (empty($template)) {
         $template = $this->renderer->render($procedure->getTemplate(), array('engine' => $this, 'procedure_type' => $input->getType()));
         $test = clone $procedure;
         $test->setTemplate($template);
         $compiled = $test->compile($input);
         $this->procedureValidator->validate($compiled);
         if ($this->cacheEnabled) {
             $this->cacheHandler->save($cacheKey, $template);
         }
     }
     $procedure->setTemplate($template);
 }
Example #3
0
 /**
  * Compile procedure.
  *
  * @access public
  * @param  \JonnyW\PhantomJs\Procedure\InputInterface $input
  * @return void
  */
 public function compile(InputInterface $input)
 {
     return $this->renderer->render($this->getTemplate(), array('input' => $input));
 }