From 8d6c1800b9fe319f702dc8999612a3adfdb4aca6 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sun, 30 Oct 2022 23:02:14 +0100 Subject: [PATCH] Cleanup, bugfixes --- bin/dataset-info | 15 ++++++++++++--- src/Dataset.php | 10 +++++++++- src/DatasetManager.php | 29 +++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/bin/dataset-info b/bin/dataset-info index 6cf73bf..7e294cf 100755 --- a/bin/dataset-info +++ b/bin/dataset-info @@ -3,11 +3,12 @@ foreach ([ __DIR__."/../vendor", - __DIR__."/../../../vendor" + __DIR__."/../../../vendor", + __DIR__."/vendor", ] as $dir) { if (file_exists($dir."/autoload.php")) { + define("COMPOSER_VENDOR_PATH", $dir); require_once $dir."/autoload.php"; - echo $dir."\n"; } } @@ -16,5 +17,13 @@ $datasetManager = new NoccyLabs\Dataset\DatasetManager(); $datasets = $datasetManager->getAvailableDatasets(); 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"; } diff --git a/src/Dataset.php b/src/Dataset.php index 0eb2f1a..71eba33 100644 --- a/src/Dataset.php +++ b/src/Dataset.php @@ -11,10 +11,13 @@ class Dataset 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->options = $options; + $this->version = $version; } public function getIdentifier(): string @@ -22,6 +25,11 @@ class Dataset return $this->identifier; } + public function getVersion(): ?string + { + return $this->version; + } + public function open(): ReaderInterface { $filename = $this->options['filename']; diff --git a/src/DatasetManager.php b/src/DatasetManager.php index bd64c7d..ecfa9c1 100644 --- a/src/DatasetManager.php +++ b/src/DatasetManager.php @@ -11,9 +11,9 @@ namespace NoccyLabs\Dataset; */ class DatasetManager { + private static array $packageVersions = []; - - private static $datasets = []; + private static array $datasets = []; public function __construct() { @@ -47,6 +47,15 @@ class DatasetManager $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) { $path = dirname($match); $package = basename(dirname($path))."/".basename($path); @@ -81,20 +90,24 @@ class DatasetManager 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); + foreach ($datasets as $name=>$options) { + if (!array_key_exists('filename', $options)) { + $this->loadDatasets($options, ltrim($prefix . "." . $name, "."), $package, $path); return; } - $info['filename'] = $path . "/" . $info['filename']; + $options['filename'] = $path . "/" . $options['filename']; $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); } } private function determineVendorPath(): ?string { + if (defined("COMPOSER_VENDOR_PATH")) { + return COMPOSER_VENDOR_PATH; + } if (file_exists(__DIR__."/../../../autoload.php")) { // we are installed as a composer package return dirname(__DIR__, 3); @@ -127,4 +140,4 @@ class DatasetManager return self::$datasets[$identifier]; } -} \ No newline at end of file +}