batchrequest.class.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * Copyright 2010-2011 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 2010.08.09
  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. // CONSTRUCTOR
  46. /**
  47. * Constructs a new instance of this class.
  48. *
  49. * @param integer $limit (Optional) The size of the request window. Defaults to unlimited.
  50. * @return boolean `false` if no valid values are set, otherwise `true`.
  51. */
  52. public function __construct($limit = null)
  53. {
  54. $this->queue = array();
  55. $this->limit = $limit ? $limit : -1;
  56. return $this;
  57. }
  58. /**
  59. * Adds a new cURL handle to the request queue.
  60. *
  61. * @param resource $handle (Required) A cURL resource to add to the queue.
  62. * @return $this A reference to the current instance.
  63. */
  64. public function add($handle)
  65. {
  66. $this->queue[] = $handle;
  67. return $this;
  68. }
  69. /**
  70. * Executes the batch request queue.
  71. *
  72. * @param array $opt (DO NOT USE) Enabled for compatibility with the method this overrides, although any values passed will be ignored.
  73. * @return array An indexed array of <CFResponse> objects.
  74. */
  75. public function send($opt = null)
  76. {
  77. $http = new $this->request_class();
  78. // Make the request
  79. $response = $http->send_multi_request($this->queue, array(
  80. 'limit' => $this->limit
  81. ));
  82. return $response;
  83. }
  84. }