You are here

function varnish_requirements in Varnish 8

Same name and namespace in other branches
  1. 5 varnish.module \varnish_requirements()
  2. 6 varnish.module \varnish_requirements()
  3. 7 varnish.install \varnish_requirements()

Implements hook_requirements().

Ensure that dependencies are met and that varnish's connection is good.

File

./varnish.install, line 13
Install and runtime requirements for the Varnish module.

Code

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;
}