You are here

abstract class CdnBase in Libraries CDN API 8

Class CdnBase.

Hierarchy

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_cdn
View 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

Namesort descending Modifiers Type Description Overrides
CdnBase::convertFiles public function
CdnBase::formatData public function Return the data to use in each method. Overrides CdnBaseInterface::formatData 2
CdnBase::getConfiguration public function Return the configuration of the object. Overrides CdnBaseInterface::getConfiguration
CdnBase::getFiles public function Return all available file(s). Overrides CdnBaseInterface::getFiles
CdnBase::getInformation public function Get library information. Overrides CdnBaseInterface::getInformation
CdnBase::getLatestVersion public function Get latest version available of a library. Overrides CdnBaseInterface::getLatestVersion
CdnBase::getLibrary public function Get the library in use. Overrides CdnBaseInterface::getLibrary
CdnBase::getLocalCopy public function Copy a library from the CDN to the local filesystem. Overrides CdnBaseInterface::getLocalCopy
CdnBase::getLocalDirectoryName public function Get the local directory name of a library. Overrides CdnBaseInterface::getLocalDirectoryName
CdnBase::getLocalFileName public function Get the local file name of a library file. Overrides CdnBaseInterface::getLocalFileName
CdnBase::getScheme public function Get the default scheme. Overrides CdnBaseInterface::getScheme
CdnBase::getURL public function Get a particular URL. Overrides CdnBaseInterface::getURL
CdnBase::getURLs public function Get URLs. Overrides CdnBaseInterface::getURLs
CdnBase::getVersions public function Return all available version(s). Overrides CdnBaseInterface::getVersions
CdnBase::isAvailable public function Check if library is available. Overrides CdnBaseInterface::isAvailable
CdnBase::isLocalAvailable public function Check if a file is available locally. Overrides CdnBaseInterface::isLocalAvailable
CdnBase::query public function Request wrapper for querying a CDN. Overrides CdnBaseInterface::query
CdnBase::request public function Make an HTTP Request. Overrides CdnBaseInterface::request
CdnBase::search public function Perform a search for a library. Overrides CdnBaseInterface::search
CdnBase::setConfiguration public function Set the configuration of the object. Overrides CdnBaseInterface::setConfiguration
CdnBase::setLibrary public function Set the library to work with. Overrides CdnBaseInterface::setLibrary
CdnBase::setScheme public function Set default scheme for an url. Overrides CdnBaseInterface::setScheme
CdnBase::setURL public function Set a particular URL. Overrides CdnBaseInterface::setURL
CdnBase::setURLs public function Set URLs. Overrides CdnBaseInterface::setURLs
CdnBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 2
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.