Open Data Catalog v2.0.0
Docs.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types = 1);
4 
6 
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Serialization\Yaml;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\JsonResponse;
12 use Symfony\Component\HttpFoundation\RequestStack;
13 
17 class Docs implements ContainerInjectionInterface {
18 
24  private $spec;
25 
31  private $moduleHandler;
32 
38  private $requestStack;
39 
45  private $serializer;
46 
52  public static function create(ContainerInterface $container) {
53  return new Docs(
54  $container->get('module_handler'),
55  $container->get('request_stack')
56  );
57  }
58 
62  public function __construct(
63  ModuleHandlerInterface $moduleHandler,
64  RequestStack $requestStack
65  ) {
66  $this->moduleHandler = $moduleHandler;
67  $this->requestStack = $requestStack;
68  $this->serializer = new Yaml();
69  $this->spec = $this->getJsonFromYmlFile();
70  }
71 
75  public function getVersions() {
76  return new JsonResponse(["version" => 1, "url" => "/api/1"]);
77  }
78 
85  public function getJsonFromYmlFile() {
86  $modulePath = $this->moduleHandler->getModule('dkan_api')->getPath();
87  $ymlSpecPath = $modulePath . '/docs/dkan_api_openapi_spec.yml';
88  $ymlSpec = file_get_contents($ymlSpecPath);
89 
90  return $this->serializer->decode($ymlSpec);
91  }
92 
99  public function getComplete() {
100  if ($this->requestStack->getCurrentRequest()->get('authentication') === "false") {
101  $spec = $this->getPublic();
102  }
103  else {
104  $spec = $this->spec;
105  }
106 
107  $jsonSpec = json_encode($spec);
108  return $this->sendResponse($jsonSpec);
109  }
110 
120  private function getPublic() {
121  $publicSpec = $this->removeAuthenticatedEndpoints($this->spec);
122  $cleanSpec = $this->cleanUpEndpoints($publicSpec);
123  unset($cleanSpec['components']['securitySchemes']);
124  return $cleanSpec;
125  }
126 
136  private function removeAuthenticatedEndpoints(array $spec) {
137  foreach ($spec['paths'] as $path => $operations) {
138  $this->removeAuthenticatedOperations($operations, $path, $spec);
139  }
140  return $spec;
141  }
142 
153  private function removeAuthenticatedOperations(array $operations, string $path, array &$spec) {
154  foreach ($operations as $operation => $details) {
155  if (isset($spec['paths'][$path][$operation]['security'])) {
156  unset($spec['paths'][$path][$operation]);
157  }
158  }
159  }
160 
170  private function cleanUpEndpoints(array $spec) {
171  foreach ($spec['paths'] as $path => $operations) {
172  if (empty($operations)) {
173  unset($spec['paths'][$path]);
174  }
175  }
176  return $spec;
177  }
178 
188  private function sendResponse(string $jsonSpec) {
189  return new JsonResponse(
190  $jsonSpec,
191  200,
192  [
193  'Content-type' => 'application/json',
194  'Access-Control-Allow-Origin' => '*',
195  ],
196  TRUE
197  );
198  }
199 
200 }
Drupal\dkan_api\Controller\Docs\__construct
__construct(ModuleHandlerInterface $moduleHandler, RequestStack $requestStack)
Definition: Docs.php:62
Drupal\dkan_api\Controller
Definition: Docs.php:5
Drupal\dkan_api\Controller\Docs\create
static create(ContainerInterface $container)
Definition: Docs.php:52
Drupal\dkan_api\Controller\Docs\getJsonFromYmlFile
getJsonFromYmlFile()
Definition: Docs.php:85
Drupal\dkan_api\Controller\Docs
Definition: Docs.php:17
Drupal\dkan_api\Controller\Docs\getComplete
getComplete()
Definition: Docs.php:99
Drupal\dkan_api\Controller\Docs\getVersions
getVersions()
Definition: Docs.php:75