file DrupalBootstrapStyles.php

Namespace

Drupal\bootstrap\Plugin\Provider
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Provider;
  3. use Drupal\bootstrap\Bootstrap;
  4. /**
  5. * The "drupal_bootstrap_styles" CDN Provider plugin.
  6. *
  7. * @ingroup plugins_provider
  8. *
  9. * @BootstrapProvider(
  10. * id = "drupal_bootstrap_styles",
  11. * label = @Translation("Drupal Bootstrap Styles"),
  12. * description = @Translation("Provides styles that bridge the gap between Drupal and Bootstrap."),
  13. * hidden = true,
  14. * )
  15. */
  16. class DrupalBootstrapStyles extends JsDelivr {
  17. const KNOWN_FALL_BACK_VERSION = '0.0.1';
  18. /**
  19. * Retrieves the latest version of the published NPM package.
  20. *
  21. * While this isn't technically necessary, jsDelivr have been known to not
  22. * favor "version-less" requests. This ensures that a specific and published
  23. * NPM version is always used.
  24. *
  25. * @return string
  26. * The latest version.
  27. */
  28. protected function getLatestVersion() {
  29. $json = $this->request('https://data.jsdelivr.com/v1/package/npm/@unicorn-fail/drupal-bootstrap-styles', ['ttl' => static::TTL_ONE_WEEK])->getData();
  30. return isset($json['tags']['latest']) ? $json['tags']['latest'] : static::KNOWN_FALL_BACK_VERSION;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function getApiAssetsUrlTemplate() {
  36. $latest = $this->getLatestVersion();
  37. return "https://cdn.jsdelivr.net/npm/@unicorn-fail/[email protected]$latest/dist/api.json";
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function getApiVersionsUrlTemplate() {
  43. $latest = $this->getLatestVersion();
  44. return "https://cdn.jsdelivr.net/npm/@unicorn-fail/[email protected]$latest/dist/api.json";
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function getCdnUrlTemplate() {
  50. $latest = $this->getLatestVersion();
  51. return "https://cdn.jsdelivr.net/npm/@unicorn-fail/[email protected]$latest/@file";
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function parseAssets(array $data, $library, $version, CdnAssets $assets = NULL) {
  57. if (!isset($assets)) {
  58. $assets = new CdnAssets();
  59. }
  60. $files = array_filter(isset($data['files']) ? $data['files'] : [], function ($file) use ($library, $version) {
  61. if (strpos($file['name'], '/dist/' . $version . '/' . Bootstrap::PROJECT_BRANCH . '/') !== 0) {
  62. return FALSE;
  63. }
  64. $theme = !!preg_match("`drupal-bootstrap-([\w]+)(\.min)?\.css$`", $file['name']);
  65. return ($library === 'bootstrap' && !$theme) || ($library === 'bootswatch' && $theme);
  66. });
  67. foreach ($files as $file) {
  68. $assets->append($this->getCdnUrl('drupal-bootstrap-styles', $version, !empty($file['symlink']) ? $file['symlink'] : $file['name'], $file));
  69. }
  70. return $assets;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function parseVersions(array $data = []) {
  76. $versions = [];
  77. $files = isset($data['files']) ? $data['files'] : [];
  78. foreach ($files as $file) {
  79. if (preg_match("`^/?dist/(\d+\.\d+\.\d+)/(\d\.x-\d\.x)/drupal-bootstrap-?([\w]+)?(\.min)?\.css$`", $file['name'], $matches)) {
  80. $version = $matches[1];
  81. $branch = $matches[2];
  82. if ($branch === Bootstrap::PROJECT_BRANCH && $this->isValidVersion($version)) {
  83. $versions[$version] = $version;
  84. }
  85. }
  86. }
  87. return $versions;
  88. }
  89. }

Classes

Name Description
DrupalBootstrapStyles The "drupal_bootstrap_styles" CDN Provider plugin.