Open Data Catalog v
SchemaRetriever.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\metastore;
4 
5 use Contracts\RetrieverInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Extension\ModuleExtensionList;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 
13 class SchemaRetriever implements RetrieverInterface, ContainerInjectionInterface {
14 
20  protected $directory;
21 
27  public static function create(ContainerInterface $container) {
28  $appRoot = $container->getParameter('app.root');
29  $moduleExtension = $container->get('extension.list.module');
30 
31  return new static($appRoot, $moduleExtension);
32  }
33 
37  public function __construct($appRoot, ModuleExtensionList $extensionList) {
38  $this->findSchemaDirectory($appRoot, $extensionList);
39  }
40 
44  public function getAllIds() {
45  return [
46  'catalog',
47  'dataset',
48  'dataset.ui',
49  'publisher',
50  'publisher.ui',
51  'distribution',
52  'distribution.ui',
53  'theme',
54  'theme.ui',
55  'keyword',
56  'keyword.ui',
57  'data-dictionary',
58  ];
59  }
60 
64  public function getSchemaDirectory() {
65  return $this->directory;
66  }
67 
71  public function retrieve(string $id): ?string {
72 
73  $filename = $this->getSchemaDirectory() . "/collections/{$id}.json";
74 
75  if (in_array($id, $this->getAllIds())
76  && is_readable($filename)
77  ) {
78  return file_get_contents($filename);
79  }
80  throw new \Exception("Schema {$id} not found.");
81  }
82 
86  protected function findSchemaDirectory($appRoot, $extensionList) {
87 
88  $drupalRoot = $appRoot;
89  $drupalRootSchema = $drupalRoot . "/schema";
90 
91  $defaultSchema = $drupalRoot . "/" . $this->getDefaultSchemaDirectory($extensionList);
92 
93  if (is_dir($drupalRootSchema)) {
94  $this->directory = $drupalRootSchema;
95  }
96  elseif (is_dir($defaultSchema)) {
97  $this->directory = $defaultSchema;
98  }
99  else {
100  throw new \Exception("No schema directory found.");
101  }
102  }
103 
112  protected function getDefaultSchemaDirectory(ModuleExtensionList $extensionList) {
113  $infoFile = $extensionList->getPathname('dkan');
114  return dirname($infoFile) . '/schema';
115  }
116 
117 }
Drupal\metastore\SchemaRetriever\$directory
$directory
Definition: SchemaRetriever.php:20
Drupal\metastore\SchemaRetriever\getSchemaDirectory
getSchemaDirectory()
Definition: SchemaRetriever.php:64
Drupal\metastore\SchemaRetriever\getDefaultSchemaDirectory
getDefaultSchemaDirectory(ModuleExtensionList $extensionList)
Definition: SchemaRetriever.php:112
Drupal\metastore
Drupal\metastore\SchemaRetriever\__construct
__construct($appRoot, ModuleExtensionList $extensionList)
Definition: SchemaRetriever.php:37
Drupal\metastore\SchemaRetriever\retrieve
retrieve(string $id)
Definition: SchemaRetriever.php:71
Drupal\metastore\SchemaRetriever\create
static create(ContainerInterface $container)
Definition: SchemaRetriever.php:27
Drupal\metastore\SchemaRetriever\getAllIds
getAllIds()
Definition: SchemaRetriever.php:44
Drupal\metastore\SchemaRetriever\findSchemaDirectory
findSchemaDirectory($appRoot, $extensionList)
Definition: SchemaRetriever.php:86
Drupal\metastore\SchemaRetriever
Definition: SchemaRetriever.php:13