<?php

/**
 * @file
 * Installation for Echo module.
 */

/**
 * Implements hook_requirements().
 *
 * Since the Echo module relies on a working cache implementation, check whether
 * a cache_set() followed immediately by a cache_get() retrieves what it stored.
 */
function echo_requirements($phase) {
  $title = 'Echo test';
  $uniqid = uniqid();
  $content = "<div id='$uniqid'>Hello, world!</div>";
  $theme = 'bartik';
  if ($phase == 'runtime') {
    // The echo_themed_page() function should be available at this point.
    $document = new DomDocument();
    $document->loadHTML(echo_themed_page($title, $content, $theme));
    $xpath = new DOMXpath($document);
    $pass = $xpath->query("//div[@id='$uniqid']");
  }
  else {
    $key = sha1($title . $content . $theme);
    // Thirty seconds ought to be enough for anyone.
    $expiration = REQUEST_TIME + max(ini_get('max_execution_time'), 30);
    cache_set($key, $key, 'cache', $expiration);
    sleep(1);
    $cached = cache_get($key);
    $pass = ($cached->data === $key);
  }
  return array(
    'echo' => array(
      'title' => t('Cache system'),
      'description' => t('The <a href="!echo">%echo</a> module requires a working cache system.', array('!echo' => 'http://drupal.org/project/echo', '%echo' => 'Echo')),
      'value' => $pass ? t('Working') : t('Not working'),
      'severity' => $pass ? REQUIREMENT_OK : REQUIREMENT_ERROR,
    ),
  );
}
