annotate core/modules/rest/src/Entity/RestResourceConfig.php @ 0:c75dbcec494b

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