batchrequest.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // EXCEPTIONS
  18. /**
  19. * Default CFBatchRequest Exception.
  20. */
  21. class CFBatchRequest_Exception extends Exception {}
  22. /*%******************************************************************************************%*/
  23. // CLASS
  24. /**
  25. * Simplifies the flow involved with managing and executing a batch request queue. Batch requesting is the
  26. * ability to queue up a series of requests and execute them all in parallel. This allows for faster
  27. * application performance when a lot of requests are involved.
  28. *
  29. * @version 2011.12.02
  30. * @license See the included NOTICE.md file for more information.
  31. * @copyright See the included NOTICE.md file for more information.
  32. * @link http://aws.amazon.com/php/ PHP Developer Center
  33. */
  34. class CFBatchRequest extends CFRuntime
  35. {
  36. /**
  37. * Stores the cURL handles that are to be processed.
  38. */
  39. public $queue;
  40. /**
  41. * Stores the size of the request window.
  42. */
  43. public $limit;
  44. /**
  45. * The proxy to use for connecting.
  46. */
  47. public $proxy = null;
  48. /**
  49. * The helpers to use when connecting.
  50. */
  51. public $helpers = null;
  52. /**
  53. * The active credential set.
  54. */
  55. public $credentials;
  56. /*%******************************************************************************************%*/
  57. // CONSTRUCTOR
  58. /**
  59. * Constructs a new instance of this class.
  60. *
  61. * @param integer $limit (Optional) The size of the request window. Defaults to unlimited.
  62. * @return boolean `false` if no valid values are set, otherwise `true`.
  63. */
  64. public function __construct($limit = null)
  65. {
  66. $this->queue = array();
  67. $this->limit = $limit ? $limit : -1;
  68. $this->credentials = new CFCredential(array());
  69. return $this;
  70. }
  71. /**
  72. * Sets the AWS credentials to use for the batch request.
  73. *
  74. * @param CFCredential $credentials (Required) The credentials to use for signing and making requests.
  75. * @return $this A reference to the current instance.
  76. */
  77. public function use_credentials(CFCredential $credentials)
  78. {
  79. $this->credentials = $credentials;
  80. return $this;
  81. }
  82. /**
  83. * Adds a new cURL handle to the request queue.
  84. *
  85. * @param resource $handle (Required) A cURL resource to add to the queue.
  86. * @return $this A reference to the current instance.
  87. */
  88. public function add($handle)
  89. {
  90. $this->queue[] = $handle;
  91. return $this;
  92. }
  93. /**
  94. * Executes the batch request queue.
  95. *
  96. * @param array $opt (DO NOT USE) Enabled for compatibility with the method this overrides, although any values passed will be ignored.
  97. * @return array An indexed array of <CFResponse> objects.
  98. */
  99. public function send($opt = null)
  100. {
  101. $http = new $this->request_class(null, $this->proxy, null, $this->credentials);
  102. // Make the request
  103. $response = $http->send_multi_request($this->queue, array(
  104. 'limit' => $this->limit
  105. ));
  106. return $response;
  107. }
  108. }