Version 1
[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
25         $enabled_resources = array_combine(array_keys($config), array_keys($config));
26         $available_resources = ['enabled' => [], 'disabled' => []];
27
28         foreach ($resources as $id => $resource) {
29             $status = in_array($id, $enabled_resources) ? 'enabled' : 'disabled';
30             $available_resources[$status][$id] = $resource;
31         }
32
33         // Sort the list of resources by label.
34         $sort_resources = function ($resource_a, $resource_b) {
35             return strcmp($resource_a['label'], $resource_b['label']);
36         };
37         if (!empty($available_resources['enabled'])) {
38             uasort($available_resources['enabled'], $sort_resources);
39         }
40         if (!empty($available_resources['disabled'])) {
41             uasort($available_resources['disabled'], $sort_resources);
42         }
43
44         if (isset($available_resources[$rest_status])) {
45             return [$rest_status => $available_resources[$rest_status]];
46         }
47
48         return $available_resources;
49     }
50
51     public function getRestDrupalConfig()
52     {
53         if ($this->configFactory) {
54             return $this->configFactory->get('rest.settings')->get('resources') ?: [];
55         }
56
57         return null;
58     }
59
60     /**
61    * @param $rest
62    * @param $rest_resources_ids
63    * @param $translator
64    *
65    * @return mixed
66    */
67     public function validateRestResource($rest, $rest_resources_ids, $translator)
68     {
69         if (in_array($rest, $rest_resources_ids)) {
70             return $rest;
71         } else {
72             throw new \InvalidArgumentException(
73                 sprintf(
74                     $translator->trans('commands.rest.disable.messages.invalid-rest-id'),
75                     $rest
76                 )
77             );
78         }
79     }
80 }