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