Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Provider / ProviderBase.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Provider\ProviderBase.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Provider;
8
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;
15
16 /**
17  * CDN provider base class.
18  *
19  * @ingroup plugins_provider
20  */
21 class ProviderBase extends PluginBase implements ProviderInterface {
22
23   /**
24    * The currently set assets.
25    *
26    * @var array
27    */
28   protected $assets = [];
29
30   /**
31    * The versions supplied by the CDN provider.
32    *
33    * @var array
34    */
35   protected $versions;
36
37   /**
38    * {@inheritdoc}
39    */
40   public function getApi() {
41     return $this->pluginDefinition['api'];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getAssets($types = NULL) {
48     // Immediately return if there are no assets.
49     if (!$this->assets) {
50       return $this->assets;
51     }
52
53     $assets = [];
54
55     // If no type is set, return all CSS and JS.
56     if (!isset($types)) {
57       $types = ['css', 'js'];
58     }
59     $types = is_array($types) ? $types : [$types];
60
61     // Ensure default arrays exist for the requested types.
62     foreach ($types as $type) {
63       $assets[$type] = [];
64     }
65
66     // Retrieve the system performance config.
67     $config = \Drupal::config('system.performance');
68
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) {
74         $data = [
75           'data' => $asset,
76           'type' => 'external',
77           'weight' => -19.999,
78         ];
79         // CSS library assets use "SMACSS" categorization, assign it to "base".
80         if ($type === 'css') {
81           $assets[$type]['base'][$asset] = $data;
82         }
83         else {
84           $assets[$type][$asset] = $data;
85         }
86       }
87     }
88
89     return count($types) === 1 ? $assets[$types[0]] : $assets;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getDescription() {
96     return $this->pluginDefinition['description'];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getLabel() {
103     return $this->pluginDefinition['label'] ?: $this->getPluginId();
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getThemes() {
110     return $this->pluginDefinition['themes'];
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function getVersions() {
117     return $this->pluginDefinition['versions'];
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function hasError() {
124     return $this->pluginDefinition['error'];
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function isImported() {
131     return $this->pluginDefinition['imported'];
132   }
133
134   /**
135    * {@inheritdoc}
136    */
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);
140
141     // Process API data.
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);
147       }
148       // Otherwise, attempt to request API data if the provider has specified
149       // an "api" URL to use.
150       else {
151         $client = \Drupal::httpClient();
152         $request = new Request('GET', $api);
153         try {
154           $response = $client->send($request);
155         }
156         catch (RequestException $e) {
157           $response = new Response(400);
158         }
159       }
160       $contents = $response->getBody(TRUE)->getContents();
161       $json = Json::decode($contents) ?: [];
162       $this->processApi($json, $definition);
163     }
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function processApi(array $json, array &$definition) {}
170
171 }