You are here

public function AcsfInitCommands::initVerify in Acquia Cloud Site Factory Connector 8.2

Verifies that acsf-init was successfully run in the current version.

@command acsf-init-verify @option skip-default-settings Skip verifying the default settings.php file. @bootstrap root

Parameters

array $options: The command options supplied to the executed command.

Throws

\Drupal\acsf\AcsfException If something is wrong with the current codebase.

1 call to AcsfInitCommands::initVerify()
AcsfInitCommands::init in acsf_init/src/Commands/AcsfInitCommands.php
Make this repository compatible with Acquia Site Factory.

File

acsf_init/src/Commands/AcsfInitCommands.php, line 250

Class

AcsfInitCommands
Provides drush commands to set up a codebase for Acquia Cloud Site Factory.

Namespace

Drush\Commands

Code

public function initVerify(array $options = [
  'skip-default-settings' => NULL,
]) {
  $drupal_root = realpath(DRUPAL_ROOT);
  if (basename($drupal_root) !== 'docroot') {

    // If for some reason we wanted to enable this command to run on directory
    // structures where no /docroot exists, that's possible in theory... but
    // we need to to audit / change the code in this file to
    // - derive the repo root in a good way;
    // - properly distinguish the repo root from the drupal root;
    // - figure out what to do with remaining instances of "docroot", if any;
    // - figure out what to do when drupal root and repo root are equal...
    //   like not copy hook files?
    throw new AcsfException(dt("Drupal must be installed in a subdirectory of your code repository, named 'docroot'."));
  }
  $repo_root = dirname($drupal_root);
  $skip_default_settings = $options['skip-default-settings'];
  $lib_path = sprintf('%s/lib', dirname(dirname(dirname(__FILE__))));
  $error = FALSE;
  foreach ($this
    ->getRequiredFiles($repo_root) as $location) {
    $file = $location['filename'];
    $dest = sprintf('%s/%s', $location['dest'], $file);

    // Some files only contain a destination as they are already in place.
    if (isset($location['source']) && isset($location['dest'])) {
      $source = sprintf('%s/%s/%s', $lib_path, $location['source'], $file);
      if (!file_exists($dest)) {
        $error = TRUE;
        $this
          ->logger()
          ->error(dt('The file !file is missing.', [
          '!file' => $file,
        ]));
      }
      elseif (md5_file($source) != md5_file($dest)) {
        $error = TRUE;
        $this
          ->logger()
          ->error(dt('The file !file is out of date.', [
          '!file' => $file,
        ]));
      }
    }

    // Verify the file is executable.
    // Note: The approach here is to not check for the exact file perms (in
    // other words to not test against the 'mod' element), since git - by
    // design - does not respect anything beyond a simple executable bit, the
    // other perms may be filesystem/OS-dependent, and can't be guaranteed to
    // be consistent.
    if (file_exists($dest) && !empty($location['test_executable'])) {
      $dest_permissions = fileperms($dest);

      // We do want to test the owner executable bit, and the group executable
      // bit as well.
      // e.g. to test whether the owner has execute permission, it is the case
      // of testing with: 00000000 01000000 (which is 0100 in octal, 64 in
      // decimal).
      if (($dest_permissions & (0100 | 010)) != (0100 | 010)) {
        $error = TRUE;
        $this
          ->logger()
          ->error(dt('The file !file is not executable. Make this file executable for the owner and group, then commit it again.', [
          '!file' => $file,
        ]));
      }
    }
  }
  if (!$this
    ->testHtaccessIsPatched()) {
    $error = TRUE;
    $this
      ->logger()
      ->error(dt('The .htaccess file has not been patched to allow access to apc_rebuild.php.'));
  }

  // Skip the default settings.php file if --skip-default-settings is set.
  if (!$skip_default_settings) {

    // Check that the default settings.php contains the necessary ACSF
    // business logic.
    $acsf_business_logic = $this
      ->defaultSettingsPhpIncludeGet();

    // Break up the business logic by lines.
    $acsf_business_logic_fragments = explode("\n", $acsf_business_logic);

    // Examine each line in the business logic to make sure it appears in the
    // file. This way minor indentation changes will not cause failure.
    $missing_piece = FALSE;
    $default_settings_php_contents = file_get_contents($repo_root . '/docroot/sites/default/settings.php');
    foreach ($acsf_business_logic_fragments as $line) {
      if (strpos($default_settings_php_contents, $line) === FALSE) {
        $missing_piece = TRUE;
        break;
      }
    }
    if ($missing_piece) {
      $error = TRUE;
      $this
        ->logger()
        ->error(dt('The default settings.php file is out of date.'));
    }
  }
  if ($error) {
    throw new AcsfException(dt('Please run drush acsf-init to correct these problems and commit the resulting code changes.'));
  }
  else {

    // The Site Factory code deployment uses this string to determine if the
    // acsf-init has been properly run. If this is changed, also ensure that
    // the check in VcsVerifyAcsf matches.
    $this
      ->logger()
      ->success(dt('acsf-init required files ok'));
  }
}