4 * Contains \Drupal\bootstrap\Plugin\Provider\ProviderBase.
7 namespace Drupal\bootstrap\Plugin\Provider;
9 use Drupal\bootstrap\Plugin\PluginBase;
10 use Drupal\bootstrap\Plugin\ProviderManager;
11 use Drupal\Component\Serialization\Json;
12 use GuzzleHttp\Exception\RequestException;
13 use GuzzleHttp\Psr7\Request;
14 use GuzzleHttp\Psr7\Response;
17 * CDN provider base class.
19 * @ingroup plugins_provider
21 class ProviderBase extends PluginBase implements ProviderInterface {
24 * The currently set assets.
28 protected $assets = [];
31 * The versions supplied by the CDN provider.
40 public function getApi() {
41 return $this->pluginDefinition['api'];
47 public function getAssets($types = NULL) {
48 // Immediately return if there are no assets.
55 // If no type is set, return all CSS and JS.
57 $types = ['css', 'js'];
59 $types = is_array($types) ? $types : [$types];
61 // Ensure default arrays exist for the requested types.
62 foreach ($types as $type) {
66 // Retrieve the system performance config.
67 $config = \Drupal::config('system.performance');
69 // Iterate over each type.
70 foreach ($types as $type) {
71 $min = $config->get("$type.preprocess");
72 $files = $min && isset($this->assets['min'][$type]) ? $this->assets['min'][$type] : (isset($this->assets[$type]) ? $this->assets[$type] : []);
73 foreach ($files as $asset) {
79 // CSS library assets use "SMACSS" categorization, assign it to "base".
80 if ($type === 'css') {
81 $assets[$type]['base'][$asset] = $data;
84 $assets[$type][$asset] = $data;
89 return count($types) === 1 ? $assets[$types[0]] : $assets;
95 public function getDescription() {
96 return $this->pluginDefinition['description'];
102 public function getLabel() {
103 return $this->pluginDefinition['label'] ?: $this->getPluginId();
109 public function getThemes() {
110 return $this->pluginDefinition['themes'];
116 public function getVersions() {
117 return $this->pluginDefinition['versions'];
123 public function hasError() {
124 return $this->pluginDefinition['error'];
130 public function isImported() {
131 return $this->pluginDefinition['imported'];
137 public function processDefinition(array &$definition, $plugin_id) {
138 $provider_path = ProviderManager::FILE_PATH;
139 file_prepare_directory($provider_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
142 if ($api = $this->getApi()) {
143 // Use manually imported API data, if it exists.
144 if (file_exists("$provider_path/$plugin_id.json") && ($imported_data = file_get_contents("$provider_path/$plugin_id.json"))) {
145 $definition['imported'] = TRUE;
146 $response = new Response(200, [], $imported_data);
148 // Otherwise, attempt to request API data if the provider has specified
149 // an "api" URL to use.
151 $client = \Drupal::httpClient();
152 $request = new Request('GET', $api);
154 $response = $client->send($request);
156 catch (RequestException $e) {
157 $response = new Response(400);
160 $contents = $response->getBody(TRUE)->getContents();
161 $json = Json::decode($contents) ?: [];
162 $this->processApi($json, $definition);
169 public function processApi(array $json, array &$definition) {}