From 0542c7e844c46c733f3e50ac30141db043fbaa95 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 3 Sep 2022 14:11:20 +0200 Subject: [PATCH] Dataset scanning --- src/Dataset.php | 7 ++++-- src/DatasetManager.php | 52 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Dataset.php b/src/Dataset.php index 315d63c..e740795 100644 --- a/src/Dataset.php +++ b/src/Dataset.php @@ -5,11 +5,14 @@ namespace NoccyLabs\Dataset; class Dataset { - protected string $identfier; + protected string $identifier; + + protected array $options; - public function __construct(string $identifier, string $filename, array $metadata) + public function __construct(string $identifier, array $options) { $this->identifier = $identifier; + $this->options = $options; } public function getIdentifier(): string diff --git a/src/DatasetManager.php b/src/DatasetManager.php index 0af5b62..f6de99d 100644 --- a/src/DatasetManager.php +++ b/src/DatasetManager.php @@ -22,6 +22,16 @@ class DatasetManager } } + public function getAvailableDatasets(): array + { + return self::$datasets; + } + + /** + * Find the vendor directory and try to locate all bundled datasets + * + * + */ private function scanForDatasets() { $root = $this->determineVendorPath(); @@ -33,9 +43,47 @@ class DatasetManager $glob = glob($root."/*/*/dataset.json"); foreach ($glob as $match) { - $path = dirname($glob); + $path = dirname($match); $package = basename(dirname($path))."/".basename($path); - printf("found: %s\n", $package); + $this->scanPackageDatasets($package, $path); + } + } + + /** + * + * + * + * @param string The package name (org/package) + * @param string The full path to the package (..../vendor/org/package) + */ + private function scanPackageDatasets(string $package, string $path) + { + $file = $path."/dataset.json"; + $json = @json_decode( + @file_get_contents($file), + true + ); + + if (!$json) { + trigger_error("Unable to parse dataset.json in {$package}"); + return; + } + + $this->loadDatasets($json['datasets'], null, $package, $path); + //printf("found %d sets in %s\n", count($json['datasets']), $package); + + } + + private function loadDatasets(array $datasets, ?string $prefix, string $package, string $path) + { + foreach ($datasets as $name=>$info) { + if (!array_key_exists('filename', $info)) { + $this->loadDatasets($info, ltrim($prefix . "." . $name, "."), $package, $path); + return; + } + $pn = sprintf("%s#%s", $package, ltrim($prefix.".".$name,".")); + $ds = new Dataset($pn, $info); + $this->registerDataset($ds); } }