View source
<?php
namespace Drupal\bootstrap\Plugin\Provider;
use Drupal\bootstrap\Annotation\BootstrapProvider;
use Drupal\bootstrap\Bootstrap;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Annotation\Translation;
class JsDelivr extends ProviderBase {
protected function extractThemes(array $files, $base_url = '') {
$themes = [];
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][] = "{$base_url}/{$path}/bootstrap{$matches[2]}{$min}.{$filetype}";
}
else {
$themes[$theme][$filetype][] = "{$base_url}/{$path}/bootstrap{$matches[2]}{$min}.{$filetype}";
}
}
}
return $themes;
}
public function getAssets($types = NULL) {
$this->assets = [];
$error = !empty($provider['error']);
$version = $error ? Bootstrap::FRAMEWORK_VERSION : $this->theme
->getSetting('cdn_jsdelivr_version');
$theme = $error ? 'bootstrap' : $this->theme
->getSetting('cdn_jsdelivr_theme');
if (isset($this->pluginDefinition['themes'][$version][$theme])) {
$this->assets = $this->pluginDefinition['themes'][$version][$theme];
}
return parent::getAssets($types);
}
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',
]);
$bootstrap = 'twitter-bootstrap';
$bootswatch = 'bootswatch';
$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 (!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;
}
foreach (array_keys($libraries[$bootstrap]) as $version) {
$definition['versions'][$version] = $version;
if (!isset($definition['themes'][$version])) {
$definition['themes'][$version] = [];
}
$definition['themes'][$version] = NestedArray::mergeDeep($definition['themes'][$version], $this
->extractThemes($libraries[$bootstrap][$version], "//cdn.jsdelivr.net/bootstrap/{$version}"));
if (isset($libraries[$bootswatch][$version])) {
$definition['themes'][$version] = NestedArray::mergeDeep($definition['themes'][$version], $this
->extractThemes($libraries[$bootswatch][$version], "//cdn.jsdelivr.net/bootswatch/{$version}"));
}
}
foreach (array_keys($definition['themes']) as $version) {
foreach (array_keys($definition['themes'][$version]) as $theme) {
if ($theme !== 'bootstrap') {
foreach ([
'css',
'js',
] as $type) {
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]);
}
}
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];
}
}
}
}
}
}