You are here

flysystem.install in Flysystem 8

Install, update and uninstall functions for the flysystem module.

File

flysystem.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the flysystem module.
 */
use Drupal\Core\Site\Settings;
use Drupal\flysystem\Logger\Convert;

/**
 * Implements hook_requirements().
 */
function flysystem_requirements($phase) {
  $requirements = [];
  __flysystem_validate_schemes($requirements);
  __flysystem_check_dependencies($requirements);
  if ($phase === 'runtime') {
    __flysystem_call_ensure($requirements);
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function flysystem_install() {
  Drupal::service('flysystem_factory')
    ->ensure();
}

/**
 * Validates configured schemes.
 */
function __flysystem_validate_schemes(array &$requirements) {
  $invalid = [];
  foreach (array_keys(Settings::get('flysystem', [])) as $scheme) {
    if (!preg_match('/^[a-zA-Z0-9+.-]+$/', $scheme)) {
      $invalid[] = $scheme;
    }
  }
  if ($invalid) {
    $requirements['flysystem_invalid_scheme'] = [
      'title' => \Drupal::translation()
        ->translate('Invalid Flysystem schemes in settings.php'),
      'description' => \Drupal::translation()
        ->translate("The following schemes are not in the correct format: %schemes. Scheme names can only contain letters, numbers, + (plus sign), . (period), - (hyphen).", [
        '%schemes' => implode(', ', $invalid),
      ]),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
}

/**
 * Checks that dependencies are installed.
 */
function __flysystem_check_dependencies(array &$requirements) {
  $dependencies = [
    'League\\Flysystem\\Filesystem' => \Drupal::translation()
      ->translate('Flysystem'),
    'League\\Flysystem\\Replicate\\ReplicateAdapter' => \Drupal::translation()
      ->translate('replicate adapter'),
    'Twistor\\FlysystemStreamWrapper' => \Drupal::translation()
      ->translate('stream wrapper'),
  ];
  $missing = array_filter(array_map(function ($dependency) use ($dependencies) {
    return class_exists($dependency) ? FALSE : $dependencies[$dependency];
  }, array_keys($dependencies)));

  // @codeCoverageIgnoreStart
  if ($missing) {
    $requirements['flysystem_dependencies'] = [
      'title' => \Drupal::translation()
        ->translate('Flysystem'),
      'description' => \Drupal::translation()
        ->translate('Dependencies missing: @deps.', [
        '@deps' => implode(', ', $missing),
      ]),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  // @codeCoverageIgnoreEnd
}

/**
 * Checks the state of existing configuratio.
 */
function __flysystem_call_ensure(array &$requirements) {
  foreach (\Drupal::service('flysystem_factory')
    ->ensure() as $scheme => $errors) {
    foreach ($errors as $error) {
      $requirements['flysystem:' . $scheme] = [
        'title' => \Drupal::translation()
          ->translate('Flysystem: @scheme', [
          '@scheme' => $scheme,
        ]),
        'description' => \Drupal::translation()
          ->translate($error['message'], $error['context']),
        'severity' => Convert::rfcToHookRequirements($error['severity']),
      ];
    }
  }
}

Functions

Namesort descending Description
flysystem_install Implements hook_install().
flysystem_requirements Implements hook_requirements().
__flysystem_call_ensure Checks the state of existing configuratio.
__flysystem_check_dependencies Checks that dependencies are installed.
__flysystem_validate_schemes Validates configured schemes.