You are here

public function JsDelivr::processApi in Express 8

Processes the provider plugin definition upon discovery.

Parameters

array $json: The JSON data retrieved from the API request.

array $definition: The provider plugin definition.

Overrides ProviderBase::processApi

File

themes/contrib/bootstrap/src/Plugin/Provider/JsDelivr.php, line 102
Contains \Drupal\bootstrap\Plugin\Provider\JsDelivr.

Class

JsDelivr
The "jsdelivr" CDN provider plugin.

Namespace

Drupal\bootstrap\Plugin\Provider

Code

public function processApi(array $json, array &$definition) {
  $definition['description'] = t('<p style="background:#EB4C36"><a href=":jsdelivr" target="_blank"><img src="http://www.jsdelivr.com/img/logo-34.png" alt="jsDelivr Logo"/></a></p><p><a href=":jsdelivr" target="_blank">jsDelivr</a> is a free multi-CDN infrastructure that uses <a href=":maxcdn" target="_blank">MaxCDN</a>, <a href=":cloudflare" target="_blank">Cloudflare</a> and many others to combine their powers for the good of the open source community... <a href=":jsdelivr_about" target="_blank">read more</a></p>', [
    ':jsdelivr' => 'http://www.jsdelivr.com',
    ':jsdelivr_about' => 'http://www.jsdelivr.com/about',
    ':maxcdn' => 'http://www.maxcdn.com',
    ':cloudflare' => 'http://www.cloudflare.com',
  ]);

  // Expected library names from jsDelivr API v1. Must use "twitter-bootstrap"
  // instead of "bootstrap" (which is just a directory alias).
  // @see https://www.drupal.org/node/2504343
  // @see https://github.com/jsdelivr/api/issues/94
  $bootstrap = 'twitter-bootstrap';
  $bootswatch = 'bootswatch';

  // Extract the raw asset files from the JSON data for each framework.
  $libraries = [];
  if ($json) {
    foreach ($json as $data) {
      if ($data['name'] === $bootstrap || $data['name'] === $bootswatch) {
        foreach ($data['assets'] as $asset) {
          if (preg_match('/^' . substr(Bootstrap::FRAMEWORK_VERSION, 0, 1) . '\\.\\d\\.\\d$/', $asset['version'])) {
            $libraries[$data['name']][$asset['version']] = $asset['files'];
          }
        }
      }
    }
  }

  // If the main bootstrap library could not be found, then provide defaults.
  if (!isset($libraries[$bootstrap])) {
    $definition['error'] = TRUE;
    $definition['versions'][Bootstrap::FRAMEWORK_VERSION] = Bootstrap::FRAMEWORK_VERSION;
    $definition['themes'][Bootstrap::FRAMEWORK_VERSION] = [
      'bootstrap' => [
        'title' => (string) t('Bootstrap'),
        'css' => [
          '//cdn.jsdelivr.net/bootstrap/' . Bootstrap::FRAMEWORK_VERSION . '/css/bootstrap.css',
        ],
        'js' => [
          '//cdn.jsdelivr.net/bootstrap/' . Bootstrap::FRAMEWORK_VERSION . '/js/bootstrap.js',
        ],
        'min' => [
          'css' => [
            '//cdn.jsdelivr.net/bootstrap/' . Bootstrap::FRAMEWORK_VERSION . '/css/bootstrap.min.css',
          ],
          'js' => [
            '//cdn.jsdelivr.net/bootstrap/' . Bootstrap::FRAMEWORK_VERSION . '/js/bootstrap.min.js',
          ],
        ],
      ],
    ];
    return;
  }

  // Populate the provider array with the versions and themes available.
  foreach (array_keys($libraries[$bootstrap]) as $version) {
    $definition['versions'][$version] = $version;
    if (!isset($definition['themes'][$version])) {
      $definition['themes'][$version] = [];
    }

    // Extract Bootstrap themes.
    $definition['themes'][$version] = NestedArray::mergeDeep($definition['themes'][$version], $this
      ->extractThemes($libraries[$bootstrap][$version], "//cdn.jsdelivr.net/bootstrap/{$version}"));

    // Extract Bootswatch themes.
    if (isset($libraries[$bootswatch][$version])) {
      $definition['themes'][$version] = NestedArray::mergeDeep($definition['themes'][$version], $this
        ->extractThemes($libraries[$bootswatch][$version], "//cdn.jsdelivr.net/bootswatch/{$version}"));
    }
  }

  // Post process the themes to fill in any missing assets.
  foreach (array_keys($definition['themes']) as $version) {
    foreach (array_keys($definition['themes'][$version]) as $theme) {

      // Some themes actually require Bootstrap framework assets to still
      // function properly.
      if ($theme !== 'bootstrap') {
        foreach ([
          'css',
          'js',
        ] as $type) {

          // Bootswatch themes include the Bootstrap framework in their CSS.
          // Skip the CSS portions.
          if ($theme !== 'bootstrap_theme' && $type === 'css') {
            continue;
          }
          if (!isset($definition['themes'][$version][$theme][$type]) && !empty($definition['themes'][$version]['bootstrap'][$type])) {
            $definition['themes'][$version][$theme][$type] = [];
          }
          $definition['themes'][$version][$theme][$type] = NestedArray::mergeDeep($definition['themes'][$version]['bootstrap'][$type], $definition['themes'][$version][$theme][$type]);
          if (!isset($definition['themes'][$version][$theme]['min'][$type]) && !empty($definition['themes'][$version]['bootstrap']['min'][$type])) {
            $definition['themes'][$version][$theme]['min'][$type] = [];
          }
          $definition['themes'][$version][$theme]['min'][$type] = NestedArray::mergeDeep($definition['themes'][$version]['bootstrap']['min'][$type], $definition['themes'][$version][$theme]['min'][$type]);
        }
      }

      // Some themes do not have a non-minified version, clone them to the
      // "normal" css/js arrays to ensure that the theme still loads if
      // aggregation (minification) is disabled.
      foreach ([
        'css',
        'js',
      ] as $type) {
        if (!isset($definition['themes'][$version][$theme][$type]) && isset($definition['themes'][$version][$theme]['min'][$type])) {
          $definition['themes'][$version][$theme][$type] = $definition['themes'][$version][$theme]['min'][$type];
        }
      }
    }
  }
}