|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the php-phantomjs. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace JonnyW\PhantomJs\Procedure; |
|
10
|
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Engine; |
|
12
|
|
|
use JonnyW\PhantomJs\Cache\CacheInterface; |
|
13
|
|
|
use JonnyW\PhantomJs\Parser\ParserInterface; |
|
14
|
|
|
use JonnyW\PhantomJs\Template\TemplateRendererInterface; |
|
15
|
|
|
use JonnyW\PhantomJs\Exception\NotWritableException; |
|
16
|
|
|
use JonnyW\PhantomJs\Exception\ProcedureFailedException; |
|
17
|
|
|
use JonnyW\PhantomJs\StringUtils; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PHP PhantomJs |
|
21
|
|
|
* |
|
22
|
|
|
* @author Jon Wenmoth <[email protected]> |
|
23
|
|
|
*/ |
|
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
|
56 |
|
public function __construct(Engine $engine, ParserInterface $parser, CacheInterface $cacheHandler, TemplateRendererInterface $renderer) |
|
76
|
|
|
{ |
|
77
|
56 |
|
$this->engine = $engine; |
|
78
|
56 |
|
$this->parser = $parser; |
|
79
|
56 |
|
$this->cacheHandler = $cacheHandler; |
|
80
|
56 |
|
$this->renderer = $renderer; |
|
81
|
56 |
|
} |
|
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
|
49 |
|
public function run(InputInterface $input, OutputInterface $output) |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
|
|
97
|
49 |
|
$executable = $this->write( |
|
98
|
49 |
|
$this->compile($input) |
|
99
|
49 |
|
); |
|
100
|
|
|
|
|
101
|
|
|
$descriptorspec = array( |
|
102
|
48 |
|
array('pipe', 'r'), |
|
103
|
48 |
|
array('pipe', 'w'), |
|
104
|
48 |
|
array('pipe', 'w') |
|
105
|
48 |
|
); |
|
106
|
|
|
|
|
107
|
48 |
|
$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
|
47 |
|
); |
|
125
|
|
|
|
|
126
|
47 |
|
$this->engine->log($log); |
|
127
|
|
|
|
|
128
|
47 |
|
$this->remove($executable); |
|
129
|
|
|
|
|
130
|
49 |
|
} catch (NotWritableException $e) { |
|
131
|
1 |
|
throw $e; |
|
132
|
1 |
|
} catch (\Exception $e) { |
|
133
|
|
|
|
|
134
|
1 |
|
if (isset($executable)) { |
|
135
|
1 |
|
$this->remove($executable); |
|
136
|
1 |
|
} |
|
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) |
|
150
|
|
|
{ |
|
151
|
51 |
|
$this->template = $template; |
|
152
|
|
|
|
|
153
|
51 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get procedure template. |
|
158
|
|
|
* |
|
159
|
|
|
* @access public |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
52 |
|
public function getTemplate() |
|
163
|
|
|
{ |
|
164
|
52 |
|
return $this->template; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Compile procedure. |
|
169
|
|
|
* |
|
170
|
|
|
* @access public |
|
171
|
|
|
* @param \JonnyW\PhantomJs\Procedure\InputInterface $input |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
50 |
|
public function compile(InputInterface $input) |
|
175
|
|
|
{ |
|
176
|
50 |
|
return $this->renderer->render($this->getTemplate(), array('input' => $input)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Write compiled procedure to cache. |
|
181
|
|
|
* |
|
182
|
|
|
* @access protected |
|
183
|
|
|
* @param string $compiled |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
49 |
|
protected function write($compiled) |
|
187
|
|
|
{ |
|
188
|
49 |
|
return $this->cacheHandler->save(StringUtils::random(20), $compiled); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Remove procedure script cache. |
|
193
|
|
|
* |
|
194
|
|
|
* @access protected |
|
195
|
|
|
* @param string $filePath |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
48 |
|
protected function remove($filePath) |
|
199
|
|
|
{ |
|
200
|
48 |
|
$this->cacheHandler->delete($filePath); |
|
201
|
48 |
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|