CacheDecoratedDataProvider.php in RESTful 7.2
File
src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php
View source
<?php
namespace Drupal\restful\Plugin\resource\DataProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\restful\Exception\InaccessibleRecordException;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\Field\ResourceFieldCollectionInterface;
use Drupal\restful\Plugin\resource\Field\ResourceFieldInterface;
use Drupal\restful\RenderCache\RenderCache;
use Drupal\restful\Util\ExplorableDecoratorInterface;
class CacheDecoratedDataProvider implements CacheDecoratedDataProviderInterface, ExplorableDecoratorInterface {
protected $subject;
protected $cacheController;
public function __construct(DataProviderInterface $subject, \DrupalCacheInterface $cache_controller) {
$this->subject = $subject;
$this->cacheController = $cache_controller;
}
public static function isNestedField($field_name) {
return DataProvider::isNestedField($field_name);
}
public static function processFilterInput($filter, $public_field) {
return DataProvider::processFilterInput($filter, $public_field);
}
public function discover($path = NULL) {
return $this->subject
->discover($path);
}
public function getRange() {
return $this->subject
->getRange();
}
public function setRange($range) {
return $this->subject
->setRange($range);
}
public function getAccount() {
return $this->subject
->getAccount();
}
public function setAccount($account) {
$this->subject
->setAccount($account);
}
public function getRequest() {
return $this->subject
->getRequest();
}
public function setRequest(RequestInterface $request) {
$this->subject
->setRequest($request);
}
public function getLangCode() {
return $this->subject
->getLangCode();
}
public function setLangCode($langcode) {
$this->subject
->setLangCode($langcode);
}
public function getOptions() {
return $this->subject
->getOptions();
}
public function addOptions(array $options) {
$this->subject
->addOptions($options);
}
public function getCacheFragments($identifier) {
return $this->subject
->getCacheFragments($identifier);
}
public function index() {
$ids = $this
->getIndexIds();
return $this
->viewMultiple($ids);
}
public function getIndexIds() {
return $this->subject
->getIndexIds();
}
public function count() {
return $this->subject
->count();
}
public function create($object) {
return $this->subject
->create($object);
}
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;
}
public function viewMultiple(array $identifiers) {
$return = array();
foreach ($identifiers as $identifier) {
try {
$row = $this
->view($identifier);
} catch (InaccessibleRecordException $e) {
$row = NULL;
}
$return[] = $row;
}
return array_values(array_filter($return));
}
public function update($identifier, $object, $replace = TRUE) {
$this
->clearRenderedCache($this
->getCacheFragments($identifier));
return $this->subject
->update($identifier, $object, $replace);
}
public function remove($identifier) {
$this
->clearRenderedCache($this
->getCacheFragments($identifier));
$this->subject
->remove($identifier);
}
public function methodAccess(ResourceFieldInterface $resource_field) {
$this->subject
->methodAccess($resource_field);
}
public function canonicalPath($path) {
return $this->subject
->canonicalPath($path);
}
public function setOptions(array $options) {
$this->subject
->setOptions($options);
}
public function setResourcePath($resource_path) {
$this->subject
->setResourcePath($resource_path);
}
public function getResourcePath() {
return $this->subject
->getResourcePath();
}
public function getMetadata() {
return $this->subject
->getMetadata();
}
protected function clearRenderedCache(ArrayCollection $cache_fragments) {
$cache_object = new RenderCache($cache_fragments, NULL, $this->cacheController);
$cache_object
->clear();
}
public function isInstanceOf($class) {
if ($this instanceof $class || $this->subject instanceof $class) {
return TRUE;
}
if ($this->subject instanceof ExplorableDecoratorInterface) {
return $this->subject
->isInstanceOf($class);
}
return FALSE;
}
}