/** * 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())); } }
/** * Send request * * @access public * @param \JonnyW\PhantomJs\Message\RequestInterface $request * @param \JonnyW\PhantomJs\Message\ResponseInterface $response * @return \JonnyW\PhantomJs\Message\ResponseInterface */ public function send(RequestInterface $request, ResponseInterface $response) { $this->clearLog(); $procedure = $this->procedureLoader->load($request->getType()); $procedure->run($this, $request, $response); return $response; }