Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
RingBridge.php
001 <?php
002 namespace GuzzleHttp;
003
004 use GuzzleHttp\Message\MessageFactoryInterface;
005 use GuzzleHttp\Message\RequestInterface;
006 use GuzzleHttp\Event\ProgressEvent;
007 use GuzzleHttp\Message\Request;
008 use GuzzleHttp\Ring\Core;
009 use GuzzleHttp\Stream\Stream;
010 use GuzzleHttp\Exception\RequestException;
011
012 /**
013 * Provides the bridge between Guzzle requests and responses and Guzzle Ring.
014 */
015 class RingBridge
016 {
017 /**
018 * Creates a Ring request from a request object.
019 *
020 * This function does not hook up the "then" and "progress" events that
021 * would be required for actually sending a Guzzle request through a
022 * RingPHP handler.
023 *
024 * @param RequestInterface $request Request to convert.
025 *
026 * @return array Converted Guzzle Ring request.
027 */
028 public static function createRingRequest(RequestInterface $request)
029 {
030 $options = $request->getConfig()->toArray();
031 $url = $request->getUrl();
032 // No need to calculate the query string twice (in URL and query).
033 $qs = ($pos = strpos($url, '?')) ? substr($url, $pos + 1) : null;
034
035 return [
036 'scheme' => $request->getScheme(),
037 'http_method' => $request->getMethod(),
038 'url' => $url,
039 'uri' => $request->getPath(),
040 'headers' => $request->getHeaders(),
041 'body' => $request->getBody(),
042 'version' => $request->getProtocolVersion(),
043 'client' => $options,
044 'query_string' => $qs,
045 'future' => isset($options['future']) ? $options['future'] : false
046 ];
047 }
048
049 /**
050 * Creates a Ring request from a request object AND prepares the callbacks.
051 *
052 * @param Transaction $trans Transaction to update.
053 *
054 * @return array Converted Guzzle Ring request.
055 */
056 public static function prepareRingRequest(Transaction $trans)
057 {
058 // Clear out the transaction state when initiating.
059 $trans->exception = null;
060 $request = self::createRingRequest($trans->request);
061
062 // Emit progress events if any progress listeners are registered.
063 if ($trans->request->getEmitter()->hasListeners('progress')) {
064 $emitter = $trans->request->getEmitter();
065 $request['client']['progress'] = function ($a, $b, $c, $d) use ($trans, $emitter) {
066 $emitter->emit('progress', new ProgressEvent($trans, $a, $b, $c, $d));
067 };
068 }
069
070 return $request;
071 }
072
073 /**
074 * Handles the process of processing a response received from a ring
075 * handler. The created response is added to the transaction, and the
076 * transaction stat is set appropriately.
077 *
078 * @param Transaction $trans Owns request and response.
079 * @param array $response Ring response array
080 * @param MessageFactoryInterface $messageFactory Creates response objects.
081 */
082 public static function completeRingResponse(
083 Transaction $trans,
084 array $response,
085 MessageFactoryInterface $messageFactory
086 ) {
087 $trans->state = 'complete';
088 $trans->transferInfo = isset($response['transfer_stats'])
089 ? $response['transfer_stats'] : [];
090
091 if (!empty($response['status'])) {
092 $options = [];
093 if (isset($response['version'])) {
094 $options['protocol_version'] = $response['version'];
095 }
096 if (isset($response['reason'])) {
097 $options['reason_phrase'] = $response['reason'];
098 }
099 $trans->response = $messageFactory->createResponse(
100 $response['status'],
101 isset($response['headers']) ? $response['headers'] : [],
102 isset($response['body']) ? $response['body'] : null,
103 $options
104 );
105 if (isset($response['effective_url'])) {
106 $trans->response->setEffectiveUrl($response['effective_url']);
107 }
108 } elseif (empty($response['error'])) {
109 // When nothing was returned, then we need to add an error.
110 $response['error'] = self::getNoRingResponseException($trans->request);
111 }
112
113 if (isset($response['error'])) {
114 $trans->state = 'error';
115 $trans->exception = $response['error'];
116 }
117 }
118
119 /**
120 * Creates a Guzzle request object using a ring request array.
121 *
122 * @param array $request Ring request
123 *
124 * @return Request
125 * @throws \InvalidArgumentException for incomplete requests.
126 */
127 public static function fromRingRequest(array $request)
128 {
129 $options = [];
130 if (isset($request['version'])) {
131 $options['protocol_version'] = $request['version'];
132 }
133
134 if (!isset($request['http_method'])) {
135 throw new \InvalidArgumentException('No http_method');
136 }
137
138 return new Request(
139 $request['http_method'],
140 Core::url($request),
141 isset($request['headers']) ? $request['headers'] : [],
142 isset($request['body']) ? Stream::factory($request['body']) : null,
143 $options
144 );
145 }
146
147 /**
148 * Get an exception that can be used when a RingPHP handler does not
149 * populate a response.
150 *
151 * @param RequestInterface $request
152 *
153 * @return RequestException
154 */
155 public static function getNoRingResponseException(RequestInterface $request)
156 {
157 $message = <<<EOT
158 Sending the request did not return a response, exception, or populate the
159 transaction with a response. This is most likely due to an incorrectly
160 implemented RingPHP handler. If you are simply trying to mock responses,
161 then it is recommended to use the GuzzleHttp\Ring\Client\MockHandler.
162 EOT;
163
164 return new RequestException($message, $request);
165 }
166 }
167