Unverified Commit 21004416 authored by Mark Halliwell's avatar Mark Halliwell
Browse files

Issue #3031415 by markcarver: Reduce time it takes to populate CDN assets from cold cache request

parent ab31570e
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -745,28 +745,19 @@ function bootstrap_element_smart_description(array &$element, array &$target = N
 *   $assets = bootstrap_get_cdn_assets($type, $provider, $theme);
 *
 *   // After.
 *   use Drupal\bootstrap\Bootstrap;
 *   $theme = Bootstrap::getTheme($theme);
 *   $assets = [];
 *   if ($provider = $theme->getProvider($provider)) {
 *     $assets = $provider->getAssets($type);
 *   }
 *   use Drupal\bootstrap\Plugin\ProviderManager;
 *   $assets = ProviderManager::load($theme, $provider)->getAssets($type);
 * @endcode
 *
 * @see \Drupal\bootstrap\Plugin\Provider\Custom::getAssets()
 * @see \Drupal\bootstrap\Plugin\Provider\JsDelivr::getAssets()
 * @see \Drupal\bootstrap\Plugin\Provider\ProviderBase::getAssets()
 * @see \Drupal\bootstrap\Plugin\Provider\ProviderInterface::getAssets()
 * @see \Drupal\bootstrap\Theme::getProvider()
 * @see \Drupal\bootstrap\Bootstrap::getTheme()
 * @see \Drupal\bootstrap\Plugin\ProviderManager::load()
 */
function bootstrap_get_cdn_assets($type = NULL, $provider = NULL, $theme = NULL) {
  Bootstrap::deprecated();
  $assets = [];
  if ($provider = Bootstrap::getTheme($theme)->getProvider($provider)) {
    $assets = $provider->getAssets($type);
  }
  return $assets;
  return ProviderManager::load($theme, $provider)->getAssets($type);
}

/**
+16 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ class BootstrapProvider extends Plugin {
   * An API URL used to retrieve data for the provider.
   *
   * @var string
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $api = '';

@@ -32,6 +34,8 @@ class BootstrapProvider extends Plugin {
   * An array of CSS assets.
   *
   * @var array
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $css = [];

@@ -46,6 +50,8 @@ class BootstrapProvider extends Plugin {
   * A flag determining whether or not the API request has failed.
   *
   * @var bool
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $error = FALSE;

@@ -53,6 +59,8 @@ class BootstrapProvider extends Plugin {
   * A flag determining whether or not data has been manually imported.
   *
   * @var bool
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $imported = FALSE;

@@ -60,6 +68,8 @@ class BootstrapProvider extends Plugin {
   * An array of JavaScript assets.
   *
   * @var array
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $js = [];

@@ -74,6 +84,8 @@ class BootstrapProvider extends Plugin {
   * An associative array of minified CSS and JavaScript assets.
   *
   * @var array
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $min = ['css' => [], 'js' => []];

@@ -81,6 +93,8 @@ class BootstrapProvider extends Plugin {
   * An array of themes supported by the provider.
   *
   * @var array
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $themes = [];

@@ -88,6 +102,8 @@ class BootstrapProvider extends Plugin {
   * An array of versions supported by the provider.
   *
   * @var array
   *
   * @deprecated in 8.x-3.18, will be removed in a future release.
   */
  protected $versions = [];

+22 −11
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;

/**
 * The primary class for the Drupal Bootstrap base theme.
@@ -415,15 +416,15 @@ class Bootstrap {

  /**
   * Logs and displays a warning about a deprecated function/method being used.
   *
   * @param bool $show_message
   *   Flag indicating whether to show a message to the user. If TRUE, it will
   *   force showing the message. If FALSE, it will only log the message. If
   *   not set, showing the message will be determined by whether the current
   *   theme has suppressed showing deprecated warnings.
   */
  public static function deprecated() {
    // Log backtrace.
  public static function deprecated($show_message = NULL) {
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
    \Drupal::logger('bootstrap')->warning('<pre><code>' . print_r($backtrace, TRUE) . '</code></pre>');

    if (!self::getTheme()->getSetting('suppress_deprecated_warnings')) {
      return;
    }

    // Extrapolate the caller.
    $caller = $backtrace[1];
@@ -432,11 +433,21 @@ class Bootstrap {
      $parts = explode('\\', $caller['class']);
      $class = array_pop($parts) . '::';
    }
    drupal_set_message(t('The following function(s) or method(s) have been deprecated, please check the logs for a more detailed backtrace on where these are being invoked. Click on the function or method link to search the documentation site for a possible replacement or solution.'), 'warning');
    drupal_set_message(t('<a href=":url" target="_blank">@title</a>.', [

    $message = t('The following function(s) or method(s) have been deprecated, please check the logs for a more detailed backtrace on where these are being invoked. Click on the function or method link to search the documentation site for a possible replacement or solution: <a href=":url" target="_blank">@title</a>', [
      ':url' => self::apiSearchUrl($class . $caller['function']),
      '@title' => ($class ? $caller['class'] . $caller['type'] : '') . $caller['function'] . '()',
    ]), 'warning');
      '@title' => ($class ? $caller['class'] . '::' : '') . $caller['function'] . '()',
    ]);

    if ($show_message || (!isset($show_message) && !self::getTheme()->getSetting('suppress_deprecated_warnings', FALSE))) {
      drupal_set_message($message, 'warning');
    }

    // Log message and accompanying backtrace.
    \Drupal::logger('bootstrap')->warning('<div>@message</div><pre><code>@backtrace</code></pre>', [
      '@message' => $message,
      '@backtrace' => Markup::create(print_r($backtrace, TRUE)),
    ]);
  }

  /**
+3 −36
Original line number Diff line number Diff line
@@ -2,9 +2,7 @@

namespace Drupal\bootstrap\Plugin\Alter;

use Drupal\bootstrap\Bootstrap;
use Drupal\bootstrap\Plugin\PluginBase;
use Drupal\Component\Utility\NestedArray;

/**
 * Implements hook_library_info_alter().
@@ -37,40 +35,9 @@ class LibraryInfo extends PluginBase implements AlterInterface {
        unset($libraries['livereload']['js']['livereload.js']);
      }

      // Retrieve the theme's CDN provider and assets.
      $provider = $this->theme->getProvider();
      $assets = $provider ? $provider->getAssets() : [];

      // Immediately return if there is no provider or assets.
      if (!$provider || !$assets) {
        return;
      }

      // Merge the assets into the library info.
      $libraries['framework'] = NestedArray::mergeDeepArray([$assets, $libraries['framework']], TRUE);

      // Add a specific version and theme CSS overrides file.
      // @todo This should be retrieved by the Provider API.
      $version = $this->theme->getSetting('cdn_' . $provider->getPluginId() . '_version') ?: Bootstrap::FRAMEWORK_VERSION;
      $libraries['framework']['version'] = $version;
      $provider_theme = $this->theme->getSetting('cdn_' . $provider->getPluginId() . '_theme') ?: 'bootstrap';
      $provider_theme = $provider_theme === 'bootstrap' || $provider_theme === 'bootstrap_theme' ? '' : "-$provider_theme";

      foreach ($this->theme->getAncestry(TRUE) as $ancestor) {
        $overrides = $ancestor->getPath() . "/css/$version/overrides$provider_theme.min.css";
        if (file_exists($overrides)) {
          // Since this uses a relative path to the ancestor from DRUPAL_ROOT,
          // we must prepend the entire path with forward slash (/) so it
          // doesn't prepend the active theme's path.
          $overrides = "/$overrides";

          // The overrides file must also be stored in the "base" category so
          // it isn't added after any potential sub-theme's "theme" category.
          // There's no weight, so it will be added after the provider's assets.
          // @see https://www.drupal.org/node/2770613
          $libraries['framework']['css']['base'][$overrides] = [];
          break;
        }
      // Alter the framework library based on currently set CDN provider.
      if ($provider = $this->theme->getProvider()) {
        $provider->alterFrameworkLibrary($libraries['framework']);
      }
    }
    // Core replacements.
+11 −0
Original line number Diff line number Diff line
@@ -119,6 +119,7 @@ class SystemThemeSettings extends FormBase implements FormInterface {
    $cache_tags = [];
    $save = FALSE;
    $settings = $theme->settings();
    $rebuild_cdn_assets = FALSE;

    // Iterate over all setting plugins and manually save them since core's
    // process is severely limiting and somewhat broken.
@@ -147,6 +148,11 @@ class SystemThemeSettings extends FormBase implements FormInterface {
        // Retrieve the cache tags for the setting.
        $cache_tags = array_unique(array_merge($setting->getCacheTags()));

        // A CDN setting has changed, flag that CDN assets should be rebuilt.
        if (strpos($name, 'cdn') === 0) {
          $rebuild_cdn_assets = TRUE;
        }

        // Flag the save.
        $save = TRUE;
      }
@@ -157,6 +163,11 @@ class SystemThemeSettings extends FormBase implements FormInterface {

    // Save the settings, if needed.
    if ($save) {
      // Remove any cached CDN assets so they can be rebuilt.
      if ($rebuild_cdn_assets) {
        $settings->clear('cdn_cache');
      }

      $settings->save();

      // Invalidate necessary cache tags.
Loading