Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / rest / src / Entity / RestResourceConfig.php
1 <?php
2
3 namespace Drupal\rest\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
8 use Drupal\rest\RestResourceConfigInterface;
9
10 /**
11  * Defines a RestResourceConfig configuration entity class.
12  *
13  * @ConfigEntityType(
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",
22  *   ),
23  *   config_prefix = "resource",
24  *   admin_permission = "administer rest resources",
25  *   label_callback = "getLabelFromPlugin",
26  *   entity_keys = {
27  *     "id" = "id"
28  *   },
29  *   config_export = {
30  *     "id",
31  *     "plugin_id",
32  *     "granularity",
33  *     "configuration"
34  *   }
35  * )
36  */
37 class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface {
38
39   /**
40    * The REST resource config id.
41    *
42    * @var string
43    */
44   protected $id;
45
46   /**
47    * The REST resource plugin id.
48    *
49    * @var string
50    */
51   protected $plugin_id;
52
53   /**
54    * The REST resource configuration granularity.
55    *
56    * Currently either:
57    * - \Drupal\rest\RestResourceConfigInterface::METHOD_GRANULARITY
58    * - \Drupal\rest\RestResourceConfigInterface::RESOURCE_GRANULARITY
59    *
60    * @var string
61    */
62   protected $granularity;
63
64   /**
65    * The REST resource configuration.
66    *
67    * @var array
68    */
69   protected $configuration;
70
71   /**
72    * The rest resource plugin manager.
73    *
74    * @var \Drupal\Component\Plugin\PluginManagerInterface
75    */
76   protected $pluginManager;
77
78   /**
79    * {@inheritdoc}
80    */
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);
88     }
89   }
90
91   /**
92    * The label callback for this configuration entity.
93    *
94    * @return string The label.
95    */
96   protected function getLabelFromPlugin() {
97     $plugin_definition = $this->getResourcePluginManager()
98       ->getDefinition(['id' => $this->plugin_id]);
99     return $plugin_definition['label'];
100   }
101
102   /**
103    * Returns the resource plugin manager.
104    *
105    * @return \Drupal\Component\Plugin\PluginManagerInterface
106    */
107   protected function getResourcePluginManager() {
108     if (!isset($this->pluginManager)) {
109       $this->pluginManager = \Drupal::service('plugin.manager.rest');
110     }
111     return $this->pluginManager;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function getResourcePlugin() {
118     return $this->getPluginCollections()['resource']->get($this->plugin_id);
119   }
120
121   /**
122    * {@inheritdoc}
123    */
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'];
130       default:
131         throw new \InvalidArgumentException('Invalid granularity specified.');
132     }
133   }
134
135   /**
136    * Retrieves a list of supported HTTP methods for this resource.
137    *
138    * @return string[]
139    *   A list of supported HTTP methods.
140    */
141   protected function getMethodsForMethodGranularity() {
142     $methods = array_keys($this->configuration);
143     return array_map([$this, 'normalizeRestMethod'], $methods);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
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'];
155       default:
156         throw new \InvalidArgumentException('Invalid granularity specified.');
157     }
158   }
159
160   /**
161    * Retrieves a list of supported authentication providers.
162    *
163    * @param string $method
164    *   The request method e.g GET or POST.
165    *
166    * @return string[]
167    *   A list of supported authentication provider IDs.
168    */
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'];
173     }
174     return [];
175   }
176
177   /**
178    * {@inheritdoc}
179    */
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'];
186       default:
187         throw new \InvalidArgumentException('Invalid granularity specified.');
188     }
189   }
190
191   /**
192    * Retrieves a list of supported response formats.
193    *
194    * @param string $method
195    *   The request method e.g GET or POST.
196    *
197    * @return string[]
198    *   A list of supported format IDs.
199    */
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'];
204     }
205     return [];
206   }
207
208   /**
209    * {@inheritdoc}
210    */
211   public function getPluginCollections() {
212     return [
213       'resource' => new DefaultSingleLazyPluginCollection($this->getResourcePluginManager(), $this->plugin_id, []),
214     ];
215   }
216
217   /**
218    * (@inheritdoc)
219    */
220   public function calculateDependencies() {
221     parent::calculateDependencies();
222
223     foreach ($this->getRestResourceDependencies()->calculateDependencies($this) as $type => $dependencies) {
224       foreach ($dependencies as $dependency) {
225         $this->addDependency($type, $dependency);
226       }
227     }
228     return $this;
229   }
230
231   /**
232    * {@inheritdoc}
233    */
234   public function onDependencyRemoval(array $dependencies) {
235     $parent = parent::onDependencyRemoval($dependencies);
236
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
239     // be deleted.
240     $changed = $this->getRestResourceDependencies()->onDependencyRemoval($this, $dependencies);
241     return $parent || $changed;
242   }
243
244   /**
245    * Returns the REST resource dependencies.
246    *
247    * @return \Drupal\rest\Entity\ConfigDependencies
248    */
249   protected function getRestResourceDependencies() {
250     return \Drupal::service('class_resolver')->getInstanceFromDefinition(ConfigDependencies::class);
251   }
252
253   /**
254    * Normalizes the method.
255    *
256    * @param string $method
257    *   The request method.
258    *
259    * @return string
260    *   The normalized request method.
261    */
262   protected function normalizeRestMethod($method) {
263     return strtoupper($method);
264   }
265
266   /**
267    * {@inheritdoc}
268    */
269   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
270     parent::postSave($storage, $update);
271
272     \Drupal::service('router.builder')->setRebuildNeeded();
273   }
274
275   /**
276    * {@inheritdoc}
277    */
278   public static function postDelete(EntityStorageInterface $storage, array $entities) {
279     parent::postDelete($storage, $entities);
280
281     \Drupal::service('router.builder')->setRebuildNeeded();
282   }
283
284 }