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