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\Procedure\ProcedureLoaderInterface; |
12
|
|
|
use JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface; |
13
|
|
|
use JonnyW\PhantomJs\Http\MessageFactoryInterface; |
14
|
|
|
use JonnyW\PhantomJs\Http\RequestInterface; |
15
|
|
|
use JonnyW\PhantomJs\Http\ResponseInterface; |
16
|
|
|
use JonnyW\PhantomJs\DependencyInjection\ServiceContainer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* PHP PhantomJs |
20
|
|
|
* |
21
|
|
|
* @author Jon Wenmoth <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Client implements ClientInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Client. |
27
|
|
|
* |
28
|
|
|
* @var \JonnyW\PhantomJs\ClientInterface |
29
|
|
|
* @access private |
30
|
|
|
*/ |
31
|
|
|
private static $instance; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* PhantomJs engine. |
35
|
|
|
* |
36
|
|
|
* @var \JonnyW\PhantomJs\Engine |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $engine; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Procedure loader. |
43
|
|
|
* |
44
|
|
|
* @var \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $procedureLoader; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Procedure validator. |
51
|
|
|
* |
52
|
|
|
* @var \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface |
53
|
|
|
* @access protected |
54
|
|
|
*/ |
55
|
|
|
protected $procedureCompiler; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Message factory. |
59
|
|
|
* |
60
|
|
|
* @var \JonnyW\PhantomJs\Http\MessageFactoryInterface |
61
|
|
|
* @access protected |
62
|
|
|
*/ |
63
|
|
|
protected $messageFactory; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Procedure template |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
* @access protected |
70
|
|
|
*/ |
71
|
|
|
protected $procedure; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Internal constructor |
75
|
|
|
* |
76
|
|
|
* @access public |
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 |
81
|
|
|
* @return void |
|
|
|
|
82
|
|
|
*/ |
83
|
35 |
|
public function __construct(Engine $engine, ProcedureLoaderInterface $procedureLoader, ProcedureCompilerInterface $procedureCompiler, MessageFactoryInterface $messageFactory) |
84
|
|
|
{ |
85
|
35 |
|
$this->engine = $engine; |
86
|
35 |
|
$this->procedureLoader = $procedureLoader; |
87
|
35 |
|
$this->procedureCompiler = $procedureCompiler; |
88
|
35 |
|
$this->messageFactory = $messageFactory; |
89
|
35 |
|
$this->procedure = 'http_default'; |
90
|
35 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get singleton instance |
94
|
|
|
* |
95
|
|
|
* @access public |
96
|
|
|
* @return \JonnyW\PhantomJs\Client |
97
|
|
|
*/ |
98
|
6 |
|
public static function getInstance() |
99
|
|
|
{ |
100
|
6 |
|
if (!self::$instance instanceof ClientInterface) { |
101
|
|
|
|
102
|
1 |
|
$serviceContainer = ServiceContainer::getInstance(); |
103
|
|
|
|
104
|
1 |
|
self::$instance = new static( |
105
|
1 |
|
$serviceContainer->get('engine'), |
|
|
|
|
106
|
1 |
|
$serviceContainer->get('procedure_loader'), |
|
|
|
|
107
|
1 |
|
$serviceContainer->get('procedure_compiler'), |
|
|
|
|
108
|
1 |
|
$serviceContainer->get('message_factory') |
|
|
|
|
109
|
1 |
|
); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
6 |
|
return self::$instance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get PhantomJs engine. |
117
|
|
|
* |
118
|
|
|
* @access public |
119
|
|
|
* @return \JonnyW\PhantomJs\Engine |
120
|
|
|
*/ |
121
|
10 |
|
public function getEngine() |
122
|
|
|
{ |
123
|
10 |
|
return $this->engine; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get message factory instance |
128
|
|
|
* |
129
|
|
|
* @access public |
130
|
|
|
* @return \JonnyW\PhantomJs\Http\MessageFactoryInterface |
131
|
|
|
*/ |
132
|
32 |
|
public function getMessageFactory() |
133
|
|
|
{ |
134
|
32 |
|
return $this->messageFactory; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get procedure loader instance |
139
|
|
|
* |
140
|
|
|
* @access public |
141
|
|
|
* @return \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface |
142
|
|
|
*/ |
143
|
9 |
|
public function getProcedureLoader() |
144
|
|
|
{ |
145
|
9 |
|
return $this->procedureLoader; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Send request |
150
|
|
|
* |
151
|
|
|
* @access public |
152
|
|
|
* @param \JonnyW\PhantomJs\Http\RequestInterface $request |
153
|
|
|
* @param \JonnyW\PhantomJs\Http\ResponseInterface $response |
154
|
|
|
* @return \JonnyW\PhantomJs\Http\ResponseInterface |
155
|
|
|
*/ |
156
|
31 |
|
public function send(RequestInterface $request, ResponseInterface $response) |
157
|
|
|
{ |
158
|
31 |
|
$procedure = $this->procedureLoader->load($this->procedure); |
159
|
|
|
|
160
|
31 |
|
$this->procedureCompiler->compile($procedure, $request); |
|
|
|
|
161
|
|
|
|
162
|
30 |
|
$procedure->run($request, $response); |
|
|
|
|
163
|
|
|
|
164
|
30 |
|
return $response; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get log. |
169
|
|
|
* |
170
|
|
|
* @access public |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
9 |
|
public function getLog() |
174
|
|
|
{ |
175
|
9 |
|
return $this->getEngine()->getLog(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Set procedure template. |
180
|
|
|
* |
181
|
|
|
* @access public |
182
|
|
|
* @param string $procedure |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
3 |
|
public function setProcedure($procedure) |
186
|
|
|
{ |
187
|
3 |
|
$this->procedure = $procedure; |
188
|
3 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get procedure template. |
192
|
|
|
* |
193
|
|
|
* @access public |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getProcedure() |
197
|
|
|
{ |
198
|
|
|
return $this->procedure; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get procedure compiler. |
203
|
|
|
* |
204
|
|
|
* @access public |
205
|
|
|
* @return \JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface |
206
|
|
|
*/ |
207
|
|
|
public function getProcedureCompiler() |
208
|
|
|
{ |
209
|
|
|
return $this->procedureCompiler; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set lazy request flag. |
214
|
|
|
* |
215
|
|
|
* @access public |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
2 |
|
public function isLazy() |
219
|
|
|
{ |
220
|
2 |
|
$this->procedure = 'http_lazy'; |
221
|
2 |
|
} |
222
|
|
|
} |
223
|
|
|
|
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.