1 | <?php |
||
24 | class Procedure implements ProcedureInterface |
||
25 | { |
||
26 | /** |
||
27 | * PhantomJS engine |
||
28 | * |
||
29 | * @var \JonnyW\PhantomJs\Engine |
||
30 | * @access protected |
||
31 | */ |
||
32 | protected $engine; |
||
33 | |||
34 | /** |
||
35 | * Parser instance. |
||
36 | * |
||
37 | * @var \JonnyW\PhantomJs\Parser\ParserInterface |
||
38 | * @access protected |
||
39 | */ |
||
40 | protected $parser; |
||
41 | |||
42 | /** |
||
43 | * Cache handler instance. |
||
44 | * |
||
45 | * @var \JonnyW\PhantomJs\Cache\CacheInterface |
||
46 | * @access protected |
||
47 | */ |
||
48 | protected $cacheHandler; |
||
49 | |||
50 | /** |
||
51 | * Template renderer. |
||
52 | * |
||
53 | * @var \JonnyW\PhantomJs\Template\TemplateRendererInterface |
||
54 | * @access protected |
||
55 | */ |
||
56 | protected $renderer; |
||
57 | |||
58 | /** |
||
59 | * Procedure template. |
||
60 | * |
||
61 | * @var string |
||
62 | * @access protected |
||
63 | */ |
||
64 | protected $template; |
||
65 | |||
66 | /** |
||
67 | * Internal constructor. |
||
68 | * |
||
69 | * @access public |
||
70 | * @param \JonnyW\PhantomJs\Engine $engine |
||
71 | * @param \JonnyW\PhantomJs\Parser\ParserInterface $parser |
||
72 | * @param \JonnyW\PhantomJs\Cache\CacheInterface $cacheHandler |
||
73 | * @param \JonnyW\PhantomJs\Template\TemplateRendererInterface $renderer |
||
74 | */ |
||
75 | 55 | public function __construct(Engine $engine, ParserInterface $parser, CacheInterface $cacheHandler, TemplateRendererInterface $renderer) |
|
82 | |||
83 | /** |
||
84 | * Run procedure. |
||
85 | * |
||
86 | * @access public |
||
87 | * @param \JonnyW\PhantomJs\Procedure\InputInterface $input |
||
88 | * @param \JonnyW\PhantomJs\Procedure\OutputInterface $output |
||
89 | * @throws \JonnyW\PhantomJs\Exception\ProcedureFailedException |
||
90 | * @throws \JonnyW\PhantomJs\Exception\NotWritableException |
||
91 | * @return void |
||
92 | */ |
||
93 | 48 | public function run(InputInterface $input, OutputInterface $output) |
|
94 | { |
||
95 | try { |
||
96 | |||
97 | 48 | $executable = $this->write( |
|
98 | 48 | $this->compile($input) |
|
99 | ); |
||
100 | |||
101 | $descriptorspec = array( |
||
102 | 47 | array('pipe', 'r'), |
|
103 | array('pipe', 'w'), |
||
104 | array('pipe', 'w') |
||
105 | ); |
||
106 | |||
107 | 47 | $process = proc_open(escapeshellcmd(sprintf('%s %s', $this->engine->getCommand(), $executable)), $descriptorspec, $pipes, null, null); |
|
108 | |||
109 | 47 | if (!is_resource($process)) { |
|
110 | throw new ProcedureFailedException('proc_open() did not return a resource'); |
||
111 | } |
||
112 | |||
113 | 47 | $result = stream_get_contents($pipes[1]); |
|
114 | 47 | $log = stream_get_contents($pipes[2]); |
|
115 | |||
116 | 47 | fclose($pipes[0]); |
|
117 | 47 | fclose($pipes[1]); |
|
118 | 47 | fclose($pipes[2]); |
|
119 | |||
120 | 47 | proc_close($process); |
|
121 | |||
122 | 47 | $output->import( |
|
123 | 47 | $this->parser->parse($result) |
|
124 | ); |
||
125 | |||
126 | 47 | $this->engine->log($log); |
|
127 | |||
128 | 47 | $this->remove($executable); |
|
129 | |||
130 | 1 | } catch (NotWritableException $e) { |
|
131 | throw $e; |
||
132 | 1 | } catch (\Exception $e) { |
|
133 | |||
134 | 1 | if (isset($executable)) { |
|
135 | $this->remove($executable); |
||
136 | } |
||
137 | |||
138 | 1 | throw new ProcedureFailedException(sprintf('Error when executing PhantomJs procedure - %s', $e->getMessage())); |
|
139 | } |
||
140 | 47 | } |
|
141 | |||
142 | /** |
||
143 | * Set procedure template. |
||
144 | * |
||
145 | * @access public |
||
146 | * @param string $template |
||
147 | * @return \JonnyW\PhantomJs\Procedure\Procedure |
||
148 | */ |
||
149 | 51 | public function setTemplate($template) |
|
155 | |||
156 | /** |
||
157 | * Get procedure template. |
||
158 | * |
||
159 | * @access public |
||
160 | * @return string |
||
161 | */ |
||
162 | 51 | public function getTemplate() |
|
166 | |||
167 | /** |
||
168 | * Compile procedure. |
||
169 | * |
||
170 | * @access public |
||
171 | * @param \JonnyW\PhantomJs\Procedure\InputInterface $input |
||
172 | * @return void |
||
173 | */ |
||
174 | 49 | public function compile(InputInterface $input) |
|
178 | |||
179 | /** |
||
180 | * Write compiled procedure to cache. |
||
181 | * |
||
182 | * @access protected |
||
183 | * @param string $compiled |
||
184 | * @return string |
||
185 | */ |
||
186 | 47 | protected function write($compiled) |
|
190 | |||
191 | /** |
||
192 | * Remove procedure script cache. |
||
193 | * |
||
194 | * @access protected |
||
195 | * @param string $filePath |
||
196 | * @return void |
||
197 | */ |
||
198 | 47 | protected function remove($filePath) |
|
202 | } |
||
203 |