You are here

class CacheDecoratedDataProvider in RESTful 7.2

Class CacheDecoratedDataProvider.

@package Drupal\restful\Plugin\resource\DataProvider

Hierarchy

Expanded class hierarchy of CacheDecoratedDataProvider

2 files declare their use of CacheDecoratedDataProvider
CacheDecoratedResource.php in src/Plugin/resource/Decorators/CacheDecoratedResource.php
Contains \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource
RestfulReferenceTestCase.test in tests/RestfulReferenceTestCase.test
Contains RestfulReferenceTestCase.

File

src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php, line 23
Contains \Drupal\restful\Plugin\resource\DataProvider\CacheDecoratedDataProvider.

Namespace

Drupal\restful\Plugin\resource\DataProvider
View source
class CacheDecoratedDataProvider implements CacheDecoratedDataProviderInterface, ExplorableDecoratorInterface {

  /**
   * The decorated object.
   *
   * @var DataProviderInterface
   */
  protected $subject;

  /**
   * The cache controller to interact with the cache backed.
   *
   * @var \DrupalCacheInterface
   */
  protected $cacheController;

  /**
   * Constructs a CacheDecoratedDataProvider object.
   *
   * @param DataProviderInterface $subject
   *   The data provider to add caching to.
   * @param \DrupalCacheInterface $cache_controller
   *   The cache controller to add the cache.
   */
  public function __construct(DataProviderInterface $subject, \DrupalCacheInterface $cache_controller) {
    $this->subject = $subject;
    $this->cacheController = $cache_controller;
  }

  /**
   * {@inheritdoc}
   */
  public static function isNestedField($field_name) {
    return DataProvider::isNestedField($field_name);
  }

  /**
   * {@inheritdoc}
   */
  public static function processFilterInput($filter, $public_field) {
    return DataProvider::processFilterInput($filter, $public_field);
  }

  /**
   * {@inheritdoc}
   */
  public function discover($path = NULL) {
    return $this->subject
      ->discover($path);
  }

  /**
   * {@inheritdoc}
   */
  public function getRange() {
    return $this->subject
      ->getRange();
  }

