Open Data Catalog v2.0.0
SchemaRetriever.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Contracts\RetrieverInterface;
6 use Drupal\Core\Extension\ExtensionList;
7 
11 class SchemaRetriever implements RetrieverInterface {
12 
18  protected $directory;
19 
23  public function __construct($appRoot, ExtensionList $extensionList) {
24  $this->findSchemaDirectory($appRoot, $extensionList);
25  }
26 
30  public function getAllIds() {
31  return [
32  'catalog',
33  'dataset',
34  'dataset.ui',
35  ];
36  }
37 
41  public function getSchemaDirectory() {
42  return $this->directory;
43  }
44 
48  public function retrieve(string $id): ?string {
49 
50  $filename = $this->getSchemaDirectory() . "/collections/{$id}.json";
51 
52  if (in_array($id, $this->getAllIds())
53  && is_readable($filename)
54  ) {
55  return file_get_contents($filename);
56  }
57  throw new \Exception("Schema {$id} not found.");
58  }
59 
63  protected function findSchemaDirectory($appRoot, $extensionList) {
64 
65  $drupalRoot = $appRoot;
66 
67  if (is_dir($drupalRoot . "/schema")) {
68  $this->directory = $drupalRoot . "/schema";
69  }
70  elseif (($directory = $this->getDefaultSchemaDirectory($extensionList))
71  && is_dir($directory)
72  ) {
73  $this->directory = $directory;
74  }
75  else {
76  throw new \Exception("No schema directory found.");
77  }
78  }
79 
88  protected function getDefaultSchemaDirectory($extensionList) {
90  $extensionList = $extensionList;
91  $infoFile = $extensionList->getPathname('dkan2');
92 
93  return dirname($infoFile) . '/schema';
94  }
95 
96 }
Drupal\dkan_schema\SchemaRetriever\findSchemaDirectory
findSchemaDirectory($appRoot, $extensionList)
Definition: SchemaRetriever.php:63
Drupal\dkan_schema\SchemaRetriever\getAllIds
getAllIds()
Definition: SchemaRetriever.php:30
Drupal\dkan_schema\SchemaRetriever\getSchemaDirectory
getSchemaDirectory()
Definition: SchemaRetriever.php:41
Drupal\dkan_schema\SchemaRetriever
Definition: SchemaRetriever.php:11
Drupal\dkan_schema
Definition: SchemaRetriever.php:3
Drupal\dkan_schema\SchemaRetriever\$directory
$directory
Definition: SchemaRetriever.php:18
Drupal\dkan_schema\SchemaRetriever\retrieve
retrieve(string $id)
Definition: SchemaRetriever.php:48
Drupal\dkan_schema\SchemaRetriever\__construct
__construct($appRoot, ExtensionList $extensionList)
Definition: SchemaRetriever.php:23