You are here

advagg.install in Advanced CSS/JS Aggregation 8.4

Handles Advanced Aggregation installation and upgrade tasks.

File

advagg.install
View source
<?php

/**
 * @file
 * Handles Advanced Aggregation installation and upgrade tasks.
 */
use Drupal\Core\Url;
use Drupal\advagg\Asset\AssetOptimizer;
use Drupal\advagg\Form\OperationsForm;

/**
 * Implements hook_install().
 */
function advagg_install() {

  // Make sure permissions for dirs are correct. Needed if installed via drush.
  $stat_public = stat('public://');

  // Check if this folder already exists - could be the case on re-install.
  if (!file_exists('public://css')) {
    \Drupal::service('file_system')
      ->mkdir('public://css');
  }
  $stat_css = stat('public://css');

  // Check if this folder already exists - could be the case on re-install.
  if (!file_exists('public://js')) {
    \Drupal::service('file_system')
      ->mkdir('public://js');
  }
  $stat_js = stat('public://js');
  if (isset($stat_public['uid'])) {
    if (isset($stat_css['uid']) && $stat_public['uid'] != $stat_css['uid']) {
      @chown($stat_css[0], $stat_public['uid']);
    }
    if (isset($stat_js['uid']) && $stat_public['uid'] != $stat_js['uid']) {
      @chown($stat_js[0], $stat_public['uid']);
    }
  }
  if (isset($stat_public['gid'])) {
    if (isset($stat_css['gid']) && $stat_public['gid'] != $stat_css['gid']) {
      @chgrp($stat_css[0], $stat_public['gid']);
    }
    if (isset($stat_js['uid']) && $stat_public['gid'] != $stat_js['gid']) {
      @chgrp($stat_js[0], $stat_public['gid']);
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function advagg_uninstall() {
  $form = OperationsForm::create(Drupal::getContainer());
  $form
    ->clearAggregates();
}

/**
 * Implements hook_requirements().
 */
function advagg_requirements($phase) {
  $requirements = [];

  // Ensure translations don't break at install time.
  $t = 't';

  // Always check these, independent of the current phase.
  $function_list = [
    'rename',
  ];

  // Check each function to make sure it exists.
  foreach ($function_list as $function_name) {
    if (!function_exists($function_name)) {
      $requirements['advagg_function_' . $function_name] = [
        'title' => $t('Adv CSS/JS Agg - Function Disabled'),
        'value' => $phase === 'install' ? FALSE : $function_name,
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('<a href="!url">%name()</a> is disabled on this server. Please contact your hosting provider or server administrator and see if they can re-enable this function for you.', [
          '!url' => 'http://php.net/' . str_replace('_', '-', $function_name),
          '%name' => $function_name,
        ]),
      ];
    }
  }

  // If not at runtime, return here.
  if ($phase !== 'runtime') {
    return $requirements;
  }
  $config = \Drupal::config('advagg.settings');
  if (!$config
    ->get('skip_enabled_preprocess_check')) {

    // Make sure variables are set correctly.
    if (!$config
      ->get('enabled')) {
      $requirements['advagg_not_on'] = [
        'title' => $t('Adv CSS/JS Agg - Enabled'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Advanced CSS/JS aggregation is disabled.'),
        'description' => $t('Go to the Advanced CSS/JS aggregation <a href="@settings">settings page</a> and enable it.', [
          '@settings' => Url::fromRoute('advagg.settings')
            ->toString(),
        ]),
      ];
    }
  }
  $advaggMessages = [];
  if (!$config
    ->get('enabled')) {
    $advaggMessages[] = $t('Advanced CSS/JS aggregation is disabled. Go to the Advanced CSS/JS aggregation <a href="@settings">settings page</a> and enable it.', [
      '@settings' => Url::fromRoute('advagg.settings')
        ->toString(),
    ]);
  }
  if ($config
    ->get('cache_level') === 0) {
    $advaggMessages[] = $t('Currently running in development mode.');
  }
  $requirements['advagg_ok'] = [
    'title' => $t('Adv CSS/JS Agg'),
    'severity' => REQUIREMENT_OK,
    'value' => $t('Advanced CSS/JS Aggregator should be working correctly.'),
    'description' => [
      '#theme' => 'item_list',
      '#items' => $advaggMessages,
      '#title' => '',
      '#list_type' => 'ul',
      '#attributes' => [],
    ],
  ];
  return $requirements;
}

/**
 * Implements hook_update_N().
 *
 * Remove unused configuration keys.
 */
function advagg_update_8202() {
  \Drupal::configFactory()
    ->getEditable('advagg.settings')
    ->clear('separator')
    ->clear('advagg_clear_scripts')
    ->save();
}

/**
 * Implements hook_update_N().
 *
 * Remove deprecated configuration.
 */
function advagg_update_8203() {
  \Drupal::configFactory()
    ->getEditable('advagg.settings')
    ->clear('path.convert.absolute_to_relative')
    ->save();
}

/**
 * Implements hook_update_N().
 *
 * Update Configuration options for 8.x-3.x.
 */
function advagg_update_8301() {

  /** @var \Drupal\Core\Config\Config $config */
  $config = \Drupal::configFactory()
    ->getEditable('advagg.settings');
  $config
    ->clear('serializer')
    ->clear('css.ie')
    ->set('cache_level', 2)
    ->clear('debug')
    ->clear('include_base_url')
    ->clear('css.via_js')
    ->clear('root_dir_prefix')
    ->clear('core_groups')
    ->set('js.fix_type', $config
    ->get('js_fix_type'))
    ->set('js.preserve_external', $config
    ->get('js_preserve_external'))
    ->clear('js_preserve_external')
    ->clear('js_fix_type')
    ->set('css.brotli', TRUE)
    ->set('js.brotli', TRUE)
    ->set('immutable', TRUE)
    ->save();
  AssetOptimizer::generateHtaccess('css');
  AssetOptimizer::generateHtaccess('js');
}

/**
 * Implements hook_update_N().
 *
 * Remove advagg_bundler module from system schema.
 */
function advagg_update_8302() {
  \Drupal::database()
    ->delete('key_value')
    ->condition('collection', 'system.schema')
    ->condition('name', 'advagg_bundler')
    ->execute();
}