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; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Exception\InvalidExecutableException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHP PhantomJs |
15
|
|
|
* |
16
|
|
|
* @author Jon Wenmoth <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Engine |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Executable path. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
* @access protected |
25
|
|
|
*/ |
26
|
|
|
protected $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Debug flag. |
30
|
|
|
* |
31
|
|
|
* @var boolean |
32
|
|
|
* @access protected |
33
|
|
|
*/ |
34
|
|
|
protected $debug; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Cache flag. |
38
|
|
|
* |
39
|
|
|
* @var boolean |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $cache; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* PhantomJs run options. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $options; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Log info |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected $log; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Internal constructor |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
* @return void |
|
|
|
|
65
|
|
|
*/ |
66
|
1 |
|
public function __construct() |
67
|
|
|
{ |
68
|
1 |
|
$this->path = 'bin/phantomjs'; |
69
|
1 |
|
$this->options = array(); |
70
|
|
|
|
71
|
1 |
|
$this->debug = false; |
72
|
1 |
|
$this->cache = true; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get PhantomJs run command with |
77
|
|
|
* loader run options. |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function getCommand() |
83
|
|
|
{ |
84
|
1 |
|
$path = $this->getPath(); |
85
|
1 |
|
$options = $this->getOptions(); |
86
|
|
|
|
87
|
1 |
|
$this->validateExecutable($path); |
88
|
|
|
|
89
|
|
|
if ($this->cache) { |
90
|
|
|
array_push($options, '--disk-cache=true'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($this->debug) { |
94
|
|
|
array_push($options, '--debug=true'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return sprintf('%s %s', $path, implode(' ', $options)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set path. |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* @param string $path |
105
|
|
|
* @return \JonnyW\PhantomJs\Client |
106
|
|
|
*/ |
107
|
|
|
public function setPath($path) |
108
|
|
|
{ |
109
|
|
|
$this->validateExecutable($path); |
110
|
|
|
|
111
|
|
|
$this->path = $path; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get path. |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
public function getPath() |
123
|
|
|
{ |
124
|
1 |
|
return $this->path; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set PhantomJs run options. |
129
|
|
|
* |
130
|
|
|
* @access public |
131
|
|
|
* @param array $options |
132
|
|
|
* @return \JonnyW\PhantomJs\Client |
133
|
|
|
*/ |
134
|
|
|
public function setOptions(array $options) |
135
|
|
|
{ |
136
|
|
|
$this->options = $options; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get PhantomJs run options. |
143
|
|
|
* |
144
|
|
|
* @access public |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
1 |
|
public function getOptions() |
148
|
|
|
{ |
149
|
1 |
|
return (array) $this->options; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add single PhantomJs run option. |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
* @param string $option |
157
|
|
|
* @return \JonnyW\PhantomJs\Client |
158
|
|
|
*/ |
159
|
|
|
public function addOption($option) |
160
|
|
|
{ |
161
|
|
|
if (!in_array($option, $this->options)) { |
162
|
|
|
$this->options[] = $option; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Debug. |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* @param boolean $doDebug |
173
|
|
|
* @return \JonnyW\PhantomJs\Client |
174
|
|
|
*/ |
175
|
|
|
public function debug($doDebug) |
176
|
|
|
{ |
177
|
|
|
$this->debug = $doDebug; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Cache. |
184
|
|
|
* |
185
|
|
|
* @access public |
186
|
|
|
* @param boolean $doCache |
187
|
|
|
* @return \JonnyW\PhantomJs\Client |
188
|
|
|
*/ |
189
|
|
|
public function cache($doCache) |
190
|
|
|
{ |
191
|
|
|
$this->cache = $doCache; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Log info. |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
* @param string $info |
201
|
|
|
* @return \JonnyW\PhantomJs\Client |
202
|
|
|
*/ |
203
|
|
|
public function log($info) |
204
|
|
|
{ |
205
|
|
|
$this->log = $info; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get log info. |
212
|
|
|
* |
213
|
|
|
* @access public |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function getLog() |
217
|
|
|
{ |
218
|
|
|
return $this->log; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Clear log info. |
223
|
|
|
* |
224
|
|
|
* @access public |
225
|
|
|
* @return \JonnyW\PhantomJs\Client |
226
|
|
|
*/ |
227
|
|
|
public function clearLog() |
228
|
|
|
{ |
229
|
|
|
$this->log = ''; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Validate execuable file. |
236
|
|
|
* |
237
|
|
|
* @access private |
238
|
|
|
* @param string $file |
239
|
|
|
* @return boolean |
240
|
|
|
* @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException |
241
|
|
|
*/ |
242
|
1 |
|
private function validateExecutable($file) |
243
|
|
|
{ |
244
|
1 |
|
if (!file_exists($file) || !is_executable($file)) { |
245
|
1 |
|
throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return true; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.