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 |
CurlHandler.php
001 <?php
002 namespace GuzzleHttp\Ring\Client;
003
004 use GuzzleHttp\Ring\Future\CompletedFutureArray;
005 use GuzzleHttp\Ring\Core;
006
007 /**
008 * HTTP handler that uses cURL easy handles as a transport layer.
009 *
010 * Requires PHP 5.5+
011 *
012 * When using the CurlHandler, custom curl options can be specified as an
013 * associative array of curl option constants mapping to values in the
014 * **curl** key of the "client" key of the request.
015 */
016 class CurlHandler
017 {
018 /** @var callable */
019 private $factory;
020
021 /** @var array Array of curl easy handles */
022 private $handles = [];
023
024 /** @var array Array of owned curl easy handles */
025 private $ownedHandles = [];
026
027 /** @var int Total number of idle handles to keep in cache */
028 private $maxHandles;
029
030 /**
031 * Accepts an associative array of options:
032 *
033 * - factory: Optional callable factory used to create cURL handles.
034 * The callable is passed a request hash when invoked, and returns an
035 * array of the curl handle, headers resource, and body resource.
036 * - max_handles: Maximum number of idle handles (defaults to 5).
037 *
038 * @param array $options Array of options to use with the handler
039 */
040 public function __construct(array $options = [])
041 {
042 $this->handles = $this->ownedHandles = [];
043 $this->factory = isset($options['handle_factory'])
044 ? $options['handle_factory']
045 : new CurlFactory();
046 $this->maxHandles = isset($options['max_handles'])
047 ? $options['max_handles']
048 : 5;
049 }
050
051 public function __destruct()
052 {
053 foreach ($this->handles as $handle) {
054 if (is_resource($handle)) {
055 curl_close($handle);
056 }
057 }
058 }
059
060 /**
061 * @param array $request
062 *
063 * @return CompletedFutureArray
064 */
065 public function __invoke(array $request)
066 {
067 return new CompletedFutureArray(
068 $this->_invokeAsArray($request)
069 );
070 }
071
072 /**
073 * @internal
074 *
075 * @param array $request
076 *
077 * @return array
078 */
079 public function _invokeAsArray(array $request)
080 {
081 $factory = $this->factory;
082
083 // Ensure headers are by reference. They're updated elsewhere.
084 $result = $factory($request, $this->checkoutEasyHandle());
085 $h = $result[0];
086 $hd =& $result[1];
087 $bd = $result[2];
088 Core::doSleep($request);
089 curl_exec($h);
090 $response = ['transfer_stats' => curl_getinfo($h)];
091 $response['curl']['error'] = curl_error($h);
092 $response['curl']['errno'] = curl_errno($h);
093 $response['transfer_stats'] = array_merge($response['transfer_stats'], $response['curl']);
094 $this->releaseEasyHandle($h);
095
096 return CurlFactory::createResponse([$this, '_invokeAsArray'], $request, $response, $hd, $bd);
097 }
098
099 private function checkoutEasyHandle()
100 {
101 // Find an unused handle in the cache
102 if (false !== ($key = array_search(false, $this->ownedHandles, true))) {
103 $this->ownedHandles[$key] = true;
104 return $this->handles[$key];
105 }
106
107 // Add a new handle
108 $handle = curl_init();
109 $id = (int) $handle;
110 $this->handles[$id] = $handle;
111 $this->ownedHandles[$id] = true;
112
113 return $handle;
114 }
115
116 private function releaseEasyHandle($handle)
117 {
118 $id = (int) $handle;
119 if (count($this->ownedHandles) > $this->maxHandles) {
120 curl_close($this->handles[$id]);
121 unset($this->handles[$id], $this->ownedHandles[$id]);
122 } else {
123 // curl_reset doesn't clear these out for some reason
124 static $unsetValues = [
125 CURLOPT_HEADERFUNCTION => null,
126 CURLOPT_WRITEFUNCTION => null,
127 CURLOPT_READFUNCTION => null,
128 CURLOPT_PROGRESSFUNCTION => null,
129 ];
130 curl_setopt_array($handle, $unsetValues);
131 curl_reset($handle);
132 $this->ownedHandles[$id] = false;
133 }
134 }
135 }
136