| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
|
6 |
* You may not use this file except in compliance with the License.
|
|
|
7 |
* A copy of the License is located at
|
|
|
8 |
*
|
|
|
9 |
* http://aws.amazon.com/apache2.0
|
|
|
10 |
*
|
|
|
11 |
* or in the "license" file accompanying this file. This file is distributed
|
|
|
12 |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
|
13 |
* express or implied. See the License for the specific language governing
|
|
|
14 |
* permissions and limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
/*%******************************************************************************************%*/
|
|
|
19 |
// EXCEPTIONS
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Default CFBatchRequest Exception.
|
|
|
23 |
*/
|
|
|
24 |
class CFBatchRequest_Exception extends Exception {}
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
/*%******************************************************************************************%*/
|
|
|
28 |
// CLASS
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Simplifies the flow involved with managing and executing a batch request queue. Batch requesting is the
|
|
|
32 |
* ability to queue up a series of requests and execute them all in parallel. This allows for faster
|
|
|
33 |
* application performance when a lot of requests are involved.
|
|
|
34 |
*
|
|
|
35 |
* @version 2011.12.02
|
|
|
36 |
* @license See the included NOTICE.md file for more information.
|
|
|
37 |
* @copyright See the included NOTICE.md file for more information.
|
|
|
38 |
* @link http://aws.amazon.com/php/ PHP Developer Center
|
|
|
39 |
*/
|
|
|
40 |
class CFBatchRequest extends CFRuntime
|
|
|
41 |
{
|
|
|
42 |
/**
|
|
|
43 |
* Stores the cURL handles that are to be processed.
|
|
|
44 |
*/
|
|
|
45 |
public $queue;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Stores the size of the request window.
|
|
|
49 |
*/
|
|
|
50 |
public $limit;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* The proxy to use for connecting.
|
|
|
54 |
*/
|
|
|
55 |
public $proxy = null;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* The helpers to use when connecting.
|
|
|
59 |
*/
|
|
|
60 |
public $helpers = null;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* The active credential set.
|
|
|
64 |
*/
|
|
|
65 |
public $credentials;
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
/*%******************************************************************************************%*/
|
|
|
69 |
// CONSTRUCTOR
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Constructs a new instance of this class.
|
|
|
73 |
*
|
|
|
74 |
* @param integer $limit (Optional) The size of the request window. Defaults to unlimited.
|
|
|
75 |
* @return boolean `false` if no valid values are set, otherwise `true`.
|
|
|
76 |
*/
|
|
|
77 |
public function __construct($limit = null)
|
|
|
78 |
{
|
|
|
79 |
$this->queue = array();
|
|
|
80 |
$this->limit = $limit ? $limit : -1;
|
|
|
81 |
$this->credentials = new CFCredential(array());
|
|
|
82 |
return $this;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Sets the AWS credentials to use for the batch request.
|
|
|
87 |
*
|
|
|
88 |
* @param CFCredential $credentials (Required) The credentials to use for signing and making requests.
|
|
|
89 |
* @return $this A reference to the current instance.
|
|
|
90 |
*/
|
|
|
91 |
public function use_credentials(CFCredential $credentials)
|
|
|
92 |
{
|
|
|
93 |
$this->credentials = $credentials;
|
|
|
94 |
return $this;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Adds a new cURL handle to the request queue.
|
|
|
99 |
*
|
|
|
100 |
* @param resource $handle (Required) A cURL resource to add to the queue.
|
|
|
101 |
* @return $this A reference to the current instance.
|
|
|
102 |
*/
|
|
|
103 |
public function add($handle)
|
|
|
104 |
{
|
|
|
105 |
$this->queue[] = $handle;
|
|
|
106 |
return $this;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Executes the batch request queue.
|
|
|
111 |
*
|
|
|
112 |
* @param array $opt (DO NOT USE) Enabled for compatibility with the method this overrides, although any values passed will be ignored.
|
|
|
113 |
* @return array An indexed array of <CFResponse> objects.
|
|
|
114 |
*/
|
|
|
115 |
public function send($opt = null)
|
|
|
116 |
{
|
|
|
117 |
$http = new $this->request_class(null, $this->proxy, null, $this->credentials);
|
|
|
118 |
|
|
|
119 |
// Make the request
|
|
|
120 |
$response = $http->send_multi_request($this->queue, array(
|
|
|
121 |
'limit' => $this->limit
|
|
|
122 |
));
|
|
|
123 |
|
|
|
124 |
return $response;
|
|
|
125 |
}
|
|
|
126 |
}
|