View source
<?php
namespace Drupal\restful\Plugin\resource\DataProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\restful\Exception\NotFoundException;
use Drupal\restful\Exception\NotImplementedException;
use Drupal\restful\Exception\InaccessibleRecordException;
use Drupal\restful\Exception\UnauthorizedException;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\DataInterpreter\DataInterpreterPlug;
use Drupal\restful\Plugin\resource\DataInterpreter\PluginWrapper;
use Drupal\restful\Plugin\resource\Field\ResourceFieldCollectionInterface;
class DataProviderPlug extends DataProvider implements DataProviderInterface {
public function __construct(RequestInterface $request, ResourceFieldCollectionInterface $field_definitions, $account, $plugin_id, $resource_path = NULL, array $options = array(), $langcode = NULL) {
parent::__construct($request, $field_definitions, $account, $plugin_id, $resource_path, $options, $langcode);
if (empty($this->options['urlParams'])) {
$this->options['urlParams'] = array(
'filter' => TRUE,
'sort' => TRUE,
'fields' => TRUE,
);
}
}
public function count() {
return count($this
->getIndexIds());
}
public function create($object) {
throw new NotImplementedException('You cannot create plugins through the API.');
}
public function view($identifier) {
$resource_field_collection = $this
->initResourceFieldCollection($identifier);
$input = $this
->getRequest()
->getParsedInput();
$limit_fields = !empty($input['fields']) ? explode(',', $input['fields']) : array();
foreach ($this->fieldDefinitions as $resource_field_name => $resource_field) {
if ($limit_fields && !in_array($resource_field_name, $limit_fields)) {
continue;
}
if (!$this
->methodAccess($resource_field) || !$resource_field
->access('view', $resource_field_collection
->getInterpreter())) {
continue;
}
$resource_field_collection
->set($resource_field
->id(), $resource_field);
}
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 = FALSE) {
$disabled_plugins = variable_get('restful_disabled_plugins', array());
if ($object['enable']) {
$disabled_plugins[$identifier] = FALSE;
}
variable_set('restful_disabled_plugins', $disabled_plugins);
}
public function remove($identifier) {
$disabled_plugins = variable_get('restful_disabled_plugins', array());
$disabled_plugins[$identifier] = TRUE;
variable_set('restful_disabled_plugins', $disabled_plugins);
}
public function getIndexIds() {
$plugins = restful()
->getResourceManager()
->getPlugins();
$output = $plugins
->getIterator()
->getArrayCopy();
$output = $this
->applyFilters($output);
$output = $this
->applySort($output);
return array_keys($output);
}
protected function applyFilters(array $plugins) {
$resource_manager = restful()
->getResourceManager();
$filters = $this
->parseRequestForListFilter();
$input = $this
->getRequest()
->getParsedInput();
$all = !empty($input['all']);
foreach ($plugins as $instance_id => $plugin) {
if (!$all) {
$version = $plugin
->getVersion();
list($last_major, $last_minor) = $resource_manager
->getResourceLastVersion($plugin
->getResourceMachineName());
if ($version['major'] != $last_major || $version['minor'] != $last_minor) {
unset($plugins[$instance_id]);
continue;
}
}
$definition = $plugin
->getPluginDefinition();
if (!$definition['discoverable']) {
unset($plugins[$instance_id]);
continue;
}
$interpreter = new DataInterpreterPlug($this
->getAccount(), new PluginWrapper($plugin));
$this->fieldDefinitions
->setInterpreter($interpreter);
foreach ($filters as $filter) {
if (!$this->fieldDefinitions
->evalFilter($filter)) {
unset($plugins[$instance_id]);
}
}
}
$this->fieldDefinitions
->setInterpreter(NULL);
return $plugins;
}
protected function applySort(array $plugins) {
if ($sorts = $this
->parseRequestForListSort()) {
uasort($plugins, function ($plugin1, $plugin2) use ($sorts) {
$interpreter1 = new DataInterpreterPlug($this
->getAccount(), new PluginWrapper($plugin1));
$interpreter2 = new DataInterpreterPlug($this
->getAccount(), new PluginWrapper($plugin2));
foreach ($sorts as $key => $order) {
$property = $this->fieldDefinitions
->get($key)
->getProperty();
$value1 = $interpreter1
->getWrapper()
->get($property);
$value2 = $interpreter2
->getWrapper()
->get($property);
if ($value1 == $value2) {
continue;
}
return ($order == 'DESC' ? -1 : 1) * strcmp($value1, $value2);
}
return 0;
});
}
return $plugins;
}
protected function initDataInterpreter($identifier) {
$resource_manager = restful()
->getResourceManager();
try {
$plugin = $resource_manager
->getPlugin($identifier);
} catch (UnauthorizedException $e) {
return NULL;
} catch (PluginNotFoundException $e) {
throw new NotFoundException('Invalid URL path.');
}
$definition = $plugin
->getPluginDefinition();
if (empty($definition['discoverable'])) {
throw new InaccessibleRecordException(sprintf('The plugin %s is not discoverable.', $plugin
->getResourceName()));
}
return new DataInterpreterPlug($this
->getAccount(), new PluginWrapper($plugin));
}
public function getCacheFragments($identifier) {
if (is_array($identifier)) {
$identifier = implode(',', $identifier);
}
$fragments = new ArrayCollection(array(
'resource' => $identifier,
));
$options = $this
->getOptions();
switch ($options['renderCache']['granularity']) {
case DRUPAL_CACHE_PER_USER:
if ($uid = $this
->getAccount()->uid) {
$fragments
->set('user_id', (int) $uid);
}
break;
case DRUPAL_CACHE_PER_ROLE:
$fragments
->set('user_role', implode(',', $this
->getAccount()->roles));
break;
}
return $fragments;
}
}