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
|
1 |
|
public function __construct() |
59
|
|
|
{ |
60
|
1 |
|
$this->path = 'bin/phantomjs'; |
61
|
1 |
|
$this->options = array(); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get PhantomJs run command with |
66
|
|
|
* loader run options. |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
27 |
|
public function getCommand() |
72
|
|
|
{ |
73
|
27 |
|
$path = $this->getPath(); |
74
|
27 |
|
$options = $this->getOptions(); |
75
|
|
|
|
76
|
27 |
|
$this->validateExecutable($path); |
77
|
|
|
|
78
|
27 |
|
if ($this->debug) { |
79
|
|
|
array_push($options, '--debug=true'); |
80
|
|
|
} |
81
|
|
|
|
82
|
27 |
|
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
|
|
|
public function setPath($path) |
93
|
|
|
{ |
94
|
|
|
$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
|
27 |
|
public function getPath() |
108
|
|
|
{ |
109
|
27 |
|
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
|
|
|
public function setOptions(array $options) |
120
|
|
|
{ |
121
|
|
|
$this->options = $options; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get PhantomJs run options. |
128
|
|
|
* |
129
|
|
|
* @access public |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
27 |
|
public function getOptions() |
133
|
|
|
{ |
134
|
27 |
|
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
|
|
|
public function addOption($option) |
145
|
|
|
{ |
146
|
|
|
if (!in_array($option, $this->options)) { |
147
|
|
|
$this->options[] = $option; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Debug. |
155
|
|
|
* |
156
|
|
|
* @access public |
157
|
|
|
* @param boolean $doDebug |
158
|
|
|
* @return \JonnyW\PhantomJs\Client |
159
|
|
|
*/ |
160
|
|
|
public function debug($doDebug) |
161
|
|
|
{ |
162
|
|
|
$this->debug = $doDebug; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Log info. |
169
|
|
|
* |
170
|
|
|
* @access public |
171
|
|
|
* @param string $info |
172
|
|
|
* @return \JonnyW\PhantomJs\Client |
173
|
|
|
*/ |
174
|
27 |
|
public function log($info) |
175
|
|
|
{ |
176
|
27 |
|
$this->log = $info; |
177
|
|
|
|
178
|
27 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get log info. |
183
|
|
|
* |
184
|
|
|
* @access public |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
8 |
|
public function getLog() |
188
|
|
|
{ |
189
|
8 |
|
return $this->log; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Clear log info. |
194
|
|
|
* |
195
|
|
|
* @access public |
196
|
|
|
* @return \JonnyW\PhantomJs\Client |
197
|
|
|
*/ |
198
|
|
|
public function clearLog() |
199
|
|
|
{ |
200
|
|
|
$this->log = ''; |
201
|
|
|
|
202
|
|
|
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
|
27 |
|
private function validateExecutable($file) |
214
|
|
|
{ |
215
|
27 |
|
if (!file_exists($file) || !is_executable($file)) { |
216
|
|
|
throw new InvalidExecutableException(sprintf('File does not exist or is not executable: %s', $file)); |
217
|
|
|
} |
218
|
|
|
|
219
|
27 |
|
return true; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
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.