8ed5a6dbff124f821c031a24e2ca98c7800da0ab
[yaffs-website] / web / core / modules / rest / rest.post_update.php
1 <?php
2
3 /**
4  * @file
5  * Post update functions for Rest.
6  */
7
8 use Drupal\rest\Entity\RestResourceConfig;
9 use Drupal\rest\RestResourceConfigInterface;
10
11 /**
12  * Create REST resource configuration entities.
13  *
14  * @see rest_update_8201()
15  * @see https://www.drupal.org/node/2308745
16  */
17 function rest_post_update_create_rest_resource_config_entities() {
18   $resources = \Drupal::state()->get('rest_update_8201_resources', []);
19   foreach ($resources as $key => $resource) {
20     $resource = RestResourceConfig::create([
21       'id' => str_replace(':', '.', $key),
22       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
23       'configuration' => $resource,
24     ]);
25     $resource->save();
26   }
27 }
28
29 /**
30  * Simplify method-granularity REST resource config to resource-granularity.
31  *
32  * @see https://www.drupal.org/node/2721595
33  */
34 function rest_post_update_resource_granularity() {
35   /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */
36   $resource_config_entities = RestResourceConfig::loadMultiple();
37
38   foreach ($resource_config_entities as $resource_config_entity) {
39     if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) {
40       $configuration = $resource_config_entity->get('configuration');
41
42       $format_and_auth_configuration = [];
43       foreach (array_keys($configuration) as $method) {
44         $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']);
45         $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']);
46       }
47
48       // If each method has the same formats and the same authentication
49       // providers configured, convert it to 'granularity: resource', which has
50       // a simpler/less verbose configuration.
51       if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) {
52         $first_method = array_keys($configuration)[0];
53         $resource_config_entity->set('configuration', [
54           'methods' => array_keys($configuration),
55           'formats' => $configuration[$first_method]['supported_formats'],
56           'authentication' => $configuration[$first_method]['supported_auth'],
57         ]);
58         $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY);
59         $resource_config_entity->save();
60       }
61     }
62   }
63 }