Same filename and directory in other branches
- 8.9.x core/modules/rest/src/Entity/RestResourceConfig.php
- 9 core/modules/rest/src/Entity/RestResourceConfig.php
Namespace
Drupal\rest\Entity
File
core/modules/rest/src/Entity/RestResourceConfig.php
View source
<?php
namespace Drupal\rest\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\rest\RestResourceConfigInterface;
class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface {
protected $id;
protected $plugin_id;
protected $granularity;
protected $configuration;
protected $pluginManager;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
if (!isset($this->plugin_id) && isset($this->id)) {
$this->plugin_id = str_replace('.', ':', $this->id);
}
}
protected function getResourcePluginManager() {
if (!isset($this->pluginManager)) {
$this->pluginManager = \Drupal::service('plugin.manager.rest');
}
return $this->pluginManager;
}
public function getResourcePlugin() {
return $this
->getPluginCollections()['resource']
->get($this->plugin_id);
}
public function getMethods() {
switch ($this->granularity) {
case RestResourceConfigInterface::METHOD_GRANULARITY:
return $this
->getMethodsForMethodGranularity();
case RestResourceConfigInterface::RESOURCE_GRANULARITY:
return $this->configuration['methods'];
default:
throw new \InvalidArgumentException('Invalid granularity specified.');
}
}
protected function getMethodsForMethodGranularity() {
$methods = array_keys($this->configuration);
return array_map([
$this,
'normalizeRestMethod',
], $methods);
}
public function getAuthenticationProviders($method) {
switch ($this->granularity) {
case RestResourceConfigInterface::METHOD_GRANULARITY:
return $this
->getAuthenticationProvidersForMethodGranularity($method);
case RestResourceConfigInterface::RESOURCE_GRANULARITY:
return $this->configuration['authentication'];
default:
throw new \InvalidArgumentException('Invalid granularity specified.');
}
}
public function getAuthenticationProvidersForMethodGranularity($method) {
$method = $this
->normalizeRestMethod($method);
if (in_array($method, $this
->getMethods()) && isset($this->configuration[$method]['supported_auth'])) {
return $this->configuration[$method]['supported_auth'];
}
return [];
}
public function getFormats($method) {
switch ($this->granularity) {
case RestResourceConfigInterface::METHOD_GRANULARITY:
return $this
->getFormatsForMethodGranularity($method);
case RestResourceConfigInterface::RESOURCE_GRANULARITY:
return $this->configuration['formats'];
default:
throw new \InvalidArgumentException('Invalid granularity specified.');
}
}
protected function getFormatsForMethodGranularity($method) {
$method = $this
->normalizeRestMethod($method);
if (in_array($method, $this
->getMethods()) && isset($this->configuration[$method]['supported_formats'])) {
return $this->configuration[$method]['supported_formats'];
}
return [];
}
public function getPluginCollections() {
return [
'resource' => new DefaultSingleLazyPluginCollection($this
->getResourcePluginManager(), $this->plugin_id, []),
];
}
public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this
->getRestResourceDependencies()
->calculateDependencies($this) as $type => $dependencies) {
foreach ($dependencies as $dependency) {
$this
->addDependency($type, $dependency);
}
}
return $this;
}
public function onDependencyRemoval(array $dependencies) {
$parent = parent::onDependencyRemoval($dependencies);
$changed = $this
->getRestResourceDependencies()
->onDependencyRemoval($this, $dependencies);
return $parent || $changed;
}
protected function getRestResourceDependencies() {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ConfigDependencies::class);
}
protected function normalizeRestMethod($method) {
return strtoupper($method);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
Classes