1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Net\Bazzline\Component\Curl\Request; |
4
|
|
|
|
5
|
|
|
use Net\Bazzline\Component\Curl\Dispatcher\Dispatcher; |
6
|
|
|
use Net\Bazzline\Component\Curl\Dispatcher\DispatcherInterface; |
7
|
|
|
use Net\Bazzline\Component\Curl\FactoryInterface; |
8
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsFormUtf8; |
9
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface; |
10
|
|
|
use Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeOutInSeconds; |
11
|
|
|
use Net\Bazzline\Component\Curl\Option\OptionInterface; |
12
|
|
|
use Net\Bazzline\Component\Toolbox\HashMap\Merge; |
13
|
|
|
|
14
|
|
|
class RequestFactory implements FactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** @var DispatcherInterface */ |
17
|
|
|
private $dispatcher; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return mixed|Request |
21
|
|
|
*/ |
22
|
|
|
public function create() |
23
|
|
|
{ |
24
|
|
|
$defaultHeaderLines = $this->createDefaultHeaderLines( |
25
|
|
|
$this->getDefaultRawHeaderLine(), |
26
|
|
|
$this->getDefaultHeaderLines() |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$defaultOptions = $this->createDefaultOptions( |
30
|
|
|
$this->getDefaultRawOptions(), |
31
|
|
|
$this->getDefaultOptions() |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$request = new Request( |
35
|
|
|
$this->getDispatcher(), |
36
|
|
|
new Merge(), |
37
|
|
|
$defaultHeaderLines, |
38
|
|
|
$defaultOptions |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
return $request; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param DispatcherInterface $dispatcher |
46
|
|
|
*/ |
47
|
|
|
public function overwriteDispatcher(DispatcherInterface $dispatcher) |
48
|
|
|
{ |
49
|
|
|
$this->dispatcher = $dispatcher; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return DispatcherInterface |
54
|
|
|
*/ |
55
|
|
|
protected function getDispatcher() |
56
|
|
|
{ |
57
|
|
|
$createDispatcher = !($this->dispatcher instanceof DispatcherInterface); |
58
|
|
|
|
59
|
|
|
if ($createDispatcher) { |
60
|
|
|
$this->dispatcher = $this->getNewDispatcher(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->dispatcher; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return DispatcherInterface |
68
|
|
|
*/ |
69
|
|
|
protected function getNewDispatcher() |
70
|
|
|
{ |
71
|
|
|
return new Dispatcher(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array|HeaderLineInterface[] |
76
|
|
|
*/ |
77
|
|
|
protected function getDefaultHeaderLines() |
78
|
|
|
{ |
79
|
|
|
return array( |
80
|
|
|
new ContentTypeIsFormUtf8() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getDefaultRawHeaderLine() |
88
|
|
|
{ |
89
|
|
|
return array(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array|OptionInterface[] |
94
|
|
|
*/ |
95
|
|
|
protected function getDefaultOptions() |
96
|
|
|
{ |
97
|
|
|
return array( |
98
|
|
|
new SetTimeOutInSeconds(10) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected function getDefaultRawOptions() |
106
|
|
|
{ |
107
|
|
|
return array( |
108
|
|
|
CURLOPT_AUTOREFERER => true, |
109
|
|
|
CURLOPT_CONNECTTIMEOUT => 5, |
110
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
111
|
|
|
CURLOPT_MAXREDIRS => 10, |
112
|
|
|
CURLOPT_USERAGENT => 'net/bazzline curl component for php' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $rawHeaderLines |
118
|
|
|
* @param array|HeaderLineInterface[] $headerLines |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private function createDefaultHeaderLines(array $rawHeaderLines, array $headerLines) |
122
|
|
|
{ |
123
|
|
|
$defaultHeaderLines = $rawHeaderLines; |
124
|
|
|
|
125
|
|
|
foreach ($headerLines as $headerLine) { |
126
|
|
|
$defaultHeaderLines[] = $headerLine->line(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $defaultHeaderLines; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $rawOptions |
134
|
|
|
* @param array|OptionInterface[] $options |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
private function createDefaultOptions(array $rawOptions, array $options) |
138
|
|
|
{ |
139
|
|
|
$defaultOptions = $rawOptions; |
140
|
|
|
|
141
|
|
|
foreach ($options as $option) { |
142
|
|
|
$defaultOptions[$option->identifier()] = $option->value(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $defaultOptions; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|