Cleanup, bugfixes

This commit is contained in:
Chris 2022-10-30 23:02:14 +01:00
parent c3e651440f
commit 8d6c1800b9
3 changed files with 42 additions and 12 deletions

View File

@ -3,11 +3,12 @@
foreach ([ foreach ([
__DIR__."/../vendor", __DIR__."/../vendor",
__DIR__."/../../../vendor" __DIR__."/../../../vendor",
__DIR__."/vendor",
] as $dir) { ] as $dir) {
if (file_exists($dir."/autoload.php")) { if (file_exists($dir."/autoload.php")) {
define("COMPOSER_VENDOR_PATH", $dir);
require_once $dir."/autoload.php"; require_once $dir."/autoload.php";
echo $dir."\n";
} }
} }
@ -16,5 +17,13 @@ $datasetManager = new NoccyLabs\Dataset\DatasetManager();
$datasets = $datasetManager->getAvailableDatasets(); $datasets = $datasetManager->getAvailableDatasets();
foreach ($datasets as $dataset) { foreach ($datasets as $dataset) {
echo $dataset->getIdentifier()."\n"; echo $dataset->getIdentifier()." (".$dataset->getVersion().")\n";
$reader = $dataset->open();
$rows = 0;
$headers = null;
foreach ($reader as $row) {
if (!$headers) $headers = array_keys($row);
$rows++;
}
echo " ".$rows." rows\n - ".join("\n - ",$headers)."\n";
} }

View File

@ -11,10 +11,13 @@ class Dataset
protected array $options; protected array $options;
public function __construct(string $identifier, array $options) protected ?string $version;
public function __construct(string $identifier, array $options, ?string $version=null)
{ {
$this->identifier = $identifier; $this->identifier = $identifier;
$this->options = $options; $this->options = $options;
$this->version = $version;
} }
public function getIdentifier(): string public function getIdentifier(): string
@ -22,6 +25,11 @@ class Dataset
return $this->identifier; return $this->identifier;
} }
public function getVersion(): ?string
{
return $this->version;
}
public function open(): ReaderInterface public function open(): ReaderInterface
{ {
$filename = $this->options['filename']; $filename = $this->options['filename'];

View File

@ -11,9 +11,9 @@ namespace NoccyLabs\Dataset;
*/ */
class DatasetManager class DatasetManager
{ {
private static array $packageVersions = [];
private static array $datasets = [];
private static $datasets = [];
public function __construct() public function __construct()
{ {
@ -47,6 +47,15 @@ class DatasetManager
$glob = glob($root."/*/*/dataset.json"); $glob = glob($root."/*/*/dataset.json");
self::$packageVersions = [];
$fn = realpath($root."/composer/installed.php");
if (file_exists($fn)) {
$versions = include $fn;
foreach ($versions['versions'] as $name=>$version) {
self::$packageVersions[$name] = $version['version'];
}
}
foreach ($glob as $match) { foreach ($glob as $match) {
$path = dirname($match); $path = dirname($match);
$package = basename(dirname($path))."/".basename($path); $package = basename(dirname($path))."/".basename($path);
@ -81,20 +90,24 @@ class DatasetManager
private function loadDatasets(array $datasets, ?string $prefix, string $package, string $path) private function loadDatasets(array $datasets, ?string $prefix, string $package, string $path)
{ {
foreach ($datasets as $name=>$info) { foreach ($datasets as $name=>$options) {
if (!array_key_exists('filename', $info)) { if (!array_key_exists('filename', $options)) {
$this->loadDatasets($info, ltrim($prefix . "." . $name, "."), $package, $path); $this->loadDatasets($options, ltrim($prefix . "." . $name, "."), $package, $path);
return; return;
} }
$info['filename'] = $path . "/" . $info['filename']; $options['filename'] = $path . "/" . $options['filename'];
$pn = sprintf("%s#%s", $package, ltrim($prefix.".".$name,".")); $pn = sprintf("%s#%s", $package, ltrim($prefix.".".$name,"."));
$ds = new Dataset($pn, $info); $pv = self::$packageVersions[$package]??null;
$ds = new Dataset($pn, $options, $pv);
$this->registerDataset($ds); $this->registerDataset($ds);
} }
} }
private function determineVendorPath(): ?string private function determineVendorPath(): ?string
{ {
if (defined("COMPOSER_VENDOR_PATH")) {
return COMPOSER_VENDOR_PATH;
}
if (file_exists(__DIR__."/../../../autoload.php")) { if (file_exists(__DIR__."/../../../autoload.php")) {
// we are installed as a composer package // we are installed as a composer package
return dirname(__DIR__, 3); return dirname(__DIR__, 3);
@ -127,4 +140,4 @@ class DatasetManager
return self::$datasets[$identifier]; return self::$datasets[$identifier];
} }
} }