Open Data Catalog v
DkanApiDocsGenerator.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\common;
4 
6 use Drupal\Core\Site\Settings;
9 
14 
21 
27  protected Settings $settings;
28 
37  public function __construct(DkanApiDocsPluginManager $dkanApiDocsPluginManager, Settings $settings) {
38  $this->docManager = $dkanApiDocsPluginManager;
39  $this->settings = $settings;
40  }
41 
51  public function buildSpec(array $plugins = []) {
52  $docPluginDefinitions = $this->docManager->getDefinitions();
53  $spec = [];
54  if (!empty($plugins)) {
55  $this->filterPluginDefinitions($docPluginDefinitions, $plugins);
56  }
57  foreach ($docPluginDefinitions as $definition) {
58  $pluginSpec = $this->docManager->createInstance($definition['id'])->spec();
59  $spec = array_merge_recursive($spec, $pluginSpec);
60  }
61 
62  // Add 'dkan_api_base' setting to prefix the API path if your routing
63  // does not start with your site's root URL (e.g. "data" for API paths
64  // to use "/data/api/1").
65  if ($dkanApiBase = $this->settings->get('dkan_api_base')) {
66  $spec = ApiDocsPathModifier::prepend($spec, $dkanApiBase);
67  }
68 
69  return new OpenApiSpec(json_encode($spec));
70  }
71 
80  private function filterPluginDefinitions(array &$definitions, array $plugins) {
81  // We always include common_dkan_api_docs.
82  $plugins[] = 'common_dkan_api_docs';
83 
84  $definitions = array_filter($definitions, function ($key) use ($plugins) {
85  if (in_array($key, $plugins)) {
86  return TRUE;
87  }
88  return FALSE;
89  }, ARRAY_FILTER_USE_KEY);
90  }
91 
92 }
Drupal\common\DkanApiDocsGenerator\$docManager
DkanApiDocsPluginManager $docManager
Definition: DkanApiDocsGenerator.php:20
Drupal\common\Plugin\OpenApiSpec
Definition: OpenApiSpec.php:10
Drupal\common\Util\ApiDocsPathModifier
Definition: ApiDocsPathModifier.php:8
Drupal\common\DkanApiDocsGenerator\$settings
Settings $settings
Definition: DkanApiDocsGenerator.php:27
Drupal\common\DkanApiDocsGenerator\__construct
__construct(DkanApiDocsPluginManager $dkanApiDocsPluginManager, Settings $settings)
Definition: DkanApiDocsGenerator.php:37
Drupal\common\DkanApiDocsGenerator
Definition: DkanApiDocsGenerator.php:13
Drupal\common
Drupal\common\DkanApiDocsGenerator\buildSpec
buildSpec(array $plugins=[])
Definition: DkanApiDocsGenerator.php:51
Drupal\common\Util\ApiDocsPathModifier\prepend
static prepend(array $spec, string $pathFragment)
Definition: ApiDocsPathModifier.php:21
Drupal\common\Plugin\DkanApiDocsPluginManager
Definition: DkanApiDocsPluginManager.php:14