You are here

varnish.install in Varnish 8

Same filename and directory in other branches
  1. 6 varnish.install
  2. 7 varnish.install

Install and runtime requirements for the Varnish module.

File

varnish.install
View source
<?php

/**
 * @file
 * Install and runtime requirements for the Varnish module.
 */

/**
 * Implements hook_requirements().
 *
 * Ensure that dependencies are met and that varnish's connection is good.
 */
function varnish_requirements($phase) {
  $requirements = [];
  switch ($phase) {
    case 'install':
      $has_extension = extension_loaded('sockets');
      $requirements['varnish'] = [
        'title' => t('Varnish dependencies'),
        'value' => $has_extension ? t('Socket extension available') : t('Socket extension not installed'),
        'severity' => $has_extension ? REQUIREMENT_OK : REQUIREMENT_ERROR,
        'description' => $has_extension ? NULL : t('Install the PHP extension "sockets".'),
      ];
      break;
    case 'runtime':
      $requirements['varnish']['title'] = t('Varnish status');
      $status = varnish_get_status();
      foreach ($status as $terminal => $state) {
        list($server, $port) = explode(':', $terminal);
        if ($state === VARNISH_SERVER_STATUS_AUTHENTICATION_FAILURE) {

          // Tailor the requirements description depending on whether a secret
          // key is configured.
          $config = \Drupal::config('varnish.settings');
          $description = empty($config
            ->get('varnish_control_key')) ? t('The Varnish control terminal requires a secret key at %server on port %port.', [
            '%server' => $server,
            '%port' => $port,
          ]) : t('The Varnish control terminal did not accept the secret key at %server on port %port.', [
            '%server' => $server,
            '%port' => $port,
          ]);
          $requirements['varnish']['value'] = t('Varnish authentication failure');
          $requirements['varnish']['severity'] = REQUIREMENT_ERROR;
          $requirements['varnish']['description'] = $description;

          // Abort on the first failure.
          return $requirements;
        }
        elseif ($state === VARNISH_SERVER_STATUS_DOWN) {
          $requirements['varnish']['value'] = t('Varnish connection broken');
          $requirements['varnish']['severity'] = REQUIREMENT_ERROR;
          $requirements['varnish']['description'] = t('The Varnish control terminal is not responding at %server on port %port.', [
            '%server' => $server,
            '%port' => $port,
          ]);

          // Abort on the first failure.
          return $requirements;
        }
        else {
          $requirements['varnish']['value'] = t('Running');
        }
      }
      break;
  }
  return $requirements;
}

Functions

Namesort descending Description
varnish_requirements Implements hook_requirements().