123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- namespace Drupal\bootstrap\Backport\Plugin\Provider;
- class JsDelivr extends ProviderBase {
- protected $pluginId = 'jsdelivr';
-
- const BASE_API_URL = 'https://data.jsdelivr.com/v1/package/npm';
-
- const BASE_CDN_URL = 'https://cdn.jsdelivr.net/npm';
-
- protected $latestVersion = array();
-
- protected $themes = array();
-
- protected $versions = array();
-
- public function getDescription() {
- return t('<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>', array(
- '!jsdelivr' => 'https://www.jsdelivr.com',
- '!jsdelivr_about' => 'https://www.jsdelivr.com/about',
- '!maxcdn' => 'https://www.maxcdn.com',
- '!cloudflare' => 'https://www.cloudflare.com',
- ));
- }
-
- public function getLabel() {
- return t('jsDelivr');
- }
-
- protected function discoverCdnAssets($version, $theme = 'bootstrap') {
- $themes = $this->getCdnThemes($version);
- return isset($themes[$theme]) ? $themes[$theme] : array();
- }
-
- public function getCdnThemes($version = NULL) {
- if (!isset($version)) {
- $version = $this->getCdnVersion();
- }
- if (!isset($this->themes[$version])) {
- $instance = $this;
- $this->themes[$version] = $this->cacheGet('themes.' . static::escapeDelimiter($version), array(), function ($themes) use ($version, $instance) {
- return $instance->getCdnThemePhp53Callback($themes, $version);
- });
- }
- return $this->themes[$version];
- }
-
- public function getCdnThemePhp53Callback($themes, $version) {
- foreach (array('bootstrap', 'bootswatch') as $package) {
- $mappedVersion = $this->mapVersion($version, $package);
- $files = $this->requestApiV1($package, $mappedVersion);
- $themes = $this->parseThemes($files, $package, $mappedVersion, $themes);
- }
- return $themes;
- }
-
- public function getCdnVersions($package = 'bootstrap') {
- if (!isset($this->versions[$package])) {
- $instance = $this;
- $this->versions[$package] = $this->cacheGet("versions.$package", array(), function ($versions) use ($package, $instance) {
- return $instance->getCdnVersionsPhp53Callback($versions, $package);
- });
- }
- return $this->versions[$package];
- }
-
- public function getCdnVersionsPhp53Callback($versions, $package) {
- $json = $this->requestApiV1($package) + array('versions' => array());
- foreach ($json['versions'] as $version) {
-
- if (!preg_match('/^' . substr(BOOTSTRAP_VERSION, 0, 1) . '\.\d+\.\d+$/', $version)) {
- continue;
- }
- $versions[$version] = $version;
- }
- return $versions;
- }
-
- protected function mapVersion($version, $package = NULL) {
-
-
-
-
- if ($package === 'bootswatch') {
- switch ($version) {
-
- case '3.4.1':
- $version = '3.4.0';
- break;
-
- case '3.1.1':
- $version = '3.2.0';
- break;
- }
- }
- return $version;
- }
-
- protected function parseFiles(array $json) {
-
- if (!isset($json['files']) || !is_array($json['files'])) {
- return array();
- }
- $files = array();
- foreach ($json['files'] as $file) {
-
- if (preg_match('`^/2|/bower_components`', $file['name'], $matches)) {
- continue;
- }
- preg_match('`([^/]*)/bootstrap(-theme)?(\.min)?\.(js|css)$`', $file['name'], $matches);
- if (!empty($matches[1]) && !empty($matches[4])) {
- $files[] = $file['name'];
- }
- }
- return $files;
- }
-
- protected function parseThemes(array $files, $package, $version, array $themes = array()) {
- $baseUrl = static::BASE_CDN_URL . "/$package@$version";
- foreach ($files as $file) {
- preg_match('`([^/]*)/bootstrap(-theme)?(\.min)?\.(js|css)$`', $file, $matches);
- if (!empty($matches[1]) && !empty($matches[4])) {
- $path = $matches[1];
- $min = $matches[3];
- $filetype = $matches[4];
-
- if ($path === 'css' || $path === 'js') {
- $theme = 'bootstrap';
- $title = (string) t('Bootstrap');
- }
- else {
- $theme = $path;
- $title = ucfirst($path);
- }
- if ($matches[2]) {
- $theme = 'bootstrap_theme';
- $title = (string) t('Bootstrap Theme');
- }
- $themes[$theme]['title'] = $title;
- if ($min) {
- $themes[$theme]['min'][$filetype][] = "$baseUrl/" . ltrim($file, '/');
- }
- else {
- $themes[$theme][$filetype][] = "$baseUrl/" . ltrim($file, '/');
- }
- }
- }
-
- foreach (array_keys($themes) as $theme) {
-
-
-
- foreach (array('css', 'js') as $type) {
- if (!isset($themes[$theme][$type]) && isset($themes[$theme]['min'][$type])) {
- $themes[$theme][$type] = $themes[$theme]['min'][$type];
- }
- }
-
- if ($theme === 'bootstrap_theme') {
- if (isset($themes['bootstrap']['css'])) {
- $themes[$theme]['css'] = array_unique(array_merge($themes['bootstrap']['css'], isset($themes[$theme]['css']) ? $themes[$theme]['css'] : array()));
- }
- if (isset($themes['bootstrap']['min']['css'])) {
- $themes[$theme]['min']['css'] = array_unique(array_merge($themes['bootstrap']['min']['css'], isset($themes[$theme]['min']['css']) ? $themes[$theme]['min']['css'] : array()));
- }
- }
-
- if (!isset($themes[$theme]['js']) && isset($themes['bootstrap']['js'])) {
- $themes[$theme]['js'] = $themes['bootstrap']['js'];
- }
- if (!isset($themes[$theme]['min']['js']) && isset($themes['bootstrap']['min']['js'])) {
- $themes[$theme]['min']['js'] = $themes['bootstrap']['min']['js'];
- }
- }
- return $themes;
- }
-
- protected function requestApiV1($package, $version = NULL) {
- $url = static::BASE_API_URL . "/$package";
-
- if (!$version) {
- return $this->requestJson($url);
- }
- $json = $this->requestJson("$url@$version/flat");
-
- if (!$json && $package === 'bootstrap') {
- return array(
- '/dist/css/bootstrap.css',
- '/dist/js/bootstrap.js',
- '/dist/css/bootstrap.min.css',
- '/dist/js/bootstrap.min.js',
- );
- }
-
- return $this->parseFiles($json);
- }
- }
|