8
8
*/
9
9
namespace JonnyW \PhantomJs ;
10
10
11
- use JonnyW \PhantomJs \Exception \InvalidExecutableException ;
12
11
use JonnyW \PhantomJs \Procedure \ProcedureLoaderInterface ;
13
- use JonnyW \PhantomJs \Procedure \ProcedureValidatorInterface ;
12
+ use JonnyW \PhantomJs \Procedure \ProcedureCompilerInterface ;
14
13
use JonnyW \PhantomJs \Http \MessageFactoryInterface ;
15
14
use JonnyW \PhantomJs \Http \RequestInterface ;
16
15
use JonnyW \PhantomJs \Http \ResponseInterface ;
@@ -31,6 +30,14 @@ class Client implements ClientInterface
31
30
*/
32
31
private static $ instance ;
33
32
33
+ /**
34
+ * PhantomJs engine.
35
+ *
36
+ * @var \JonnyW\PhantomJs\Engine
37
+ * @access protected
38
+ */
39
+ protected $ engine ;
40
+
34
41
/**
35
42
* Procedure loader.
36
43
*
@@ -42,10 +49,10 @@ class Client implements ClientInterface
42
49
/**
43
50
* Procedure validator.
44
51
*
45
- * @var \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface
52
+ * @var \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface
46
53
* @access protected
47
54
*/
48
- protected $ procedureValidator ;
55
+ protected $ procedureCompiler ;
49
56
50
57
/**
51
58
* Message factory.
@@ -56,53 +63,30 @@ class Client implements ClientInterface
56
63
protected $ messageFactory ;
57
64
58
65
/**
59
- * Path to PhantomJs executable
66
+ * Procedure template
60
67
*
61
68
* @var string
62
69
* @access protected
63
70
*/
64
- protected $ phantomJs ;
65
-
66
- /**
67
- * Debug flag.
68
- *
69
- * @var boolean
70
- * @access protected
71
- */
72
- protected $ debug ;
73
-
74
- /**
75
- * PhantomJs run options.
76
- *
77
- * @var array
78
- * @access protected
79
- */
80
- protected $ options ;
81
-
82
- /**
83
- * Log info
84
- *
85
- * @var string
86
- * @access protected
87
- */
88
- protected $ log ;
71
+ protected $ procedure ;
89
72
90
73
/**
91
74
* Internal constructor
92
75
*
93
76
* @access public
94
- * @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader
95
- * @param \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface $procedureValidator
96
- * @param \JonnyW\PhantomJs\Http\MessageFactoryInterface $messageFactory
77
+ * @param \JonnyW\PhantomJs\Engine $engine
78
+ * @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader
79
+ * @param \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface $procedureCompiler
80
+ * @param \JonnyW\PhantomJs\Http\MessageFactoryInterface $messageFactory
97
81
* @return void
98
82
*/
99
- public function __construct (ProcedureLoaderInterface $ procedureLoader , ProcedureValidatorInterface $ procedureValidator , MessageFactoryInterface $ messageFactory )
83
+ public function __construct (Engine $ engine , ProcedureLoaderInterface $ procedureLoader , ProcedureCompilerInterface $ procedureCompiler , MessageFactoryInterface $ messageFactory )
100
84
{
101
- $ this ->procedureLoader = $ procedureLoader ;
102
- $ this ->procedureValidator = $ procedureValidator ;
103
- $ this ->messageFactory = $ messageFactory ;
104
- $ this ->phantomJs = ' bin/phantomjs ' ;
105
- $ this ->options = array () ;
85
+ $ this ->engine = $ engine ;
86
+ $ this ->procedureLoader = $ procedureLoader ;
87
+ $ this ->procedureCompiler = $ procedureCompiler ;
88
+ $ this ->messageFactory = $ messageFactory ;
89
+ $ this ->procedure = ' http_default ' ;
106
90
}
107
91
108
92
/**
@@ -118,15 +102,27 @@ public static function getInstance()
118
102
$ serviceContainer = ServiceContainer::getInstance ();
119
103
120
104
self ::$ instance = new static (
105
+ $ serviceContainer ->get ('engine ' ),
121
106
$ serviceContainer ->get ('procedure_loader ' ),
122
- $ serviceContainer ->get ('procedure_validator ' ),
107
+ $ serviceContainer ->get ('procedure_compiler ' ),
123
108
$ serviceContainer ->get ('message_factory ' )
124
109
);
125
110
}
126
111
127
112
return self ::$ instance ;
128
113
}
129
114
115
+ /**
116
+ * Get PhantomJs engine.
117
+ *
118
+ * @access public
119
+ * @return \JonnyW\PhantomJs\Engine
120
+ */
121
+ public function getEngine ()
122
+ {
123
+ return $ this ->engine ;
124
+ }
125
+
130
126
/**
131
127
* Get message factory instance
132
128
*
@@ -159,174 +155,46 @@ public function getProcedureLoader()
159
155
*/
160
156
public function send (RequestInterface $ request , ResponseInterface $ response )
161
157
{
162
- $ procedure = $ this ->procedureLoader ->load ($ request -> getType () );
158
+ $ procedure = $ this ->procedureLoader ->load ($ this -> procedure );
163
159
164
- $ this ->procedureValidator ->validate (
165
- $ this ,
166
- $ procedure ,
167
- $ request
168
- );
160
+ $ this ->procedureCompiler ->compile ($ procedure , $ request );
169
161
170
- $ procedure ->run ($ this , $ request , $ response );
162
+ $ procedure ->run ($ request , $ response );
171
163
172
164
return $ response ;
173
165
}
174
166
175
167
/**
176
- * Get PhantomJs run command with
177
- * loader and run options.
168
+ * Get log.
178
169
*
179
170
* @access public
180
171
* @return string
181
172
*/
182
- public function getCommand ()
183
- {
184
- $ phantomJs = $ this ->getPhantomJs ();
185
- $ options = $ this ->getOptions ();
186
-
187
- $ this ->validateExecutable ($ phantomJs );
188
-
189
- if ($ this ->debug ) {
190
- array_push ($ options , '--debug=true ' );
191
- }
192
-
193
- return sprintf ('%s %s ' , $ phantomJs , implode (' ' , $ options ));
194
- }
195
-
196
- /**
197
- * Set new PhantomJs executable path.
198
- *
199
- * @access public
200
- * @param string $path
201
- * @return \JonnyW\PhantomJs\Client
202
- */
203
- public function setPhantomJs ($ path )
204
- {
205
- $ this ->validateExecutable ($ path );
206
-
207
- $ this ->phantomJs = $ path ;
208
-
209
- return $ this ;
210
- }
211
-
212
- /**
213
- * Get PhantomJs executable path.
214
- *
215
- * @access public
216
- * @return string
217
- */
218
- public function getPhantomJs ()
219
- {
220
- return $ this ->phantomJs ;
221
- }
222
-
223
- /**
224
- * Set PhantomJs run options.
225
- *
226
- * @access public
227
- * @param array $options
228
- * @return \JonnyW\PhantomJs\Client
229
- */
230
- public function setOptions (array $ options )
231
- {
232
- $ this ->options = $ options ;
233
-
234
- return $ this ;
235
- }
236
-
237
- /**
238
- * Get PhantomJs run options.
239
- *
240
- * @access public
241
- * @return array
242
- */
243
- public function getOptions ()
244
- {
245
- return (array ) $ this ->options ;
246
- }
247
-
248
- /**
249
- * Add single PhantomJs run option.
250
- *
251
- * @access public
252
- * @param string $option
253
- * @return \JonnyW\PhantomJs\Client
254
- */
255
- public function addOption ($ option )
256
- {
257
- if (!in_array ($ option , $ this ->options )) {
258
- $ this ->options [] = $ option ;
259
- }
260
-
261
- return $ this ;
262
- }
263
-
264
- /**
265
- * Debug.
266
- *
267
- * @access public
268
- * @param boolean $doDebug
269
- * @return \JonnyW\PhantomJs\Client
270
- */
271
- public function debug ($ doDebug )
173
+ public function getLog ()
272
174
{
273
- $ this ->debug = $ doDebug ;
274
-
275
- return $ this ;
175
+ return $ this ->getEngine ()->getLog ();
276
176
}
277
177
278
178
/**
279
- * Log info .
179
+ * Set procedure template .
280
180
*
281
181
* @access public
282
- * @param string $info
283
- * @return \JonnyW\PhantomJs\Client
182
+ * @param string $procedure
183
+ * @return void
284
184
*/
285
- public function log ( $ info )
185
+ public function setProcedure ( $ procedure )
286
186
{
287
- $ this ->log = $ info ;
288
-
289
- return $ this ;
187
+ $ this ->procedure = $ procedure ;
290
188
}
291
189
292
190
/**
293
- * Get log info .
191
+ * Get procedure template .
294
192
*
295
193
* @access public
296
194
* @return string
297
195
*/
298
- public function getLog ()
196
+ public function getProcedure ()
299
197
{
300
- return $ this ->log ;
301
- }
302
-
303
- /**
304
- * Clear log info.
305
- *
306
- * @access public
307
- * @return \JonnyW\PhantomJs\Client
308
- */
309
- public function clearLog ()
310
- {
311
- $ this ->log = '' ;
312
-
313
- return $ this ;
314
- }
315
-
316
- /**
317
- * Validate execuable file.
318
- *
319
- * @access private
320
- * @param string $file
321
- * @return boolean
322
- * @throws \JonnyW\PhantomJs\Exception\InvalidExecutableException
323
- */
324
- private function validateExecutable ($ file )
325
- {
326
- if (!file_exists ($ file ) || !is_executable ($ file )) {
327
- throw new InvalidExecutableException (sprintf ('File does not exist or is not executable: %s ' , $ file ));
328
- }
329
-
330
- return true ;
198
+ return $ this ->procedure ;
331
199
}
332
200
}
0 commit comments