1 | <?php |
||
14 | class Driver_Phantomjs_Connection { |
||
15 | |||
16 | /** |
||
17 | * The file storing the pid of the current phantomjs server process |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $_pid_file; |
||
21 | |||
22 | /** |
||
23 | * The pid of the current phantomjs server process |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $_pid; |
||
27 | |||
28 | /** |
||
29 | * Ulr of the phantomjs server |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $_server = 'http://localhost'; |
||
33 | |||
34 | /** |
||
35 | * Port of the phantomjs server |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $_port; |
||
39 | |||
40 | /** |
||
41 | * Phantomjs binary |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $_phantomjs_binary = 'phantomjs'; |
||
45 | |||
46 | /** |
||
47 | * Getter / Setter of the phantomjs server port. |
||
48 | * If none is set it tries to fine an unused port between 4445 and 5000 |
||
49 | * @param string $port |
||
50 | * @return string|Driver_Phantomjs_Connection |
||
51 | */ |
||
52 | public function port($port = NULL) |
||
67 | |||
68 | /** |
||
69 | * Getter / Setter of the current phantomjs server url |
||
70 | * @param string $server |
||
71 | * @return string|Driver_Phantomjs_Connection |
||
72 | */ |
||
73 | public function server($server = NULL) |
||
82 | |||
83 | /** |
||
84 | * Get the host of the current phantomjs server (without the protocol part) |
||
85 | * @return string |
||
86 | */ |
||
87 | public function host() |
||
91 | |||
92 | /** |
||
93 | * Getter, get the current phantomjs server process pid |
||
94 | * @return string |
||
95 | */ |
||
96 | public function pid() |
||
100 | |||
101 | public function __construct($server = NULL) |
||
108 | |||
109 | /** |
||
110 | * Start a new phantomjs server, optionally provide pid_file and log file. |
||
111 | * If you provide a pid_file, it will kill the process currently running on that pid, before starting the new one |
||
112 | * If the start is unsuccessfull it will return FALSE |
||
113 | * @param string $pid_file |
||
114 | * @param string $log_file |
||
115 | * @return boolean |
||
116 | */ |
||
117 | public function start($pid_file = NULL, $log_file = '/dev/null') |
||
142 | |||
143 | /** |
||
144 | * Check if the phantomjs server has been started |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function is_started() |
||
151 | |||
152 | /** |
||
153 | * Check if the phantomjs server is actually running (the port is taken) |
||
154 | * @return boolean |
||
155 | */ |
||
156 | public function is_running() |
||
160 | |||
161 | /** |
||
162 | * Gracefully stop the phantomjs server. Return FALSE on failure. Clear the pid_file if set |
||
163 | * @return boolean |
||
164 | */ |
||
165 | public function stop() |
||
183 | |||
184 | /** |
||
185 | * Getter - get the current pid_file |
||
186 | * @return string |
||
187 | */ |
||
188 | public function pid_file() |
||
192 | |||
193 | /** |
||
194 | * Perform a get request on the phantomjs server |
||
195 | * @param string $command |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function get($command) |
||
202 | |||
203 | /** |
||
204 | * Perform a post request on the phantomjs server |
||
205 | * @param string $command |
||
206 | * @param array $params |
||
207 | * @return mixed |
||
208 | */ |
||
209 | public function post($command, array $params) |
||
217 | |||
218 | /** |
||
219 | * Perform a delete request on the phantomjs server |
||
220 | * @param string $command |
||
221 | * @return mixed |
||
222 | */ |
||
223 | public function delete($command) |
||
230 | |||
231 | /** |
||
232 | * Get the full url of a command (including server and port) |
||
233 | * @param string $command |
||
234 | * @return string |
||
235 | */ |
||
236 | public function command_url($command) |
||
240 | |||
241 | /** |
||
242 | * Set phantomjs binary location |
||
243 | * @param string $binary |
||
244 | * @throws Exception |
||
245 | */ |
||
246 | public function set_phantomjs_binary($binary) { |
||
252 | |||
253 | /** |
||
254 | * Perform a custom request on the phantomjs server, using curl |
||
255 | * @param string $command |
||
256 | * @param array $options |
||
257 | * @return mixed |
||
258 | */ |
||
259 | protected function call($command, array $options = array()) |
||
290 | |||
291 | /** |
||
292 | * Start a phantomjs server in the background. Set port, server js file, additional files and log file. |
||
293 | * |
||
294 | * @param string $file the server js file |
||
295 | * @param integer $port the port to start the server on |
||
296 | * @param string $additional additional file, passed to the js server |
||
297 | * @param string $log_file |
||
298 | * @return string the pid of the newly started process |
||
299 | * @throws Exception |
||
300 | */ |
||
301 | protected function _start($file, $port, $additional = NULL, $log_file = '/dev/null') |
||
314 | |||
315 | /** |
||
316 | * kill a server on a given pid |
||
317 | * @param string $pid |
||
318 | */ |
||
319 | protected function _kill($pid) |
||
323 | |||
324 | /** |
||
325 | * Return the command to start the phantomjs server |
||
326 | * |
||
327 | * @param string $file the server js file |
||
328 | * @param integer $port |
||
329 | * @param string $additional additional js file |
||
330 | * @return string |
||
331 | * @throws Exception |
||
332 | */ |
||
333 | protected function _command($file, $port, $additional = NULL) |
||
352 | } |
||
353 |