scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2 namespace GuzzleHttp\Adapter\Curl;
3  
4 use GuzzleHttp\Adapter\TransactionInterface;
5 use GuzzleHttp\Exception\AdapterException;
6  
7 /**
8 * Provides context for a Curl transaction, including active handles,
9 * pending transactions, and whether or not this is a batch or single
10 * transaction.
11 */
12 class BatchContext
13 {
14 /** @var resource Curl multi resource */
15 private $multi;
16  
17 /** @var \SplObjectStorage Map of transactions to curl resources */
18 private $handles;
19  
20 /** @var \Iterator Yields pending transactions */
21 private $pending;
22  
23 /** @var bool Whether or not to throw transactions */
24 private $throwsExceptions;
25  
26 /**
27 * @param resource $multiHandle Initialized curl_multi resource
28 * @param bool $throwsExceptions Whether or not exceptions are thrown
29 * @param \Iterator $pending Iterator yielding pending transactions
30 */
31 public function __construct(
32 $multiHandle,
33 $throwsExceptions,
34 \Iterator $pending = null
35 ) {
36 $this->multi = $multiHandle;
37 $this->handles = new \SplObjectStorage();
38 $this->throwsExceptions = $throwsExceptions;
39 $this->pending = $pending;
40 }
41  
42 /**
43 * Closes all of the requests associated with the underlying multi handle.
44 */
45 public function removeAll()
46 {
47 foreach ($this->handles as $transaction) {
48 $ch = $this->handles[$transaction];
49 curl_multi_remove_handle($this->multi, $ch);
50 curl_close($ch);
51 unset($this->handles[$transaction]);
52 }
53 }
54  
55 /**
56 * Find a transaction for a given curl handle
57 *
58 * @param resource $handle Curl handle
59 *
60 * @return TransactionInterface
61 * @throws AdapterException if a transaction is not found
62 */
63 public function findTransaction($handle)
64 {
65 foreach ($this->handles as $transaction) {
66 if ($this->handles[$transaction] === $handle) {
67 return $transaction;
68 }
69 }
70  
71 throw new AdapterException('No curl handle was found');
72 }
73  
74 /**
75 * Returns true if there are any active requests.
76 *
77 * @return bool
78 */
79 public function isActive()
80 {
81 return count($this->handles) > 0;
82 }
83  
84 /**
85 * Returns true if there are any remaining pending transactions
86 *
87 * @return bool
88 */
89 public function hasPending()
90 {
91 return $this->pending && $this->pending->valid();
92 }
93  
94 /**
95 * Pop the next transaction from the transaction queue
96 *
97 * @return TransactionInterface|null
98 */
99 public function nextPending()
100 {
101 if (!$this->hasPending()) {
102 return null;
103 }
104  
105 $current = $this->pending->current();
106 $this->pending->next();
107  
108 return $current;
109 }
110  
111 /**
112 * Checks if the batch is to throw exceptions on error
113 *
114 * @return bool
115 */
116 public function throwsExceptions()
117 {
118 return $this->throwsExceptions;
119 }
120  
121 /**
122 * Get the curl_multi handle
123 *
124 * @return resource
125 */
126 public function getMultiHandle()
127 {
128 return $this->multi;
129 }
130  
131 /**
132 * Add a transaction to the multi handle
133 *
134 * @param TransactionInterface $transaction Transaction to add
135 * @param resource $handle Resource to use with the handle
136 *
137 * @throws AdapterException If the handle is already registered
138 */
139 public function addTransaction(TransactionInterface $transaction, $handle)
140 {
141 if (isset($this->handles[$transaction])) {
142 throw new AdapterException('Transaction already registered');
143 }
144  
145 $code = curl_multi_add_handle($this->multi, $handle);
146 if ($code != CURLM_OK) {
147 MultiAdapter::throwMultiError($code);
148 }
149  
150 $this->handles[$transaction] = $handle;
151 }
152  
153 /**
154 * Remove a transaction and associated handle from the context
155 *
156 * @param TransactionInterface $transaction Transaction to remove
157 *
158 * @return array Returns the curl_getinfo array
159 * @throws AdapterException if the transaction is not found
160 */
161 public function removeTransaction(TransactionInterface $transaction)
162 {
163 if (!isset($this->handles[$transaction])) {
164 throw new AdapterException('Transaction not registered');
165 }
166  
167 $handle = $this->handles[$transaction];
168 $this->handles->detach($transaction);
169 $info = curl_getinfo($handle);
170 $code = curl_multi_remove_handle($this->multi, $handle);
171 curl_close($handle);
172  
173 if ($code !== CURLM_OK) {
174 MultiAdapter::throwMultiError($code);
175 }
176  
177 return $info;
178 }
179 }