Dataset scanning

This commit is contained in:
Chris 2022-09-03 14:11:20 +02:00
parent 3e4db35a47
commit 0542c7e844
2 changed files with 55 additions and 4 deletions

View File

@ -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

View File

@ -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);
}
}