  /**
   * {@inheritdoc}
   */
  public function setRange($range) {
    return $this->subject
      ->setRange($range);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccount() {
    return $this->subject
      ->getAccount();
  }

  /**
   * {@inheritdoc}
   */
  public function setAccount($account) {
    $this->subject
      ->setAccount($account);
  }

  /**
   * {@inheritdoc}
   */
  public function getRequest() {
    return $this->subject
      ->getRequest();
  }

  /**
   * {@inheritdoc}
   */
  public function setRequest(RequestInterface $request) {
    $this->subject
      ->setRequest($request);
  }

  /**
   * {@inheritdoc}
   */
  public function getLangCode() {
    return $this->subject
      ->getLangCode();
  }

  /**
   * {@inheritdoc}
   */
  public function setLangCode($langcode) {
    $this->subject
      ->setLangCode($langcode);
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions() {
    return $this->subject
      ->getOptions();
  }

  /**
   * {@inheritdoc}
   */
  public function addOptions(array $options) {
    $this->subject
      ->addOptions($options);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheFragments($identifier) {
    return $this->subject
      ->getCacheFragments($identifier);
  }

  /**
   * {@inheritdoc}
   */
  public function index() {

    // TODO: This is duplicating the code from DataProvider::index
    $ids = $this
      ->getIndexIds();
    return $this
      ->viewMultiple($ids);
  }

  /**
   * {@inheritdoc}
   */
  public function getIndexIds() {
    return $this->subject
      ->getIndexIds();
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    return $this->subject
      ->count();
  }

  /**
   * {@inheritdoc}
   */
  public function create($object) {
    return $this->subject
      ->create($object);
  }

  /**
   * {@inheritdoc}
   */
  public function view($identifier) {
    $resource_field_collection = $this->subject
      ->view($identifier);
    if (!$resource_field_collection instanceof ResourceFieldCollectionInterface) {
      return NULL;
    }
    $resource_field_collection
      ->setContext('cache_fragments', $this
      ->getCacheFragments($identifier));
    return $resource_field_collection;
  }

  /**
   * {@inheritdoc}
   */
  public function viewMultiple(array $identifiers) {
    $return = array();

    // If no IDs were requested, we should not throw an exception in case an
    // entity is un-accessible by the user.
    foreach ($identifiers as $identifier) {
      try {
        $row = $this
          ->view($identifier);
      } catch (InaccessibleRecordException $e) {
        $row = NULL;
      }
      $return[] = $row;
    }
    return array_values(array_filter($return));
  }

  /**
   * {@inheritdoc}
   */
  public function update($identifier, $object, $replace = TRUE) {
    $this
      ->clearRenderedCache($this
      ->getCacheFragments($identifier));
    return $this->subject
      ->update($identifier, $object, $replace);
  }

  /**
   * {@inheritdoc}
   */
  public function remove($identifier) {
    $this
      ->clearRenderedCache($this
      ->getCacheFragments($identifier));
    $this->subject
      ->remove($identifier);
  }

  /**
   * {@inheritdoc}
   */
  public function methodAccess(ResourceFieldInterface $resource_field) {
    $this->subject
      ->methodAccess($resource_field);
  }

  /**
   * {@inheritdoc}
   */
  public function canonicalPath($path) {
    return $this->subject
      ->canonicalPath($path);
  }

  /**
   * {@inheritdoc}
   */
  public function setOptions(array $options) {
    $this->subject
      ->setOptions($options);
  }

  /**
   * {@inheritdoc}
   */
  public function setResourcePath($resource_path) {
    $this->subject
      ->setResourcePath($resource_path);
  }

  /**
   * {@inheritdoc}
   */
  public function getResourcePath() {
    return $this->subject
      ->getResourcePath();
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadata() {
    return $this->subject
      ->getMetadata();
  }

  /**
   * Clears the cache entries related to the given cache fragments.
   *
   * @param \Doctrine\Common\Collections\ArrayCollection $cache_fragments
   *   The cache fragments to clear.
   */
  protected function clearRenderedCache(ArrayCollection $cache_fragments) {
    $cache_object = new RenderCache($cache_fragments, NULL, $this->cacheController);
    $cache_object
      ->clear();
  }

  /**
   * Checks if the decorated object is an instance of something.
   *
   * @param string $class
   *   Class or interface to check the instance.
   *
   * @return bool
   *   TRUE if the decorated object is an instace of the $class. FALSE
   *   otherwise.
   */
  public function isInstanceOf($class) {
    if ($this instanceof $class || $this->subject instanceof $class) {
      return TRUE;
    }

    // Check if the decorated resource is also a decorator.
    if ($this->subject instanceof ExplorableDecoratorInterface) {
      return $this->subject
        ->isInstanceOf($class);
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheDecoratedDataProvider::$cacheController protected property The cache controller to interact with the cache backed.
CacheDecoratedDataProvider::$subject protected property The decorated object.
CacheDecoratedDataProvider::addOptions public function Adds the options in the provided array to the data provider options. Overrides DataProviderInterface::addOptions
CacheDecoratedDataProvider::canonicalPath public function Generates the canonical path for a given path. Overrides DataProviderInterface::canonicalPath
CacheDecoratedDataProvider::clearRenderedCache protected function Clears the cache entries related to the given cache fragments.
CacheDecoratedDataProvider::count public function Counts the total results for the index call. Overrides CrudInterface::count
CacheDecoratedDataProvider::create public function Create operation. Overrides CrudInterface::create
CacheDecoratedDataProvider::discover public function Return the discovery information for the given entity. Overrides DataProviderInterface::discover
CacheDecoratedDataProvider::getAccount public function Gets the authenticated account. Overrides DataProviderInterface::getAccount
CacheDecoratedDataProvider::getCacheFragments public function Gets the entity context. Overrides DataProviderInterface::getCacheFragments
CacheDecoratedDataProvider::getIndexIds public function Returns the ID to render for the current index GET request. Overrides DataProviderInterface::getIndexIds
CacheDecoratedDataProvider::getLangCode public function Get the language code. Overrides DataProviderInterface::getLangCode
CacheDecoratedDataProvider::getMetadata public function Returns the metadata collection. Overrides DataProviderInterface::getMetadata
CacheDecoratedDataProvider::getOptions public function Gets the data provider options. Overrides DataProviderInterface::getOptions
CacheDecoratedDataProvider::getRange public function Gets the range. Overrides DataProviderInterface::getRange
CacheDecoratedDataProvider::getRequest public function Gets the request. Overrides DataProviderInterface::getRequest
CacheDecoratedDataProvider::getResourcePath public function Get the resource path. Overrides DataProviderInterface::getResourcePath
CacheDecoratedDataProvider::index public function List operation. Overrides CrudInterface::index
CacheDecoratedDataProvider::isInstanceOf public function Checks if the decorated object is an instance of something. Overrides ExplorableDecoratorInterface::isInstanceOf
CacheDecoratedDataProvider::isNestedField public static function Checks if the passed in string is a dot-nested field. Overrides DataProviderInterface::isNestedField
CacheDecoratedDataProvider::methodAccess public function Checks if the provided field can be used with the current method. Overrides DataProviderInterface::methodAccess
CacheDecoratedDataProvider::processFilterInput public static function Processes the input for a filter and adds the appropriate defaults. Overrides DataProviderInterface::processFilterInput
CacheDecoratedDataProvider::remove public function Delete operation. Overrides CrudInterface::remove
CacheDecoratedDataProvider::setAccount public function Sets the authenticated account. Overrides DataProviderInterface::setAccount
CacheDecoratedDataProvider::setLangCode public function Sets the language code. Overrides DataProviderInterface::setLangCode
CacheDecoratedDataProvider::setOptions public function Sets the options. Overrides DataProviderInterface::setOptions
CacheDecoratedDataProvider::setRange public function Sets the range. Overrides DataProviderInterface::setRange
CacheDecoratedDataProvider::setRequest public function Sets the request. Overrides DataProviderInterface::setRequest
CacheDecoratedDataProvider::setResourcePath public function Set the resource path. Overrides DataProviderInterface::setResourcePath
CacheDecoratedDataProvider::update public function Update operation. Overrides CrudInterface::update
CacheDecoratedDataProvider::view public function Read operation. Overrides CrudInterface::view
CacheDecoratedDataProvider::viewMultiple public function Read operation. Overrides CrudInterface::viewMultiple
CacheDecoratedDataProvider::__construct public function Constructs a CacheDecoratedDataProvider object.