Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Shared / RestTrait.php
1 <?php
2 /**
3  * @file
4  * Contains Drupal\Console\Command\Shared\RestTrait.
5  */
6
7 namespace Drupal\Console\Command\Shared;
8
9 trait RestTrait
10 {
11     /**
12    * [geRest get a list of Rest Resouces].
13    *
14    * @param bool $rest_status return Rest Resources by status
15    *
16    * @return array list of rest resources
17    */
18     public function getRestResources($rest_status = false)
19     {
20         $config = $this->getRestDrupalConfig();
21
22         $resources = $this->pluginManagerRest->getDefinitions();
23
24         $enabled_resources = array_combine(array_keys($config), array_keys($config));
25         $available_resources = ['enabled' => [], 'disabled' => []];
26
27         foreach ($resources as $id => $resource) {
28             $status = in_array($id, $enabled_resources) ? 'enabled' : 'disabled';
29             $available_resources[$status][$id] = $resource;
30         }
31
32         // Sort the list of resources by label.
33         $sort_resources = function ($resource_a, $resource_b) {
34             return strcmp($resource_a['label'], $resource_b['label']);
35         };
36         if (!empty($available_resources['enabled'])) {
37             uasort($available_resources['enabled'], $sort_resources);
38         }
39         if (!empty($available_resources['disabled'])) {
40             uasort($available_resources['disabled'], $sort_resources);
41         }
42
43         if (isset($available_resources[$rest_status])) {
44             return [$rest_status => $available_resources[$rest_status]];
45         }
46
47         return $available_resources;
48     }
49
50     public function getRestDrupalConfig()
51     {
52         if ($this->configFactory) {
53             return $this->configFactory->get('rest.settings')->get('resources') ?: [];
54         }
55
56         return null;
57     }
58
59     /**
60    * @param $rest
61    * @param $rest_resources_ids
62    * @param $translator
63    *
64    * @return mixed
65    */
66     public function validateRestResource($rest, $rest_resources_ids, $translator)
67     {
68         if (in_array($rest, $rest_resources_ids)) {
69             return $rest;
70         } else {
71             throw new \InvalidArgumentException(
72                 sprintf(
73                     $translator->trans('commands.rest.disable.messages.invalid-rest-id'),
74                     $rest
75                 )
76             );
77         }
78     }
79 }