You are here

class Dummy in Libraries CDN API 7

Class Dummy.

Plugin annotation


@LibrariesCDNPlugin(
 id = "dummy",
 description = "Dummy CDN",
 arguments = {
   "@drupal7"
 }
)

Hierarchy

  • class \Drupal\libraries_cdn\Type\CDNBase extends \Drupal\Component\Plugin\PluginBase implements CDNBaseInterface
    • class \Drupal\libraries_cdn_example_plugin\Plugin\LibrariesCDN\Dummy

Expanded class hierarchy of Dummy

File

modules/libraries_cdn_example_plugin/src/Plugin/LibrariesCDN/Dummy.php, line 25
Plugin: jsDelivr.

Namespace

Drupal\libraries_cdn_example_plugin\Plugin\LibrariesCDN
View source
class Dummy extends CDNBase {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, Drupal7 $drupal7) {
    if (empty($configuration['urls'])) {
      $configuration['urls'] = array();
    }
    $converFileUrl = url('libraries_cdn/dummy/files', array(
      'absolute' => TRUE,
    ));
    $converFileUrl = parse_url($converFileUrl);
    array_shift($converFileUrl);
    $converFileUrl = implode($converFileUrl);
    $configuration['urls'] += array(
      'convertFiles' => '//' . $converFileUrl . '/%s/%s/',
    );
    parent::__construct($configuration, $plugin_id, $plugin_definition, $drupal7);
  }

  /**
   * {@inheritdoc}
   */
  public function formatData($function, array $data = array()) {
    switch ($function) {
      case 'getVersions':
        return isset($data[0]) && isset($data[0]['versions']) ? $data[0]['versions'] : array();
      case 'getFiles':
        return isset($data[0]) && isset($data[0]['assets']) ? $data[0]['assets'] : array();
      case 'getLatestVersion':
        return isset($data['lastversion']) ? $data['lastversion'] : NULL;
      case 'getInformation':
        return isset($data[0]) ? $data[0] : array();
      default:
        return $data;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function search($library) {
    $this
      ->setLibrary($library);
    return array(
      0 => array(
        'name' => drupal_html_class(drupal_clean_css_identifier($library)),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFiles(array $versions = array()) {
    $data = array();
    $keys = array_merge(range(0, 9), range('a', 'z'));
    $count_version = mt_rand(1, 5) + count($versions);
    $file_length = mt_rand(5, 10);
    for ($i = 0; $i < $count_version; $i++) {
      $files = array();
      $major = mt_rand(1, 10);
      $minor = mt_rand(1, 1000);
      for ($j = 0; $j < 10; $j++) {
        $key = '';
        for ($k = 0; $k < $file_length; $k++) {
          $key .= $keys[array_rand($keys)];
        }
        $ext = $j % 2 == 0 ? 'js' : 'css';
        $files[] = $key . '.' . $ext;
      }
      if (isset($versions[$i])) {
        $version = $versions[$i];
      }
      else {
        $version = $major . '.' . $minor;
      }
      $data[] = array(
        'files' => $files,
        'version' => $version,
      );
    }
    $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 getVersions() {
    $data = array();
    $count_version = mt_rand(1, 5);
    for ($i = 0; $i < $count_version; $i++) {
      $major = mt_rand(1, 10);
      $minor = mt_rand(1, 1000);
      $data['versions'][] = $major . '.' . $minor;
    }
    return array_filter($data);
  }

  /**
   * {@inheritdoc}
   */
  public function isAvailable() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getInformation() {
    return array(
      array(
        'name' => 'dummy',
        'mainfile' => 'jquery.min.js',
        'lastversion' => '3.0.0-alpha1',
        'description' => 'jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.',
        'homepage' => 'http://jquery.com/',
        'github' => 'https://github.com/jquery/jquery',
        'author' => 'jQuery Foundation',
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CDNBase::convertFiles public function
CDNBase::getConfiguration public function Return the configuration of the object. Overrides CDNBaseInterface::getConfiguration
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::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::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
Dummy::formatData public function Return the data to use in each method. Overrides CDNBase::formatData
Dummy::getFiles public function Return all available file(s). Overrides CDNBase::getFiles
Dummy::getInformation public function Get library information. Overrides CDNBase::getInformation
Dummy::getVersions public function Return all available version(s). Overrides CDNBase::getVersions
Dummy::isAvailable public function Check if library is available. Overrides CDNBase::isAvailable
Dummy::search public function Perform a search for a library. Overrides CDNBase::search
Dummy::__construct public function Overrides CDNBase::__construct