1: <?php
2:
3: namespace Guzzle\Http\Message;
4:
5: use Guzzle\Common\Collection;
6: use Guzzle\Http\EntityBody;
7: use Guzzle\Http\Url;
8: use Guzzle\Parser\ParserRegistry;
9:
10: 11: 12:
13: class RequestFactory implements RequestFactoryInterface
14: {
15: 16: 17:
18: protected static $instance;
19:
20: 21: 22:
23: protected $requestClass = 'Guzzle\\Http\\Message\\Request';
24:
25: 26: 27:
28: protected $entityEnclosingRequestClass = 'Guzzle\\Http\\Message\\EntityEnclosingRequest';
29:
30: 31: 32: 33: 34:
35: public static function getInstance()
36: {
37:
38: if (!static::$instance) {
39: static::$instance = new static();
40: }
41:
42:
43: return static::$instance;
44: }
45:
46: 47: 48:
49: public function fromMessage($message)
50: {
51: $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($message);
52:
53: if (!$parsed) {
54: return false;
55: }
56:
57: $request = $this->fromParts($parsed['method'], $parsed['request_url'],
58: $parsed['headers'], $parsed['body'], $parsed['protocol'],
59: $parsed['version']);
60:
61:
62:
63:
64: if (!isset($parsed['headers']['Expect']) && !isset($parsed['headers']['expect'])) {
65: $request->removeHeader('Expect');
66: }
67:
68: return $request;
69: }
70:
71: 72: 73:
74: public function fromParts(
75: $method,
76: array $urlParts,
77: $headers = null,
78: $body = null,
79: $protocol = 'HTTP',
80: $protocolVersion = '1.1'
81: ) {
82: return $this->create($method, Url::buildUrl($urlParts, true), $headers, $body)
83: ->setProtocolVersion($protocolVersion);
84: }
85:
86: 87: 88:
89: public function create($method, $url, $headers = null, $body = null)
90: {
91: $method = strtoupper($method);
92:
93: if ($method == 'GET' || $method == 'HEAD' || $method == 'TRACE' || $method == 'OPTIONS') {
94:
95: $request = new $this->requestClass($method, $url, $headers);
96: if ($body) {
97:
98: $type = gettype($body);
99: if ($type == 'string' || $type == 'resource' || $type == 'object') {
100: $request->setResponseBody($body);
101: }
102: }
103: return $request;
104: }
105:
106:
107: $request = new $this->entityEnclosingRequestClass($method, $url, $headers);
108:
109: if ($body) {
110:
111: if (is_array($body) || $body instanceof Collection) {
112:
113: foreach ($body as $key => $value) {
114: if (is_string($value) && substr($value, 0, 1) == '@') {
115: $request->addPostFile($key, $value);
116: unset($body[$key]);
117: }
118: }
119:
120: $request->addPostFields($body);
121: } else {
122:
123: $request->setBody(
124: $body,
125: (string) $request->getHeader('Content-Type'),
126: (string) $request->getHeader('Transfer-Encoding') == 'chunked'
127: );
128: }
129: }
130:
131: return $request;
132: }
133:
134: 135: 136: 137: 138: 139: 140: 141: 142:
143: public function cloneRequestWithMethod(RequestInterface $request, $method)
144: {
145:
146: if ($client = $request->getClient()) {
147: $cloned = $request->getClient()->createRequest($method, $request->getUrl(), $request->getHeaders());
148: } else {
149: $cloned = $this->create($method, $request->getUrl(), $request->getHeaders());
150: }
151:
152: $cloned->getCurlOptions()->replace($request->getCurlOptions()->getAll());
153: $cloned->setEventDispatcher(clone $request->getEventDispatcher());
154:
155: if (!($cloned instanceof EntityEnclosingRequestInterface)) {
156: $cloned->removeHeader('Content-Length');
157: } elseif ($request instanceof EntityEnclosingRequestInterface) {
158: $cloned->setBody($request->getBody());
159: }
160: $cloned->getParams()
161: ->replace($request->getParams()->getAll())
162: ->remove('curl_handle')
163: ->remove('curl_multi');
164:
165: return $cloned;
166: }
167: }
168: