You are here

class ApdqcPrefetchDrupalDefaultEntityController in Asynchronous Prefetch Database Query Cache 7

Passthrough DrupalDefaultEntityController class for prefetching cache ids.

Hierarchy

Expanded class hierarchy of ApdqcPrefetchDrupalDefaultEntityController

1 string reference to 'ApdqcPrefetchDrupalDefaultEntityController'
apdqc_after_entity_info_alter in ./apdqc.module
Implements hook_entity_info_alter().

File

./apdqc.module, line 553
Asynchronous Prefetch Database Query Cache module.

View source
class ApdqcPrefetchDrupalDefaultEntityController {
  public $originalBaseClass;
  public $originalEntityType;
  public $originalEntityInfo;

  /**
   * Get the entity type and create the original controller object.
   *
   * @param string $entity_type
   *   Entity type.
   */
  public function __construct($entity_type) {
    $entity_info = entity_get_info($entity_type);
    $classname = $entity_info['original controller class'];
    $this->originalEntityInfo = $entity_info;
    $this->originalEntityType = $entity_type;
    $this->originalBaseClass = new $classname($entity_type);
  }

  /**
   * Magic method; forward all calls to the original base class.
   *
   * On load calls prefetch the field data.
   *
   * @param string $name
   *   Name of the method called.
   * @param array $arguments
   *   Array of parameters passed to the method.
   */
  public function __call($name, array $arguments) {
    if ($name == 'load' && function_exists('apdqc_run_prefetch_array') && function_exists('apdqc_escape_string')) {
      if (apdqc_get_bin_class_name('cache_field') === 'APDQCache') {

        // Use the advanced drupal_static() pattern, since this is called very
        // often.
        static $drupal_static_fast;
        if (!isset($drupal_static_fast)) {
          $drupal_static_fast['cache_field'] =& drupal_static('apdqc_cache_prefetch_ids');
        }
        $table_keys_cache_prefetch = array();
        if (!empty($arguments[0])) {
          foreach ($arguments[0] as $id) {
            if (is_string($id) || is_numeric($id)) {
              $string = apdqc_escape_string('field:' . $this->originalEntityType . ':' . $id);
              if (!isset($drupal_static_fast['cache_field'][$string])) {
                $drupal_static_fast['cache_field'][$string] = TRUE;
                $table_keys_cache_prefetch['cache_field'][] = $string;
              }
            }
          }
        }

        // Prefetch cache.
        if (!empty($table_keys_cache_prefetch)) {
          apdqc_run_prefetch_array($table_keys_cache_prefetch);
        }
      }
    }
    if ($name == 'load' && !empty($arguments[0]) && is_array($arguments[0]) && count($arguments[0]) == 1 && variable_get('apdqc_entity_load_skip_null', APDQC_ENTITY_LOAD_SKIP_NULL)) {
      $test = reset($arguments[0]);
      if (empty($test) && is_null($test)) {
        return array();
      }
    }
    $return = call_user_func_array(array(
      $this->originalBaseClass,
      $name,
    ), $arguments);
    return $return;
  }

}

Members