RestInspectionTrait.php in OpenAPI 8
File
src/RestInspectionTrait.php
View source
<?php
namespace Drupal\openapi;
use Drupal\rest\Plugin\rest\resource\EntityResource;
use Drupal\rest\RestResourceConfigInterface;
trait RestInspectionTrait {
protected function getRestEnabledEntityTypes($entity_type_id = NULL) {
$entity_types = [];
$resource_configs = $this
->getResourceConfigs();
foreach ($resource_configs as $resource_config) {
if ($entity_type = $this
->getEntityType($resource_config)) {
if (!$entity_type_id || $entity_type
->id() == $entity_type_id) {
$entity_types[$entity_type
->id()] = $entity_type;
}
}
}
return $entity_types;
}
protected function getResourceConfigs(array $options = []) {
if (isset($options['entity_type_id'])) {
$resource_configs[] = $this->entityTypeManager
->getStorage('rest_resource_config')
->load("entity.{$options['entity_type_id']}");
}
else {
$resource_configs = $this->entityTypeManager
->getStorage('rest_resource_config')
->loadMultiple();
if (isset($options['resource_types']) && $options['resource_types'] == 'entities') {
foreach (array_keys($resource_configs) as $resource_key) {
if (!$this
->isEntityResource($resource_configs[$resource_key])) {
unset($resource_configs[$resource_key]);
}
}
}
}
return $resource_configs;
}
protected function isEntityResource(RestResourceConfigInterface $resource_config) {
$resource_plugin = $resource_config
->getResourcePlugin();
return $resource_plugin instanceof EntityResource;
}
protected function getEntityType(RestResourceConfigInterface $resource_config) {
if ($this
->isEntityResource($resource_config)) {
$resource_plugin = $resource_config
->getResourcePlugin();
return $this->entityTypeManager
->getDefinition($resource_plugin
->getPluginDefinition()['entity_type']);
}
return NULL;
}
}