You are here

abstract class CDNBase in Libraries CDN API 7

Class CDNBase.

Hierarchy

Expanded class hierarchy of CDNBase

3 files declare their use of CDNBase
CDNJS.php in src/Plugin/LibrariesCDN/CDNJS.php
Plugin: CDNJS.
Dummy.php in modules/libraries_cdn_example_plugin/src/Plugin/LibrariesCDN/Dummy.php
Plugin: jsDelivr.
JSDelivr.php in src/Plugin/LibrariesCDN/JSDelivr.php
Plugin: jsDelivr.

File

src/Type/CDNBase.php, line 14
Class CDNBase.

Namespace

Drupal\libraries_cdn\Type
View source
abstract class CDNBase extends PluginBase implements CDNBaseInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Drupal7 $drupal7) {
    $this->drupal7 = $drupal7;
    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 (array) $this->drupal7
      ->drupal_http_request($url, (array) $this
      ->getConfiguration('request'));
  }

  /**
   * {@inheritdoc}
   */
  public function query($url) {
    list($scheme, $url) = explode('://', $url, 2);
    $request = $this
      ->request(sprintf('%s://' . $url, $this
      ->getScheme($scheme), $this
      ->getLibrary()));
    if ($request['code'] != 200) {
      return array();
    }
    $data = json_decode($request['data'], TRUE);
    return is_array($data) ? $data : array();
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestVersion() {
    return $this
      ->formatData(__FUNCTION__, $this
      ->getInformation());
  }

  /**
   * {@inheritdoc}
   */
  public function search($library) {
    $this
      ->setLibrary($library);
    $data = $this
      ->formatData(__FUNCTION__, $this
      ->query($this
      ->getURL(__FUNCTION__)));
    return $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($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(), array $indexes = array()) {
    $assets = $this
      ->getFiles($versions);
    if (!empty($versions)) {
      $assets = array_intersect_key($assets, array_combine($versions, $versions));
    }
    foreach ($assets as $version => $files) {
      if (empty($indexes)) {
        $indexes = array_keys(array_values($files));
      }
      foreach ($files as $index => $file) {
        if (!$this
          ->isLocalAvailable($file, $version)) {
          if (in_array($index, $indexes)) {
            $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 3
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 1
CDNBase::getInformation public function Get library information. Overrides CDNBaseInterface::getInformation 1
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 2
CDNBase::isAvailable public function Check if library is available. Overrides CDNBaseInterface::isAvailable 1
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 1
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 3