You are here

protected function JsOptimizer::fixType in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.3 src/Asset/JsOptimizer.php \Drupal\advagg\Asset\JsOptimizer::fixType()

Checks for and if found fixes incorrectly set asset types.

Parameters

array $asset: A core single asset definition array.

Overrides AssetOptimizer::fixType

File

src/Asset/JsOptimizer.php, line 38

Class

JsOptimizer
The JavaScript Optimizer.

Namespace

Drupal\advagg\Asset

Code

protected function fixType(array &$asset) {

  // Default asset type to file if not set/invalid.
  if (!in_array($asset['type'], [
    'file',
    'external',
    'settings',
  ])) {
    $asset['type'] = 'file';
  }
  $path = $asset['data'];
  if ($asset['type'] === 'external') {

    // If type is external but path doesn't start with http, https, or //
    // change it to file.
    if (stripos($path, 'http') !== 0 && stripos($path, '//') !== 0) {
      $asset['type'] = 'file';
    }
    elseif (stripos($path, $this->basePath) !== FALSE && !$this->config
      ->get('js.preserve_external')) {
      $asset['type'] = 'file';
      $asset['group'] = JS_LIBRARY;
      $asset['every_page'] = TRUE;
      $asset['weight'] = -40000;
      $asset['data'] = substr($asset['data'], stripos($asset['data'], $this->basePath) + $this->basePathLen);
    }
  }
  elseif ($asset['type'] === 'file' && (stripos($path, 'http') === 0 || stripos($path, '//') === 0)) {
    $asset['type'] = 'external';
  }
}