abstract class CdnBase in Libraries CDN API 8
Class CdnBase.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\libraries_cdn\CdnBase implements CdnBaseInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CdnBase
2 files declare their use of CdnBase
- CdnJs.php in src/
Plugin/ LibrariesCdn/ CdnJs.php - Plugin: CDNJS.
- JsDelivr.php in src/
Plugin/ LibrariesCdn/ JsDelivr.php - Plugin: jsDelivr.
File
- src/
CdnBase.php, line 13 - Class CdnBase.
Namespace
Drupal\libraries_cdnView source
abstract class CdnBase extends PluginBase implements CdnBaseInterface {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration = array()) {
$this->configuration = $configuration;
$this->configuration['available'] = NULL;
}
/**
* {@inheritdoc}
*/
public function getConfiguration($key = NULL) {
if (isset($key) && is_string($key)) {
if (isset($this->configuration[$key])) {
return $this->configuration[$key];
}
else {
return NULL;
}
}
else {
return $this->configuration;
}
}
/**
* {@inheritdoc}
*/
public function setLibrary($library) {
$this->configuration['library'] = $library;
$this->configuration['available'] = NULL;
}
/**
* {@inheritdoc}
*/
public function getLibrary() {
return $this->configuration['library'];
}
/**
* {@inheritdoc}
*/
public function setURL($identifier, $url) {
$this->configuration['urls'][$identifier] = $url;
$this->configuration['available'] = NULL;
}
/**
* {@inheritdoc}
*/
public function getURL($identifier) {
return isset($this->configuration['urls'][$identifier]) ? $this->configuration['urls'][$identifier] : FALSE;
}
/**
* {@inheritdoc}
*/
public function setURLs(array $urls = array()) {
$this->configuration['urls'] = $urls;
$this->configuration['available'] = NULL;
}
/**
* {@inheritdoc}
*/
public function getURLs() {
return $this->configuration['urls'];
}
/**
* {@inheritdoc}
*/
public function setScheme($default = 'http') {
$this->configuration['scheme'] = trim(substr($default, 0, 5));
}
/**
* {@inheritdoc}
*/
public function getScheme($default = 'http') {
return empty($this->configuration['scheme']) ? $default : $this->configuration['scheme'];
}
/**
* {@inheritdoc}
*/
public function request($url) {
return \Drupal::httpClient()
->get($url);
}
/**
* {@inheritdoc}
*/
public function query($url) {
list($scheme, $url) = explode('://', $url, 2);
$request = $this
->request(sprintf('%s://' . $url, $this
->getScheme($scheme), $this
->getLibrary()));
if ($request
->getStatusCode() != 200) {
return array();
}
return json_decode($request
->getBody()
->getContents(), TRUE);
}
/**
* {@inheritdoc}
*/
public function getLatestVersion() {
return $this
->formatData(__FUNCTION__, $this
->getInformation());
}
/**
* {@inheritdoc}
*/
public function search($library) {
$this
->setLibrary($library);
if (!$this
->isAvailable()) {
return array();
}
$data = $this
->formatData(__FUNCTION__, $this
->query($this
->getURL(__FUNCTION__)));
return array_map(function ($v) {
return $v['name'];
}, $data);
}
/**
* {@inheritdoc}
*/
public function isAvailable() {
if (isset($this->configuration['available'])) {
return (bool) $this->configuration['available'];
}
$data = $this
->query($this
->getURL(__FUNCTION__));
if (count($this
->formatData(__FUNCTION__, $data)) !== 0) {
$this->configuration['available'] = TRUE;
return TRUE;
}
else {
$this->configuration['available'] = FALSE;
return FALSE;
}
}
/**
* {@inheritdoc}
*/
public function getVersions() {
if (!$this
->isAvailable()) {
return array();
}
$data = $this
->formatData(__FUNCTION__, $this
->query($this
->getURL(__FUNCTION__)));
return array_filter(array_map(function ($v) {
return isset($v['version']) ? $v['version'] : NULL;
}, $data));
}
/**
* {@inheritdoc}
*/
public function getFiles(array $versions = array()) {
if (!$this
->isAvailable()) {
return array();
}
$data = $this
->formatData(__FUNCTION__, $this
->query($this
->getURL(__FUNCTION__)));
$results = array();
foreach ($data as $asset) {
if (isset($asset['version']) && isset($asset['files']) && is_array($asset['files'])) {
$results[$asset['version']] = $this
->convertFiles($asset['files'], $asset['version']);
}
}
return empty($versions) ? $results : array_intersect_key($results, array_combine(array_values($versions), array_values($versions)));
}
/**
* {@inheritdoc}
*/
public function getInformation() {
return $this
->formatData(__FUNCTION__, $this
->query($this
->getURL(__FUNCTION__)));
}
/**
* {@inheritdoc}
*/
public function convertFiles(array $files, $version) {
$url = $this
->getURL(__FUNCTION__);
return array_map(function ($v) use ($url, $version) {
return sprintf($url, $this
->getLibrary(), $version) . $v;
}, $files);
}
/**
* {@inheritdoc}
*/
public function isLocalAvailable($file, $version) {
return file_exists($this
->getLocalFileName(basename($file), $version));
}
/**
* {@inheritdoc}
*/
public function getLocalFileName($file, $version) {
return $this
->getLocalDirectoryName($version) . '/' . basename($file);
}
/**
* {@inheritdoc}
*/
public function getLocalDirectoryName($version = NULL) {
return implode('/', array(
'public:/',
'libraries',
$this
->getPluginId(),
$this
->getLibrary(),
$version,
));
}
/**
* {@inheritdoc}
*/
public function getLocalCopy(array $versions = array()) {
$assets = $this
->getFiles();
if (!empty($versions)) {
$assets = array_intersect_key($assets, array_combine($versions, $versions));
}
foreach ($assets as $version => $files) {
foreach ($files as $file) {
if (!$this
->isLocalAvailable($file, $version)) {
$directory = $this
->getLocalDirectoryName($version);
$this->drupal7
->file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$this->drupal7
->file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS);
$request = $this
->request($this
->getScheme() . ':' . $file);
if ($request['code'] == 200) {
$this->drupal7
->file_unmanaged_save_data($request['data'], $this
->getLocalFileName($file, $version), FILE_EXISTS_REPLACE);
}
}
}
}
}
/**
* {@inheritdoc}
*/
public function formatData($function, array $data = array()) {
return $data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CdnBase:: |
public | function | ||
CdnBase:: |
public | function |
Return the data to use in each method. Overrides CdnBaseInterface:: |
2 |
CdnBase:: |
public | function |
Return the configuration of the object. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Return all available file(s). Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get library information. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get latest version available of a library. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get the library in use. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Copy a library from the CDN to the local filesystem. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get the local directory name of a library. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get the local file name of a library file. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get the default scheme. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get a particular URL. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Get URLs. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Return all available version(s). Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Check if library is available. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Check if a file is available locally. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Request wrapper for querying a CDN. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Make an HTTP Request. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Perform a search for a library. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Set the configuration of the object. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Set the library to work with. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Set default scheme for an url. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Set a particular URL. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Set URLs. Overrides CdnBaseInterface:: |
|
CdnBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
2 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |