3 namespace Drupal\rest\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
8 use Drupal\rest\RestResourceConfigInterface;
11 * Defines a RestResourceConfig configuration entity class.
14 * id = "rest_resource_config",
15 * label = @Translation("REST resource configuration"),
16 * config_prefix = "resource",
17 * admin_permission = "administer rest resources",
18 * label_callback = "getLabelFromPlugin",
30 class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface {
33 * The REST resource config id.
40 * The REST resource plugin id.
47 * The REST resource configuration granularity.
50 * - \Drupal\rest\RestResourceConfigInterface::METHOD_GRANULARITY
51 * - \Drupal\rest\RestResourceConfigInterface::RESOURCE_GRANULARITY
55 protected $granularity;
58 * The REST resource configuration.
62 protected $configuration;
65 * The rest resource plugin manager.
67 * @var \Drupal\Component\Plugin\PluginManagerInterface
69 protected $pluginManager;
74 public function __construct(array $values, $entity_type) {
75 parent::__construct($values, $entity_type);
76 // The config entity id looks like the plugin id but uses __ instead of :
77 // because : is not valid for config entities.
78 if (!isset($this->plugin_id) && isset($this->id)) {
79 // Generate plugin_id on first entity creation.
80 $this->plugin_id = str_replace('.', ':', $this->id);
85 * The label callback for this configuration entity.
87 * @return string The label.
89 protected function getLabelFromPlugin() {
90 $plugin_definition = $this->getResourcePluginManager()
91 ->getDefinition(['id' => $this->plugin_id]);
92 return $plugin_definition['label'];
96 * Returns the resource plugin manager.
98 * @return \Drupal\Component\Plugin\PluginManagerInterface
100 protected function getResourcePluginManager() {
101 if (!isset($this->pluginManager)) {
102 $this->pluginManager = \Drupal::service('plugin.manager.rest');
104 return $this->pluginManager;
110 public function getResourcePlugin() {
111 return $this->getPluginCollections()['resource']->get($this->plugin_id);
117 public function getMethods() {
118 switch ($this->granularity) {
119 case RestResourceConfigInterface::METHOD_GRANULARITY:
120 return $this->getMethodsForMethodGranularity();
121 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
122 return $this->configuration['methods'];
124 throw new \InvalidArgumentException('Invalid granularity specified.');
129 * Retrieves a list of supported HTTP methods for this resource.
132 * A list of supported HTTP methods.
134 protected function getMethodsForMethodGranularity() {
135 $methods = array_keys($this->configuration);
136 return array_map([$this, 'normalizeRestMethod'], $methods);
142 public function getAuthenticationProviders($method) {
143 switch ($this->granularity) {
144 case RestResourceConfigInterface::METHOD_GRANULARITY:
145 return $this->getAuthenticationProvidersForMethodGranularity($method);
146 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
147 return $this->configuration['authentication'];
149 throw new \InvalidArgumentException('Invalid granularity specified.');
154 * Retrieves a list of supported authentication providers.
156 * @param string $method
157 * The request method e.g GET or POST.
160 * A list of supported authentication provider IDs.
162 public function getAuthenticationProvidersForMethodGranularity($method) {
163 $method = $this->normalizeRestMethod($method);
164 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_auth'])) {
165 return $this->configuration[$method]['supported_auth'];
173 public function getFormats($method) {
174 switch ($this->granularity) {
175 case RestResourceConfigInterface::METHOD_GRANULARITY:
176 return $this->getFormatsForMethodGranularity($method);
177 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
178 return $this->configuration['formats'];
180 throw new \InvalidArgumentException('Invalid granularity specified.');
185 * Retrieves a list of supported response formats.
187 * @param string $method
188 * The request method e.g GET or POST.
191 * A list of supported format IDs.
193 protected function getFormatsForMethodGranularity($method) {
194 $method = $this->normalizeRestMethod($method);
195 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_formats'])) {
196 return $this->configuration[$method]['supported_formats'];
204 public function getPluginCollections() {
206 'resource' => new DefaultSingleLazyPluginCollection($this->getResourcePluginManager(), $this->plugin_id, [])
213 public function calculateDependencies() {
214 parent::calculateDependencies();
216 foreach ($this->getRestResourceDependencies()->calculateDependencies($this) as $type => $dependencies) {
217 foreach ($dependencies as $dependency) {
218 $this->addDependency($type, $dependency);
227 public function onDependencyRemoval(array $dependencies) {
228 $parent = parent::onDependencyRemoval($dependencies);
230 // If the dependency problems are not marked as fixed at this point they
231 // should be related to the resource plugin and the config entity should
233 $changed = $this->getRestResourceDependencies()->onDependencyRemoval($this, $dependencies);
234 return $parent || $changed;
238 * Returns the REST resource dependencies.
240 * @return \Drupal\rest\Entity\ConfigDependencies
242 protected function getRestResourceDependencies() {
243 return \Drupal::service('class_resolver')->getInstanceFromDefinition(ConfigDependencies::class);
247 * Normalizes the method.
249 * @param string $method
250 * The request method.
253 * The normalized request method.
255 protected function normalizeRestMethod($method) {
256 return strtoupper($method);
262 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
263 parent::postSave($storage, $update);
265 \Drupal::service('router.builder')->setRebuildNeeded();
271 public static function postDelete(EntityStorageInterface $storage, array $entities) {
272 parent::postDelete($storage, $entities);
274 \Drupal::service('router.builder')->setRebuildNeeded();