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 * label_collection = @Translation("REST resource configurations"),
17 * label_singular = @Translation("REST resource configuration"),
18 * label_plural = @Translation("REST resource configurations"),
19 * label_count = @PluralTranslation(
20 * singular = "@count REST resource configuration",
21 * plural = "@count REST resource configurations",
23 * config_prefix = "resource",
24 * admin_permission = "administer rest resources",
25 * label_callback = "getLabelFromPlugin",
37 class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface {
40 * The REST resource config id.
47 * The REST resource plugin id.
54 * The REST resource configuration granularity.
57 * - \Drupal\rest\RestResourceConfigInterface::METHOD_GRANULARITY
58 * - \Drupal\rest\RestResourceConfigInterface::RESOURCE_GRANULARITY
62 protected $granularity;
65 * The REST resource configuration.
69 protected $configuration;
72 * The rest resource plugin manager.
74 * @var \Drupal\Component\Plugin\PluginManagerInterface
76 protected $pluginManager;
81 public function __construct(array $values, $entity_type) {
82 parent::__construct($values, $entity_type);
83 // The config entity id looks like the plugin id but uses __ instead of :
84 // because : is not valid for config entities.
85 if (!isset($this->plugin_id) && isset($this->id)) {
86 // Generate plugin_id on first entity creation.
87 $this->plugin_id = str_replace('.', ':', $this->id);
92 * The label callback for this configuration entity.
94 * @return string The label.
96 protected function getLabelFromPlugin() {
97 $plugin_definition = $this->getResourcePluginManager()
98 ->getDefinition(['id' => $this->plugin_id]);
99 return $plugin_definition['label'];
103 * Returns the resource plugin manager.
105 * @return \Drupal\Component\Plugin\PluginManagerInterface
107 protected function getResourcePluginManager() {
108 if (!isset($this->pluginManager)) {
109 $this->pluginManager = \Drupal::service('plugin.manager.rest');
111 return $this->pluginManager;
117 public function getResourcePlugin() {
118 return $this->getPluginCollections()['resource']->get($this->plugin_id);
124 public function getMethods() {
125 switch ($this->granularity) {
126 case RestResourceConfigInterface::METHOD_GRANULARITY:
127 return $this->getMethodsForMethodGranularity();
128 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
129 return $this->configuration['methods'];
131 throw new \InvalidArgumentException('Invalid granularity specified.');
136 * Retrieves a list of supported HTTP methods for this resource.
139 * A list of supported HTTP methods.
141 protected function getMethodsForMethodGranularity() {
142 $methods = array_keys($this->configuration);
143 return array_map([$this, 'normalizeRestMethod'], $methods);
149 public function getAuthenticationProviders($method) {
150 switch ($this->granularity) {
151 case RestResourceConfigInterface::METHOD_GRANULARITY:
152 return $this->getAuthenticationProvidersForMethodGranularity($method);
153 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
154 return $this->configuration['authentication'];
156 throw new \InvalidArgumentException('Invalid granularity specified.');
161 * Retrieves a list of supported authentication providers.
163 * @param string $method
164 * The request method e.g GET or POST.
167 * A list of supported authentication provider IDs.
169 public function getAuthenticationProvidersForMethodGranularity($method) {
170 $method = $this->normalizeRestMethod($method);
171 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_auth'])) {
172 return $this->configuration[$method]['supported_auth'];
180 public function getFormats($method) {
181 switch ($this->granularity) {
182 case RestResourceConfigInterface::METHOD_GRANULARITY:
183 return $this->getFormatsForMethodGranularity($method);
184 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
185 return $this->configuration['formats'];
187 throw new \InvalidArgumentException('Invalid granularity specified.');
192 * Retrieves a list of supported response formats.
194 * @param string $method
195 * The request method e.g GET or POST.
198 * A list of supported format IDs.
200 protected function getFormatsForMethodGranularity($method) {
201 $method = $this->normalizeRestMethod($method);
202 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_formats'])) {
203 return $this->configuration[$method]['supported_formats'];
211 public function getPluginCollections() {
213 'resource' => new DefaultSingleLazyPluginCollection($this->getResourcePluginManager(), $this->plugin_id, []),
220 public function calculateDependencies() {
221 parent::calculateDependencies();
223 foreach ($this->getRestResourceDependencies()->calculateDependencies($this) as $type => $dependencies) {
224 foreach ($dependencies as $dependency) {
225 $this->addDependency($type, $dependency);
234 public function onDependencyRemoval(array $dependencies) {
235 $parent = parent::onDependencyRemoval($dependencies);
237 // If the dependency problems are not marked as fixed at this point they
238 // should be related to the resource plugin and the config entity should
240 $changed = $this->getRestResourceDependencies()->onDependencyRemoval($this, $dependencies);
241 return $parent || $changed;
245 * Returns the REST resource dependencies.
247 * @return \Drupal\rest\Entity\ConfigDependencies
249 protected function getRestResourceDependencies() {
250 return \Drupal::service('class_resolver')->getInstanceFromDefinition(ConfigDependencies::class);
254 * Normalizes the method.
256 * @param string $method
257 * The request method.
260 * The normalized request method.
262 protected function normalizeRestMethod($method) {
263 return strtoupper($method);
269 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
270 parent::postSave($storage, $update);
272 \Drupal::service('router.builder')->setRebuildNeeded();
278 public static function postDelete(EntityStorageInterface $storage, array $entities) {
279 parent::postDelete($storage, $entities);
281 \Drupal::service('router.builder')->setRebuildNeeded();