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 | * Getter / Setter of the phantomjs server port. |
||
42 | * If none is set it tries to fine an unused port between 4445 and 5000 |
||
43 | * @param string $port |
||
44 | * @return string|Driver_Phantomjs_Connection |
||
45 | */ |
||
46 | 15 | public function port($port = NULL) |
|
47 | { |
||
48 | 15 | if ($port !== NULL) |
|
49 | 15 | { |
|
50 | 2 | $this->_port = $port; |
|
51 | 2 | return $this; |
|
52 | } |
||
53 | |||
54 | 15 | if ( ! $this->_port) |
|
55 | 15 | { |
|
56 | 2 | $this->_port = Network::ephimeral_port($this->host(), 4445, 5000); |
|
57 | 2 | } |
|
58 | |||
59 | 15 | return $this->_port; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Getter / Setter of the current phantomjs server url |
||
64 | * @param string $server |
||
65 | * @return string|Driver_Phantomjs_Connection |
||
66 | */ |
||
67 | 15 | public function server($server = NULL) |
|
68 | { |
||
69 | 15 | if ($server !== NULL) |
|
70 | 15 | { |
|
71 | 2 | $this->_server = $server; |
|
72 | 2 | return $this; |
|
73 | } |
||
74 | 15 | return $this->_server; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the host of the current phantomjs server (without the protocol part) |
||
79 | * @return string |
||
80 | */ |
||
81 | 2 | public function host() |
|
85 | |||
86 | /** |
||
87 | * Getter, get the current phantomjs server process pid |
||
88 | * @return string |
||
89 | */ |
||
90 | 1 | public function pid() |
|
94 | |||
95 | 3 | public function __construct($server = NULL) |
|
96 | { |
||
97 | if ($server) |
||
98 | 3 | { |
|
99 | 2 | $this->server($server); |
|
100 | 2 | } |
|
101 | 3 | } |
|
102 | |||
103 | /** |
||
104 | * Start a new phantomjs server, optionally provide pid_file and log file. |
||
105 | * If you provide a pid_file, it will kill the process currently running on that pid, before starting the new one |
||
106 | * If the start is unsuccessfull it will return FALSE |
||
107 | * @param string $pid_file |
||
108 | * @param string $log_file |
||
109 | * @return boolean |
||
110 | */ |
||
111 | 2 | public function start($pid_file = NULL, $log_file = '/dev/null') |
|
112 | { |
||
113 | if ($pid_file) |
||
114 | 2 | { |
|
115 | 1 | $this->_pid_file = $pid_file; |
|
116 | 1 | if (is_file($this->_pid_file)) |
|
117 | 1 | { |
|
118 | 1 | Phantomjs::kill(file_get_contents($pid_file)); |
|
119 | 1 | unlink($this->_pid_file); |
|
120 | 1 | } |
|
121 | 1 | } |
|
122 | |||
123 | 2 | $this->_pid = Phantomjs::start('phantom.js', $this->port(), 'phantomjs-connection.js', $log_file); |
|
124 | |||
125 | 2 | if ($this->_pid_file) |
|
126 | 2 | { |
|
127 | 1 | file_put_contents($this->_pid_file, $this->_pid); |
|
128 | 1 | } |
|
129 | |||
130 | 2 | $self = $this; |
|
131 | |||
132 | return Attempt::make(function() use ($self) { |
||
133 | 2 | return $self->is_running(); |
|
134 | 2 | }); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Check if the phantomjs server has been started |
||
139 | * @return boolean |
||
140 | */ |
||
141 | 2 | public function is_started() |
|
145 | |||
146 | /** |
||
147 | * Check if the phantomjs server is actually running (the port is taken) |
||
148 | * @return boolean |
||
149 | */ |
||
150 | 2 | public function is_running() |
|
154 | |||
155 | /** |
||
156 | * Gracefully stop the phantomjs server. Return FALSE on failure. Clear the pid_file if set |
||
157 | * @return boolean |
||
158 | */ |
||
159 | 2 | public function stop() |
|
160 | { |
||
161 | 2 | if ($this->is_started()) |
|
162 | 2 | { |
|
163 | 2 | $this->delete('session'); |
|
164 | 2 | $this->_pid = NULL; |
|
165 | 2 | } |
|
166 | |||
167 | 2 | if ($this->_pid_file AND is_file($this->_pid_file)) |
|
168 | 2 | { |
|
169 | 1 | unlink($this->_pid_file); |
|
170 | 1 | } |
|
171 | 2 | $self = $this; |
|
172 | |||
173 | return Attempt::make(function() use ($self) { |
||
174 | 2 | return ! $self->is_running(); |
|
175 | 2 | }); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Getter - get the current pid_file |
||
180 | * @return string |
||
181 | */ |
||
182 | 1 | public function pid_file() |
|
186 | |||
187 | /** |
||
188 | * Perform a get request on the phantomjs server |
||
189 | * @param string $command |
||
190 | * @return mixed |
||
191 | */ |
||
192 | 10 | public function get($command) |
|
196 | |||
197 | /** |
||
198 | * Perform a post request on the phantomjs server |
||
199 | * @param string $command |
||
200 | * @param array $params |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 10 | public function post($command, array $params) |
|
211 | |||
212 | /** |
||
213 | * Perform a delete request on the phantomjs server |
||
214 | * @param string $command |
||
215 | * @return mixed |
||
216 | */ |
||
217 | 3 | public function delete($command) |
|
224 | |||
225 | /** |
||
226 | * Get the full url of a command (including server and port) |
||
227 | * @param string $command |
||
228 | * @return string |
||
229 | */ |
||
230 | 15 | public function command_url($command) |
|
234 | |||
235 | /** |
||
236 | * Perform a custom request on the phantomjs server, using curl |
||
237 | * @param string $command |
||
238 | * @param array $options |
||
239 | * @return mixed |
||
240 | */ |
||
241 | 14 | protected function call($command, array $options = array()) |
|
272 | } |
||
273 |