class CacheDecoratedResource in RESTful 7.2
Hierarchy
- class \Drupal\restful\Plugin\resource\Decorators\ResourceDecoratorBase extends \Drupal\Component\Plugin\PluginBase implements ResourceDecoratorInterface, ExplorableDecoratorInterface
- class \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource implements CacheDecoratedResourceInterface
Expanded class hierarchy of CacheDecoratedResource
6 files declare their use of CacheDecoratedResource
- CacheFragmentController.php in src/
RenderCache/ Entity/ CacheFragmentController.php - Contains \Drupal\restful\RenderCache\Entity\CacheFragmentController.
- DataProvider.php in src/
Plugin/ resource/ DataProvider/ DataProvider.php - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProvider.
- DataProviderDbQuery.php in src/
Plugin/ resource/ DataProvider/ DataProviderDbQuery.php - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderDbQuery.
- DataProviderEntity.php in src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.
- restful.entity.inc in ./
restful.entity.inc - Contains entity related code.
File
- src/
Plugin/ resource/ Decorators/ CacheDecoratedResource.php, line 18 - Contains \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource
Namespace
Drupal\restful\Plugin\resource\DecoratorsView source
class CacheDecoratedResource extends ResourceDecoratorBase implements CacheDecoratedResourceInterface {
/**
* Separator string for cache fragment key value pairs.
*/
const CACHE_PAIR_SEPARATOR = '#';
/**
* Cache controller object.
*
* @var \DrupalCacheInterface
*/
protected $cacheController;
/**
* The data provider.
*
* @var DataProviderInterface
*/
protected $dataProvider;
/**
* Constructs a Drupal\Component\Plugin\PluginBase object.
*
* @param ResourceInterface $subject
* The decorated object.
* @param \DrupalCacheInterface $cache_controller
* Injected cache manager.
*/
public function __construct(ResourceInterface $subject, \DrupalCacheInterface $cache_controller = NULL) {
// TODO: Implement the ResourceManager factory to use the CacheDecoratedResource.
$this->subject = $subject;
$this->cacheController = $cache_controller ? $cache_controller : $this
->newCacheObject();
$cache_info = $this
->defaultCacheInfo();
$this->pluginDefinition['renderCache'] = $cache_info;
}
/**
* {@inheritdoc}
*/
public function getCacheController() {
return $this->cacheController;
}
/**
* Get the default cache object based on the plugin configuration.
*
* By default, this returns an instance of the DrupalDatabaseCache class.
* Classes implementing DrupalCacheInterface can register themselves both as a
* default implementation and for specific bins.
*
* @return \DrupalCacheInterface
* The cache object associated with the specified bin.
*
* @see \DrupalCacheInterface
* @see _cache_get_object()
*/
protected function newCacheObject() {
// We do not use drupal_static() here because we do not want to change the
// storage of a cache bin mid-request.
static $cache_object;
if (isset($cache_object)) {
// Return cached object.
return $cache_object;
}
$cache_info = $this
->defaultCacheInfo();
$class_name = empty($cache_info['class']) ? NULL : $cache_info['class'];
// If there is no class name in the plugin definition, try to get it from
// the variables.
if (empty($class_name)) {
$class_name = variable_get('cache_class_' . $cache_info['bin']);
}
// If it is still empty, then default to drupal's default cache class.
if (empty($class_name)) {
$class_name = variable_get('cache_default_class', 'DrupalDatabaseCache');
}
$cache_object = new $class_name($cache_info['bin']);
return $cache_object;
}
/**
* {@inheritdoc}
*/
public function dataProviderFactory() {
if ($this->dataProvider && $this->dataProvider instanceof CacheDecoratedDataProvider) {
return $this->dataProvider;
}
// Get the data provider from the subject of the decorator.
$decorated_provider = $this->subject
->dataProviderFactory();
$this->dataProvider = new CacheDecoratedDataProvider($decorated_provider, $this
->getCacheController());
$plugin_definition = $this
->getPluginDefinition();
$this->dataProvider
->addOptions(array(
'renderCache' => $this
->defaultCacheInfo(),
'resource' => array(
'version' => array(
'major' => $plugin_definition['majorVersion'],
'minor' => $plugin_definition['minorVersion'],
),
'name' => $plugin_definition['resource'],
),
));
return $this->dataProvider;
}
/**
* {@inheritdoc}
*/
public function getPath() {
return $this->subject
->getPath();
}
/**
* {@inheritdoc}
*/
public function setPath($path) {
$this->subject
->setPath($path);
}
/**
* {@inheritdoc}
*/
public function getFieldDefinitions() {
return $this->subject
->getFieldDefinitions();
}
/**
* {@inheritdoc}
*/
public function getDataProvider() {
if (isset($this->dataProvider)) {
return $this->dataProvider;
}
$this->dataProvider = $this
->dataProviderFactory();
return $this->dataProvider;
}
/**
* {@inheritdoc}
*/
public function setDataProvider(DataProviderInterface $data_provider = NULL) {
$this->dataProvider = $data_provider;
}
/**
* {@inheritdoc}
*/
public function process() {
$path = $this
->getPath();
return ResourceManager::executeCallback($this
->getControllerFromPath($path), array(
$path,
));
}
/**
* {@inheritdoc}
*/
public function view($path) {
// TODO: This is duplicating the code from Resource::view
$ids = explode(static::IDS_SEPARATOR, $path);
// REST requires a canonical URL for every resource.
$canonical_path = $this
->getDataProvider()
->canonicalPath($path);
$this
->getRequest()
->getHeaders()
->add(HttpHeader::create('Link', $this
->versionedUrl($canonical_path, array(), FALSE) . '; rel="canonical"'));
// If there is only one ID then use 'view'. Else, use 'viewMultiple'. The
// difference between the two is that 'view' allows access denied
// exceptions.
if (count($ids) == 1) {
return array(
$this
->getDataProvider()
->view($ids[0]),
);
}
else {
return $this
->getDataProvider()
->viewMultiple($ids);
}
}
/**
* {@inheritdoc}
*/
public function index($path) {
// TODO: This is duplicating the code from Resource::index
return $this
->getDataProvider()
->index();
}
/**
* {@inheritdoc}
*/
public function update($path) {
$this
->invalidateResourceCache($path);
// Update according to the decorated.
return $this->subject
->update($path);
}
/**
* {@inheritdoc}
*/
public function replace($path) {
$this
->invalidateResourceCache($path);
// Update according to the decorated.
return $this->subject
->replace($path);
}
/**
* {@inheritdoc}
*/
public function remove($path) {
$this
->invalidateResourceCache($path);
$this->subject
->remove($path);
}
/**
* Gets the default cache info.
*
* @return array
* The cache info.
*/
protected function defaultCacheInfo() {
$plugin_definition = $this
->getPluginDefinition();
$cache_info = empty($plugin_definition['renderCache']) ? array() : $plugin_definition['renderCache'];
$cache_info += array(
'render' => variable_get('restful_render_cache', FALSE),
'class' => NULL,
'bin' => RenderCache::CACHE_BIN,
'expire' => CACHE_PERMANENT,
'simpleInvalidate' => TRUE,
'granularity' => DRUPAL_CACHE_PER_USER,
);
return $cache_info;
}
/**
* {@inheritdoc}
*/
public function getResourceMachineName() {
return $this->subject
->getResourceMachineName();
}
/**
* {@inheritdoc}
*
* This is a decorated resource, get proxy the call until you reach the
* annotated resource.
*/
public function getPluginDefinition() {
return $this->subject
->getPluginDefinition();
}
/**
* {@inheritdoc}
*/
public function enable() {
$this->subject
->enable();
}
/**
* {@inheritdoc}
*/
public function disable() {
$this->subject
->disable();
}
/**
* {@inheritdoc}
*/
public function isEnabled() {
return $this->subject
->isEnabled();
}
/**
* {@inheritdoc}
*/
public function hasSimpleInvalidation() {
$data_provider = $this
->getDataProvider();
$options = $data_provider
->getOptions();
$cache_info = $options['renderCache'];
return !empty($cache_info['simpleInvalidate']);
}
/**
* Invalidates the resource cache for the given resource on the provided id.
*
* @param string $id
* The id.
*/
protected function invalidateResourceCache($id) {
// Invalidate the render cache for this resource.
$query = new \EntityFieldQuery();
$canonical_id = $this
->getDataProvider()
->canonicalPath($id);
$query
->entityCondition('entity_type', 'cache_fragment')
->propertyCondition('type', 'resource')
->propertyCondition('value', $this::serializeKeyValue($this
->getResourceName(), $canonical_id));
foreach (CacheFragmentController::lookUpHashes($query) as $hash) {
$this
->getCacheController()
->clear($hash);
}
}
/**
* {@inheritdoc}
*/
public static function serializeKeyValue($key, $value) {
return sprintf('%s%s%s', $key, static::CACHE_PAIR_SEPARATOR, $value);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheDecoratedResource:: |
protected | property | Cache controller object. | |
CacheDecoratedResource:: |
protected | property | The data provider. | |
CacheDecoratedResource:: |
constant | Separator string for cache fragment key value pairs. | ||
CacheDecoratedResource:: |
public | function |
Data provider factory. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
protected | function | Gets the default cache info. | |
CacheDecoratedResource:: |
public | function |
Disable the resource. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Enable the resource. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Getter for $cacheController. Overrides CacheDecoratedResourceInterface:: |
|
CacheDecoratedResource:: |
public | function |
Gets the data provider. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Gets the field definitions. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Gets the path of the resource. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
This is a decorated resource, get proxy the call until you reach the
annotated resource. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Gets the resource machine name. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Checks if simple invalidation is enabled for this resource. Overrides CacheDecoratedResourceInterface:: |
|
CacheDecoratedResource:: |
public | function |
Basic implementation for listing. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
protected | function | Invalidates the resource cache for the given resource on the provided id. | |
CacheDecoratedResource:: |
public | function |
Checks if the resource is enabled. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
protected | function | Get the default cache object based on the plugin configuration. | |
CacheDecoratedResource:: |
public | function |
Controller function that passes the data along and executes right action. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Basic implementation for update. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Basic implementation for update. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public static | function |
Generates a serialized key value pair. Overrides CacheDecoratedResourceInterface:: |
|
CacheDecoratedResource:: |
public | function |
Sets the data provider. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Sets the path of the resource. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Basic implementation for update. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function |
Basic implementation for view. Overrides ResourceDecoratorBase:: |
|
CacheDecoratedResource:: |
public | function | Constructs a Drupal\Component\Plugin\PluginBase object. | |
ResourceDecoratorBase:: |
protected | property | The decorated resource. | |
ResourceDecoratorBase:: |
public | function |
Determine if user can access the handler. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function | ||
ResourceDecoratorBase:: |
public | function |
Gets the controllers. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Basic implementation for create. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function | ||
ResourceDecoratorBase:: |
public | function |
Discovery controller callback. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Shorthand method to perform a quick DELETE request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Shorthand method to perform a quick GET request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Shorthand method to perform a quick PATCH request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Shorthand method to perform a quick POST request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Shorthand method to perform a quick PUT request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
private | function | ||
ResourceDecoratorBase:: |
public | function |
Get the user from for request. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function | ||
ResourceDecoratorBase:: |
public | function |
Return the controller for a given path. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Gets the controllers for this resource. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Gets the decorated resource. Overrides ResourceDecoratorInterface:: |
|
ResourceDecoratorBase:: |
public | function | ||
ResourceDecoratorBase:: |
public | function |
Gets the primary resource, the one that is not a decorator. Overrides ResourceDecoratorInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Get the request object. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Gets the resource name. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Helper method; Get the URL of the resource and query strings. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Return array keyed with the major and minor version of the resource. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Checks if the decorated object is an instance of something. Overrides ExplorableDecoratorInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Overrides ResourceInterface:: |
1 |
ResourceDecoratorBase:: |
public | function | ||
ResourceDecoratorBase:: |
public | function |
Sets the field definitions. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
This is a decorated resource, set proxy the request until you reach the
annotated resource. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Sets the request object. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Switches the user back from the original user for the session. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function |
Gets a resource URL based on the current version. Overrides ResourceInterface:: |
|
ResourceDecoratorBase:: |
public | function | If any method not declared, then defer it to the decorated field. | |
ResourceInterface:: |
constant | The string that separates multiple ids. |