Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\rest\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
|
Chris@0
|
8 use Drupal\rest\RestResourceConfigInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Defines a RestResourceConfig configuration entity class.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @ConfigEntityType(
|
Chris@0
|
14 * id = "rest_resource_config",
|
Chris@0
|
15 * label = @Translation("REST resource configuration"),
|
Chris@17
|
16 * label_collection = @Translation("REST resource configurations"),
|
Chris@17
|
17 * label_singular = @Translation("REST resource configuration"),
|
Chris@17
|
18 * label_plural = @Translation("REST resource configurations"),
|
Chris@17
|
19 * label_count = @PluralTranslation(
|
Chris@17
|
20 * singular = "@count REST resource configuration",
|
Chris@17
|
21 * plural = "@count REST resource configurations",
|
Chris@17
|
22 * ),
|
Chris@0
|
23 * config_prefix = "resource",
|
Chris@0
|
24 * admin_permission = "administer rest resources",
|
Chris@0
|
25 * label_callback = "getLabelFromPlugin",
|
Chris@0
|
26 * entity_keys = {
|
Chris@0
|
27 * "id" = "id"
|
Chris@0
|
28 * },
|
Chris@0
|
29 * config_export = {
|
Chris@0
|
30 * "id",
|
Chris@0
|
31 * "plugin_id",
|
Chris@0
|
32 * "granularity",
|
Chris@0
|
33 * "configuration"
|
Chris@0
|
34 * }
|
Chris@0
|
35 * )
|
Chris@0
|
36 */
|
Chris@0
|
37 class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface {
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The REST resource config id.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $id;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * The REST resource plugin id.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @var string
|
Chris@0
|
50 */
|
Chris@0
|
51 protected $plugin_id;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * The REST resource configuration granularity.
|
Chris@0
|
55 *
|
Chris@0
|
56 * Currently either:
|
Chris@0
|
57 * - \Drupal\rest\RestResourceConfigInterface::METHOD_GRANULARITY
|
Chris@0
|
58 * - \Drupal\rest\RestResourceConfigInterface::RESOURCE_GRANULARITY
|
Chris@0
|
59 *
|
Chris@0
|
60 * @var string
|
Chris@0
|
61 */
|
Chris@0
|
62 protected $granularity;
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * The REST resource configuration.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @var array
|
Chris@0
|
68 */
|
Chris@0
|
69 protected $configuration;
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * The rest resource plugin manager.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @var \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
75 */
|
Chris@0
|
76 protected $pluginManager;
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * {@inheritdoc}
|
Chris@0
|
80 */
|
Chris@0
|
81 public function __construct(array $values, $entity_type) {
|
Chris@0
|
82 parent::__construct($values, $entity_type);
|
Chris@0
|
83 // The config entity id looks like the plugin id but uses __ instead of :
|
Chris@0
|
84 // because : is not valid for config entities.
|
Chris@0
|
85 if (!isset($this->plugin_id) && isset($this->id)) {
|
Chris@0
|
86 // Generate plugin_id on first entity creation.
|
Chris@0
|
87 $this->plugin_id = str_replace('.', ':', $this->id);
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * The label callback for this configuration entity.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return string The label.
|
Chris@0
|
95 */
|
Chris@0
|
96 protected function getLabelFromPlugin() {
|
Chris@0
|
97 $plugin_definition = $this->getResourcePluginManager()
|
Chris@0
|
98 ->getDefinition(['id' => $this->plugin_id]);
|
Chris@0
|
99 return $plugin_definition['label'];
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Returns the resource plugin manager.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @return \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
106 */
|
Chris@0
|
107 protected function getResourcePluginManager() {
|
Chris@0
|
108 if (!isset($this->pluginManager)) {
|
Chris@0
|
109 $this->pluginManager = \Drupal::service('plugin.manager.rest');
|
Chris@0
|
110 }
|
Chris@0
|
111 return $this->pluginManager;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * {@inheritdoc}
|
Chris@0
|
116 */
|
Chris@0
|
117 public function getResourcePlugin() {
|
Chris@0
|
118 return $this->getPluginCollections()['resource']->get($this->plugin_id);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * {@inheritdoc}
|
Chris@0
|
123 */
|
Chris@0
|
124 public function getMethods() {
|
Chris@0
|
125 switch ($this->granularity) {
|
Chris@0
|
126 case RestResourceConfigInterface::METHOD_GRANULARITY:
|
Chris@0
|
127 return $this->getMethodsForMethodGranularity();
|
Chris@0
|
128 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
|
Chris@0
|
129 return $this->configuration['methods'];
|
Chris@0
|
130 default:
|
Chris@0
|
131 throw new \InvalidArgumentException('Invalid granularity specified.');
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Retrieves a list of supported HTTP methods for this resource.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return string[]
|
Chris@0
|
139 * A list of supported HTTP methods.
|
Chris@0
|
140 */
|
Chris@0
|
141 protected function getMethodsForMethodGranularity() {
|
Chris@0
|
142 $methods = array_keys($this->configuration);
|
Chris@0
|
143 return array_map([$this, 'normalizeRestMethod'], $methods);
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@0
|
149 public function getAuthenticationProviders($method) {
|
Chris@0
|
150 switch ($this->granularity) {
|
Chris@0
|
151 case RestResourceConfigInterface::METHOD_GRANULARITY:
|
Chris@0
|
152 return $this->getAuthenticationProvidersForMethodGranularity($method);
|
Chris@0
|
153 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
|
Chris@0
|
154 return $this->configuration['authentication'];
|
Chris@0
|
155 default:
|
Chris@0
|
156 throw new \InvalidArgumentException('Invalid granularity specified.');
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * Retrieves a list of supported authentication providers.
|
Chris@0
|
162 *
|
Chris@0
|
163 * @param string $method
|
Chris@0
|
164 * The request method e.g GET or POST.
|
Chris@0
|
165 *
|
Chris@0
|
166 * @return string[]
|
Chris@0
|
167 * A list of supported authentication provider IDs.
|
Chris@0
|
168 */
|
Chris@0
|
169 public function getAuthenticationProvidersForMethodGranularity($method) {
|
Chris@0
|
170 $method = $this->normalizeRestMethod($method);
|
Chris@0
|
171 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_auth'])) {
|
Chris@0
|
172 return $this->configuration[$method]['supported_auth'];
|
Chris@0
|
173 }
|
Chris@0
|
174 return [];
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * {@inheritdoc}
|
Chris@0
|
179 */
|
Chris@0
|
180 public function getFormats($method) {
|
Chris@0
|
181 switch ($this->granularity) {
|
Chris@0
|
182 case RestResourceConfigInterface::METHOD_GRANULARITY:
|
Chris@0
|
183 return $this->getFormatsForMethodGranularity($method);
|
Chris@0
|
184 case RestResourceConfigInterface::RESOURCE_GRANULARITY:
|
Chris@0
|
185 return $this->configuration['formats'];
|
Chris@0
|
186 default:
|
Chris@0
|
187 throw new \InvalidArgumentException('Invalid granularity specified.');
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * Retrieves a list of supported response formats.
|
Chris@0
|
193 *
|
Chris@0
|
194 * @param string $method
|
Chris@0
|
195 * The request method e.g GET or POST.
|
Chris@0
|
196 *
|
Chris@0
|
197 * @return string[]
|
Chris@0
|
198 * A list of supported format IDs.
|
Chris@0
|
199 */
|
Chris@0
|
200 protected function getFormatsForMethodGranularity($method) {
|
Chris@0
|
201 $method = $this->normalizeRestMethod($method);
|
Chris@0
|
202 if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_formats'])) {
|
Chris@0
|
203 return $this->configuration[$method]['supported_formats'];
|
Chris@0
|
204 }
|
Chris@0
|
205 return [];
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * {@inheritdoc}
|
Chris@0
|
210 */
|
Chris@0
|
211 public function getPluginCollections() {
|
Chris@0
|
212 return [
|
Chris@17
|
213 'resource' => new DefaultSingleLazyPluginCollection($this->getResourcePluginManager(), $this->plugin_id, []),
|
Chris@0
|
214 ];
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * (@inheritdoc)
|
Chris@0
|
219 */
|
Chris@0
|
220 public function calculateDependencies() {
|
Chris@0
|
221 parent::calculateDependencies();
|
Chris@0
|
222
|
Chris@0
|
223 foreach ($this->getRestResourceDependencies()->calculateDependencies($this) as $type => $dependencies) {
|
Chris@0
|
224 foreach ($dependencies as $dependency) {
|
Chris@0
|
225 $this->addDependency($type, $dependency);
|
Chris@0
|
226 }
|
Chris@0
|
227 }
|
Chris@0
|
228 return $this;
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 /**
|
Chris@0
|
232 * {@inheritdoc}
|
Chris@0
|
233 */
|
Chris@0
|
234 public function onDependencyRemoval(array $dependencies) {
|
Chris@0
|
235 $parent = parent::onDependencyRemoval($dependencies);
|
Chris@0
|
236
|
Chris@0
|
237 // If the dependency problems are not marked as fixed at this point they
|
Chris@0
|
238 // should be related to the resource plugin and the config entity should
|
Chris@0
|
239 // be deleted.
|
Chris@0
|
240 $changed = $this->getRestResourceDependencies()->onDependencyRemoval($this, $dependencies);
|
Chris@0
|
241 return $parent || $changed;
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * Returns the REST resource dependencies.
|
Chris@0
|
246 *
|
Chris@0
|
247 * @return \Drupal\rest\Entity\ConfigDependencies
|
Chris@0
|
248 */
|
Chris@0
|
249 protected function getRestResourceDependencies() {
|
Chris@0
|
250 return \Drupal::service('class_resolver')->getInstanceFromDefinition(ConfigDependencies::class);
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * Normalizes the method.
|
Chris@0
|
255 *
|
Chris@0
|
256 * @param string $method
|
Chris@0
|
257 * The request method.
|
Chris@0
|
258 *
|
Chris@0
|
259 * @return string
|
Chris@0
|
260 * The normalized request method.
|
Chris@0
|
261 */
|
Chris@0
|
262 protected function normalizeRestMethod($method) {
|
Chris@0
|
263 return strtoupper($method);
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * {@inheritdoc}
|
Chris@0
|
268 */
|
Chris@0
|
269 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
270 parent::postSave($storage, $update);
|
Chris@0
|
271
|
Chris@0
|
272 \Drupal::service('router.builder')->setRebuildNeeded();
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@0
|
276 * {@inheritdoc}
|
Chris@0
|
277 */
|
Chris@0
|
278 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
279 parent::postDelete($storage, $entities);
|
Chris@0
|
280
|
Chris@0
|
281 \Drupal::service('router.builder')->setRebuildNeeded();
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 }
|