70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace NoccyLabs\Dataset;
|
||
|
|
||
|
/**
|
||
|
* DatasetManager is the central class of noccylabs/dataset.
|
||
|
*
|
||
|
* @author Christopher Vagnetoft <cvagnetoft@gmail.com>
|
||
|
* @copyright (c) 2022, NoccyLabs
|
||
|
* @package noccylabs/dataset
|
||
|
*/
|
||
|
class DatasetManager
|
||
|
{
|
||
|
|
||
|
|
||
|
private static $datasets = [];
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
if (count(self::$datasets) == 0) {
|
||
|
$this->scanForDatasets();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function scanForDatasets()
|
||
|
{
|
||
|
$root = $this->determineVendorPath();
|
||
|
if (!$root) {
|
||
|
// Skip loading if we couldn't determine the root
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$glob = glob($root."/*/*/dataset.json");
|
||
|
}
|
||
|
|
||
|
private function determineVendorPath(): ?string
|
||
|
{
|
||
|
if (file_exists(__DIR__."/../../../autoload.php")) {
|
||
|
// we are installed as a composer package
|
||
|
return dirname(__DIR__, 3);
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public function registerDataset(Dataset $dataset)
|
||
|
{
|
||
|
$id = $dataset->getIdentifier();
|
||
|
if (array_key_exists($id, self::$datasets)) {
|
||
|
// Don't overwrite previously registered datasets. Investigate how
|
||
|
// this can be handled better in the future.
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
self::$datasets[$id] = $dataset;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*
|
||
|
* @throws InvalidDatasetException if the dataset can not be opened
|
||
|
* @throws UnknownDatasetExcception if the dataset does not exist
|
||
|
*/
|
||
|
public function getDataset(string $identifier): Dataset
|
||
|
{
|
||
|
|
||
|
return self::$datasets[$identifier];
|
||
|
}
|
||
|
}
|