file Custom.php

  1. 8.x-3.x src/Plugin/Provider/Custom.php
  2. 7.x-3.x includes/cdn/Custom.php

Namespace

Drupal\bootstrap\Plugin\Provider
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Provider;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\Component\Utility\UrlHelper;
  5. use Symfony\Component\HttpFoundation\Response;
  6. /**
  7. * The "custom" CDN Provider plugin.
  8. *
  9. * @ingroup plugins_provider
  10. *
  11. * @BootstrapProvider(
  12. * id = "custom",
  13. * label = @Translation("Custom"),
  14. * description = @Translation("Allows the use of any CDN by providing the ability to manually specify a repository of available URLs."),
  15. * weight = 100
  16. * )
  17. */
  18. class Custom extends ProviderBase {
  19. /**
  20. * A list of valid Custom CDN URLs.
  21. *
  22. * @var string[]
  23. */
  24. protected $urls;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function discoverCdnAssets($version, $theme = NULL) {
  29. $themes = $this->getCdnThemes($version);
  30. return isset($themes[$theme]) ? $themes[$theme] : new CdnAssets();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function discoverCdnThemes($version) {
  36. return $this->parseAssets($this->getUrls())->getThemes();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function discoverCdnVersions() {
  42. $assets = $this->parseAssets($this->getUrls());
  43. $versions = [];
  44. foreach ($assets->toArray() as $asset) {
  45. if ($version = $asset->getVersion()) {
  46. $versions[$version] = $version;
  47. }
  48. }
  49. return $versions;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function getCdnAssetsCacheData($version = NULL, $theme = NULL) {
  55. return parent::getCdnAssetsCacheData($version, $theme) + ['urls' => $this->getUrls()];
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function getCdnThemesCacheData($version = NULL) {
  61. return parent::getCdnThemesCacheData($version) + ['urls' => $this->getUrls()];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function getCdnVersionsCacheData() {
  67. return parent::getCdnVersionsCacheData() + ['urls' => $this->getUrls()];
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getCacheTtl($type) {
  73. // Because these are static URLs provided by the user, they should just be
  74. // cached forever.
  75. return static::TTL_FOREVER;
  76. }
  77. /**
  78. * Retrieves an array of URLs that should be used in the Custom CDN.
  79. *
  80. * @return array
  81. * An array of URLs.
  82. */
  83. protected function getUrls() {
  84. if (!isset($this->urls)) {
  85. $urls = [];
  86. $filtered = array_filter(explode("\n", $this->theme->getSetting('cdn_custom')));
  87. foreach ($filtered as $url) {
  88. try {
  89. $urls[] = $this->validateUrl($url);
  90. }
  91. catch (\Exception $e) {
  92. // Intentionally do nothing.
  93. }
  94. }
  95. $this->urls = $urls;
  96. }
  97. return $this->urls;
  98. }
  99. /**
  100. * Validates a URL.
  101. *
  102. * @param string $url
  103. * The URL to validate.
  104. *
  105. * @return string
  106. * The passed $url.
  107. */
  108. public function validateUrl($url) {
  109. if (!UrlHelper::isValid($url, TRUE)) {
  110. throw new InvalidCdnUrlException(sprintf('Malformed: %s', $url));
  111. }
  112. $response = Bootstrap::checkUrlIsReachable($url, ['method' => 'option']);
  113. if (($statusCode = $response->getStatusCode()) >= 400) {
  114. throw new InvalidCdnUrlException(sprintf('(%d) %s: %s', $statusCode, Response::$statusTexts[$statusCode], $url), $statusCode);
  115. }
  116. if (!$response->validMimeExtension()) {
  117. throw new InvalidCdnUrlException(sprintf('(%d) Mismatched MIME Type: %s [%s]', $statusCode, $url, $response->getMimeType()), $statusCode);
  118. }
  119. return $url;
  120. }
  121. /**
  122. * Parses URLs and places them in an "assets" like array.
  123. *
  124. * @param string[] $urls
  125. * An array of URLs to process.
  126. *
  127. * @return \Drupal\bootstrap\Plugin\Provider\CdnAssets
  128. * A CdnAssets object.
  129. */
  130. protected function parseAssets(array $urls) {
  131. $assets = new CdnAssets();
  132. foreach ($urls as $url) {
  133. // Skip invalid assets.
  134. if (!CdnAsset::isFileValid($url)) {
  135. continue;
  136. }
  137. $assets->append(new CdnAsset($url));
  138. }
  139. return $assets;
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function processDefinition(array &$definition, $plugin_id) {
  145. // Intentionally left blank so it doesn't trigger a deprecation warning.
  146. }
  147. }

Classes

Name Description
Custom The "custom" CDN Provider plugin.