You are here

class PurgeHandlerCurl in Purge 7.2

Class definition for the Curl HTTP request library.

Hierarchy

Expanded class hierarchy of PurgeHandlerCurl

File

includes/purge.class.inc, line 525
Contains all class and interface definitions for Purge.

View source
class PurgeHandlerCurl extends PurgeHandler implements PurgeDependable {

  /**
   * Funtion will determine what options are set and call the right execute
   * function.
   */
  public function execute($purges, $handler_options) {
    if ($handler_options['multi']) {

      //if ($handler_options['legacy']) {

      //  $purges = $this->execute_legacy($purges, $handler_options);

      //}

      //else {
      $purges = $this
        ->execute_multi($purges, $handler_options);

      // }
    }
    else {
      $purges = $this
        ->execute_single($purges, $handler_options);
    }
    return $purges;
  }

  /**
   * Function to procerss a single purge with curl.
   */
  private function execute_single($purges, $handler_options) {
    foreach ($purges as $purge_id => $purge) {
      $curl_request = $this
        ->get_curl_request($purge, $handler_options);

      // Execute the request
      curl_exec($curl_request);

      // Get http status code.
      $info = curl_getinfo($curl_request);
      $purges[$purge_id]['http_code'] = $info['http_code'];
    }
    return $purges;
  }

  /**
   * Function to process multiple purges with curl.
   */
  private function execute_multi($purges, $handler_options) {
    $curl_purges = curl_multi_init();
    foreach ($purges as $purge_id => $purge) {
      $curl_requests[$purge_id] = $this
        ->get_curl_request($purge, $handler_options);
      curl_multi_add_handle($curl_purges, $curl_requests[$purge_id]);
    }

    // Execute the purge requests
    ob_start();
    $active = 0;
    $multi_result = $this
      ->multi_exec($curl_purges, $active);
    while ($active && $multi_result == CURLM_OK) {
      if (curl_multi_select($curl_purges) == -1) {
        usleep(100000);
      }
      $multi_result = $this
        ->multi_exec($curl_purges, $active);
    }
    ob_end_clean();

    // Result collection. Collects the http code returned for each url purged
    foreach ($purges as $purge_id => $purge) {
      $info = curl_getinfo($curl_requests[$purge_id]);
      $purges[$purge_id]['http_code'] = $info['http_code'];
      curl_multi_remove_handle($curl_purges, $curl_requests[$purge_id]);
    }
    curl_multi_close($curl_purges);
    return $purges;
  }

  /**
   * Wrapper for curl_multi_exec().
   */
  private function multi_exec($curl_purges, &$active) {
    do {
      $multi_result = curl_multi_exec($curl_purges, $active);
    } while ($multi_result == CURLM_CALL_MULTI_PERFORM);
    return $multi_result;
  }

  /**
   * Function to construct a curl request object.
   */
  private function get_curl_request($purge, $handler_options) {
    $headers = array();
    foreach ($purge['headers'] as $header_key => $header_value) {
      $headers[] = $header_key . ": " . $header_value;
    }
    $curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, $handler_options['method']);
    curl_setopt($curl_request, CURLOPT_URL, $purge['purge_url']);
    curl_setopt($curl_request, CURLOPT_HEADER, 1);
    curl_setopt($curl_request, CURLOPT_HTTPHEADER, $purge['headers']);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 0);
    return $curl_request;
  }

  /**
   * Checks if the curl library is installed.
   */
  public function check_depend() {
    $pass = extension_loaded('curl');
    return $pass;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Purge::$access public property
Purge::$available public property
Purge::$depend public property
Purge::$description public property
Purge::$enabled public property
Purge::$item public property
Purge::$name public property
Purge::$option public property
Purge::validate public function Does basic dependency checks for builtin dependencies. 5
Purge::__sleep public function Only serialize the static values. 1
PurgeHandlerCurl::check_depend public function Checks if the curl library is installed.
PurgeHandlerCurl::execute public function Funtion will determine what options are set and call the right execute function.
PurgeHandlerCurl::execute_multi private function Function to process multiple purges with curl.
PurgeHandlerCurl::execute_single private function Function to procerss a single purge with curl.
PurgeHandlerCurl::get_curl_request private function Function to construct a curl request object.
PurgeHandlerCurl::multi_exec private function Wrapper for curl_multi_exec().