Example #1
0
 /**
  * Run procedure.
  *
  * @access public
  * @param  \JonnyW\PhantomJs\Procedure\InputInterface           $input
  * @param  \JonnyW\PhantomJs\Procedure\OutputInterface          $output
  * @throws \JonnyW\PhantomJs\Exception\ProcedureFailedException
  * @throws \JonnyW\PhantomJs\Exception\NotWritableException
  * @return void
  */
 public function run(InputInterface $input, OutputInterface $output)
 {
     try {
         $executable = $this->write($this->compile($input));
         $descriptorspec = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
         $process = proc_open(escapeshellcmd(sprintf('%s %s', $this->engine->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);
         $output->import($this->parser->parse($result));
         $this->engine->log($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', $e->getMessage()));
     }
 }
Example #2
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()));
     }
 }