summaryrefslogtreecommitdiffstats
path: root/core/modules/rest/rest.post_update.php
blob: 6d45178804bb4aded4726c5076bdd15eaa13268f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

/**
 * @file
 * Post update functions for Rest.
 */

use Drupal\rest\Entity\RestResourceConfig;
use Drupal\rest\RestResourceConfigInterface;

/**
 * Create REST resource configuration entities.
 *
 * @see rest_update_8201()
 * @see https://www.drupal.org/node/2308745
 */
function rest_post_update_create_rest_resource_config_entities() {
  $resources = \Drupal::state()->get('rest_update_8201_resources', []);
  foreach ($resources as $key => $resource) {
    $resource = RestResourceConfig::create([
      'id' => str_replace(':', '.', $key),
      'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
      'configuration' => $resource,
    ]);
    $resource->save();
  }
}

/**
 * Simplify method-granularity REST resource config to resource-granularity.
 *
 * @see https://www.drupal.org/node/2721595
 */
function rest_post_update_resource_granularity() {
  /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */
  $resource_config_entities = RestResourceConfig::loadMultiple();

  foreach ($resource_config_entities as $resource_config_entity) {
    if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) {
      $configuration = $resource_config_entity->get('configuration');

      $format_and_auth_configuration = [];
      foreach (array_keys($configuration) as $method) {
        $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']);
        $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']);
      }

      // If each method has the same formats and the same authentication
      // providers configured, convert it to 'granularity: resource', which has
      // a simpler/less verbose configuration.
      if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) {
        $first_method = array_keys($configuration)[0];
        $resource_config_entity->set('configuration', [
          'methods' => array_keys($configuration),
          'formats' => $configuration[$first_method]['supported_formats'],
          'authentication' => $configuration[$first_method]['supported_auth']
        ]);
        $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY);
        $resource_config_entity->save();
      }
    }
  }
}