You are here

class panels_cache_object in Panels 7.3

Same name and namespace in other branches
  1. 5.2 includes/plugins.inc \panels_cache_object
  2. 6.3 includes/plugins.inc \panels_cache_object
  3. 6.2 includes/plugins.inc \panels_cache_object

An object to hold caching information while it is happening.

Hierarchy

Expanded class hierarchy of panels_cache_object

File

includes/plugins.inc, line 116
Contains helper code for plugins and contexts.

View source
class panels_cache_object {
  var $content = '';
  var $head = NULL;
  var $css = NULL;
  var $js = NULL;
  var $tokens = NULL;
  var $ready = FALSE;

  /**
   * When constructed, take a snapshot of our existing out of band data.
   */
  function __construct() {
    $this->head = drupal_add_html_head();
    $this->css = drupal_add_css();
    $this->tokens = ctools_set_page_token();
    $this->js = drupal_add_js();
  }

  /**
   * Add content to the cache. This assumes a pure stream;
   * use set_content() if it's something else.
   */
  function add_content($content) {
    $this->content .= $content;
  }
  function set_content($content) {
    $this->content = $content;
  }

  /**
   * Set the object for storing. This overwrites.
   */
  function cache() {
    if ($this->ready) {
      return;
    }
    $this->ready = TRUE;

    // Simple replacement for head.
    $this->head = str_replace($this->head, '', drupal_add_html_head());

    // Slightly less simple for CSS:
    $css = drupal_add_css();
    $start = $this->css;
    $this->css = array();
    foreach ($css as $name => $data) {
      if (!isset($start[$name])) {
        $this->css[$name] = $data;
      }
    }
    $js = drupal_add_js();
    $start = $this->js;
    $this->js = array();

    // Use the advanced mapping function from Drupal >= 7.23 if available.
    $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';

    // If there are any differences between the old and the new javascript then
    // store them to be added later.
    if ($diff = $array_mapping_func($js, $start)) {

      // Iterate over the diff to ensure we keep the keys on merge and don't add
      // unnecessary items.
      foreach ($diff as $key => $diff_data) {

        // Special case the settings key and get the difference of the data.
        if ($key === 'settings') {

          // Iterate over the diff to ensure we keep the keys on merge and don't
          // add unnecessary items.
          if (isset($diff[$key]['data'])) {
            foreach ($diff[$key]['data'] as $settings_key => $settings_data) {

              // Merge the changes with the base to get a complete settings
              // array.
              $this->js[$key]['data'][] = drupal_array_merge_deep($settings_data, $diff[$key]['data'][$settings_key]);
            }
          }
        }
        else {
          $this->js[$key] = $diff_data;

          // Check if the key was present already and if so merge the changes
          // with the original data to get the full settings array.
          if (isset($start[$key])) {
            $this->js[$key] = drupal_array_merge_deep($start[$key], $this->js[$key]);
          }
        }
      }
    }

    // And for tokens:
    $tokens = ctools_set_page_token();
    foreach ($this->tokens as $token => $argument) {
      if (isset($tokens[$token])) {
        unset($tokens[$token]);
      }
    }
    $this->tokens = $tokens;
  }

  /**
   * Restore out of band data saved to cache.
   */
  function restore() {
    if (!empty($this->head)) {
      drupal_add_html_head($this->head);
    }
    if (!empty($this->css)) {
      foreach ($this->css as $args) {
        drupal_add_css($args['data'], $args);
      }
    }
    if (!empty($this->js)) {
      foreach ($this->js as $key => $args) {
        if ($key !== 'settings') {
          drupal_add_js($args['data'], $args);
        }
        else {
          foreach ($args['data'] as $setting) {
            drupal_add_js($setting, 'setting');
          }
        }
      }
    }
    if (!empty($this->tokens)) {
      foreach ($this->tokens as $token => $key) {
        list($type, $argument) = $key;
        ctools_set_page_token($token, $type, $argument);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
panels_cache_object::$content property
panels_cache_object::$css property
panels_cache_object::$head property
panels_cache_object::$js property
panels_cache_object::$ready property
panels_cache_object::$tokens property
panels_cache_object::add_content function Add content to the cache. This assumes a pure stream; use set_content() if it's something else.
panels_cache_object::cache function Set the object for storing. This overwrites.
panels_cache_object::restore function Restore out of band data saved to cache.
panels_cache_object::set_content function
panels_cache_object::__construct function When constructed, take a snapshot of our existing out of band data.