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