4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * @file CAS/Request/AbstractRequest.php
23 * @category Authentication
25 * @author Adam Franco <afranco@middlebury.edu>
26 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
27 * @link https://wiki.jasig.org/display/CASC/phpCAS
31 * This interface defines a class library for performing multiple web requests
32 * in batches. Implementations of this interface may perform requests serially
35 * @class CAS_Request_CurlMultiRequest
36 * @category Authentication
38 * @author Adam Franco <afranco@middlebury.edu>
39 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
40 * @link https://wiki.jasig.org/display/CASC/phpCAS
42 class CAS_Request_CurlMultiRequest
43 implements CAS_Request_MultiRequestInterface
45 private $_requests = array();
46 private $_sent = false;
48 /*********************************************************
50 *********************************************************/
53 * Add a new Request to this batch.
54 * Note, implementations will likely restrict requests to their own concrete
57 * @param CAS_Request_RequestInterface $request reqest to add
60 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
61 * @throws CAS_InvalidArgumentException If passed a Request of the wrong
64 public function addRequest (CAS_Request_RequestInterface $request)
67 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
69 if (!$request instanceof CAS_Request_CurlRequest) {
70 throw new CAS_InvalidArgumentException('As a CAS_Request_CurlMultiRequest, I can only work with CAS_Request_CurlRequest objects.');
73 $this->_requests[] = $request;
77 * Retrieve the number of requests added to this batch.
79 * @return number of request elements
81 public function getNumRequests()
84 throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
86 return count($this->_requests);
89 /*********************************************************
91 *********************************************************/
94 * Perform the request. After sending, all requests will have their
97 * @return bool TRUE on success, FALSE on failure.
98 * @throws CAS_OutOfSequenceException If called multiple times.
100 public function send ()
103 throw new CAS_OutOfSequenceException('Request has already been sent cannot send again.');
105 if (!count($this->_requests)) {
106 throw new CAS_OutOfSequenceException('At least one request must be added via addRequest() before the multi-request can be sent.');
111 // Initialize our handles and configure all requests.
113 $multiHandle = curl_multi_init();
114 foreach ($this->_requests as $i => $request) {
115 $handle = $request->_initAndConfigure();
116 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
117 $handles[$i] = $handle;
118 curl_multi_add_handle($multiHandle, $handle);
121 // Execute the requests in parallel.
123 curl_multi_exec($multiHandle, $running);
124 } while ($running > 0);
126 // Populate all of the responses or errors back into the request objects.
127 foreach ($this->_requests as $i => $request) {
128 $buf = curl_multi_getcontent($handles[$i]);
129 $request->_storeResponseBody($buf);
130 curl_multi_remove_handle($multiHandle, $handles[$i]);
131 curl_close($handles[$i]);
134 curl_multi_close($multiHandle);