From 90d4e89a230b19dad3ef471b5f67afe0abc7867d Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 14 Jun 2019 14:07:19 +0100 Subject: [PATCH] Handle version numbers for #2 --- composer.json | 3 +- composer.lock | 170 +++++++++++++------------- src/Application.php | 57 ++------- src/Argument/ArgumentValueList.php | 11 +- src/Argument/DefaultArgumentValue.php | 2 +- src/Command/Command.php | 4 +- src/Command/VersionCommand.php | 91 ++++++++++++++ 7 files changed, 203 insertions(+), 135 deletions(-) create mode 100644 src/Command/VersionCommand.php diff --git a/composer.json b/composer.json index 92c0bb2..4f5df27 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "url": "/home/g105b/Code/PhpGt/*" } ], + "minimum-stability": "dev", "name": "phpgt/cli", "description": "Command line interface builder.", @@ -43,4 +44,4 @@ "Gt\\Cli\\Test\\": "./test/unit" } } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index d2d9bc6..d252987 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "36433ce43d1ff2f86ed8ea2c597981ab", + "content-hash": "cdc04f7a259db667007ca22f17977df8", "packages": [ { "name": "phpgt/daemon", @@ -37,7 +37,7 @@ "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.2.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", @@ -93,7 +93,7 @@ }, { "name": "myclabs/deep-copy", - "version": "1.9.1", + "version": "1.x-dev", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", @@ -141,7 +141,7 @@ }, { "name": "phar-io/manifest", - "version": "1.0.3", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", @@ -395,16 +395,16 @@ }, { "name": "phpspec/prophecy", - "version": "1.8.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06" + "reference": "7e272180527c34a97680de85eb5aba0847a664e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/4ba436b55987b4bf311cb7c6ba82aa528aac0a06", - "reference": "4ba436b55987b4bf311cb7c6ba82aa528aac0a06", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/7e272180527c34a97680de85eb5aba0847a664e0", + "reference": "7e272180527c34a97680de85eb5aba0847a664e0", "shasum": "" }, "require": { @@ -425,8 +425,8 @@ } }, "autoload": { - "psr-0": { - "Prophecy\\": "src/" + "psr-4": { + "Prophecy\\": "src/Prophecy" } }, "notification-url": "https://packagist.org/downloads/", @@ -454,20 +454,20 @@ "spy", "stub" ], - "time": "2018-08-05T17:53:17+00:00" + "time": "2018-12-18T15:40:51+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "7.0.3", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "0317a769a81845c390e19684d9ba25d7f6aa4707" + "reference": "d8ff5ed364cb7a1a32e6040076017b38f6cbdf07" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0317a769a81845c390e19684d9ba25d7f6aa4707", - "reference": "0317a769a81845c390e19684d9ba25d7f6aa4707", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/d8ff5ed364cb7a1a32e6040076017b38f6cbdf07", + "reference": "d8ff5ed364cb7a1a32e6040076017b38f6cbdf07", "shasum": "" }, "require": { @@ -517,20 +517,20 @@ "testing", "xunit" ], - "time": "2019-02-26T07:38:26+00:00" + "time": "2019-03-29T06:00:42+00:00" }, { "name": "phpunit/php-file-iterator", - "version": "2.0.2", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "050bedf145a257b1ff02746c31894800e5122946" + "reference": "5e99c0ea25f1eb9bd4d7380499788302984dd77b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/050bedf145a257b1ff02746c31894800e5122946", - "reference": "050bedf145a257b1ff02746c31894800e5122946", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/5e99c0ea25f1eb9bd4d7380499788302984dd77b", + "reference": "5e99c0ea25f1eb9bd4d7380499788302984dd77b", "shasum": "" }, "require": { @@ -567,7 +567,7 @@ "filesystem", "iterator" ], - "time": "2018-09-13T20:33:42+00:00" + "time": "2019-02-11T12:49:18+00:00" }, { "name": "phpunit/php-text-template", @@ -612,16 +612,16 @@ }, { "name": "phpunit/php-timer", - "version": "2.1.1", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "8b389aebe1b8b0578430bda0c7c95a829608e059" + "reference": "eb9e39fb4c2034c31897cdb5f59498fc9126ddcc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/8b389aebe1b8b0578430bda0c7c95a829608e059", - "reference": "8b389aebe1b8b0578430bda0c7c95a829608e059", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/eb9e39fb4c2034c31897cdb5f59498fc9126ddcc", + "reference": "eb9e39fb4c2034c31897cdb5f59498fc9126ddcc", "shasum": "" }, "require": { @@ -657,20 +657,20 @@ "keywords": [ "timer" ], - "time": "2019-02-20T10:12:59+00:00" + "time": "2019-02-20T14:16:29+00:00" }, { "name": "phpunit/php-token-stream", - "version": "3.0.1", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "c99e3be9d3e85f60646f152f9002d46ed7770d18" + "reference": "cca9f57a2c7bb3d1e294f2aae84083ffe83dfa92" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/c99e3be9d3e85f60646f152f9002d46ed7770d18", - "reference": "c99e3be9d3e85f60646f152f9002d46ed7770d18", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/cca9f57a2c7bb3d1e294f2aae84083ffe83dfa92", + "reference": "cca9f57a2c7bb3d1e294f2aae84083ffe83dfa92", "shasum": "" }, "require": { @@ -706,20 +706,20 @@ "keywords": [ "tokenizer" ], - "time": "2018-10-30T05:52:18+00:00" + "time": "2019-02-11T12:50:48+00:00" }, { "name": "phpunit/phpunit", - "version": "8.1.2", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e7450b51b6f5d29edcd645ff72b355ab0633ca35" + "reference": "893a342763f454e029579b6a5175cd8c4a408168" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e7450b51b6f5d29edcd645ff72b355ab0633ca35", - "reference": "e7450b51b6f5d29edcd645ff72b355ab0633ca35", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/893a342763f454e029579b6a5175cd8c4a408168", + "reference": "893a342763f454e029579b6a5175cd8c4a408168", "shasum": "" }, "require": { @@ -762,7 +762,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "8.1-dev" + "dev-master": "8.2-dev" } }, "autoload": { @@ -788,20 +788,20 @@ "testing", "xunit" ], - "time": "2019-04-08T16:03:02+00:00" + "time": "2019-04-08T16:09:04+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", - "version": "1.0.1", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", - "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" + "reference": "383c44e104c1fd46ecc915f55145bd2831318747" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", - "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/383c44e104c1fd46ecc915f55145bd2831318747", + "reference": "383c44e104c1fd46ecc915f55145bd2831318747", "shasum": "" }, "require": { @@ -833,20 +833,20 @@ ], "description": "Looks up which function or method a line of code belongs to", "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2017-03-04T06:30:41+00:00" + "time": "2019-02-11T12:48:46+00:00" }, { "name": "sebastian/comparator", - "version": "3.0.2", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da" + "reference": "17ef3ffcdab9194ad7a2715a9fb1235f1361a366" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/5de4fc177adf9bce8df98d8d141a7559d7ccf6da", - "reference": "5de4fc177adf9bce8df98d8d141a7559d7ccf6da", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/17ef3ffcdab9194ad7a2715a9fb1235f1361a366", + "reference": "17ef3ffcdab9194ad7a2715a9fb1235f1361a366", "shasum": "" }, "require": { @@ -897,20 +897,20 @@ "compare", "equality" ], - "time": "2018-07-12T15:12:46+00:00" + "time": "2019-02-11T12:51:04+00:00" }, { "name": "sebastian/diff", - "version": "3.0.2", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "720fcc7e9b5cf384ea68d9d930d480907a0c1a29" + "reference": "d4193340fc9bebb17533e00bc82f61da28fe7125" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/720fcc7e9b5cf384ea68d9d930d480907a0c1a29", - "reference": "720fcc7e9b5cf384ea68d9d930d480907a0c1a29", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/d4193340fc9bebb17533e00bc82f61da28fe7125", + "reference": "d4193340fc9bebb17533e00bc82f61da28fe7125", "shasum": "" }, "require": { @@ -953,20 +953,20 @@ "unidiff", "unified diff" ], - "time": "2019-02-04T06:01:07+00:00" + "time": "2019-02-11T12:50:35+00:00" }, { "name": "sebastian/environment", - "version": "4.1.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "6fda8ce1974b62b14935adc02a9ed38252eca656" + "reference": "dbf7b0d103084622e8a5015452a2650dcb248758" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6fda8ce1974b62b14935adc02a9ed38252eca656", - "reference": "6fda8ce1974b62b14935adc02a9ed38252eca656", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/dbf7b0d103084622e8a5015452a2650dcb248758", + "reference": "dbf7b0d103084622e8a5015452a2650dcb248758", "shasum": "" }, "require": { @@ -1006,20 +1006,20 @@ "environment", "hhvm" ], - "time": "2019-02-01T05:27:49+00:00" + "time": "2019-02-11T12:51:52+00:00" }, { "name": "sebastian/exporter", - "version": "3.1.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "234199f4528de6d12aaa58b612e98f7d36adb937" + "reference": "8be786b3b65fbe706733d44a4b4a53d5391a4772" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/234199f4528de6d12aaa58b612e98f7d36adb937", - "reference": "234199f4528de6d12aaa58b612e98f7d36adb937", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/8be786b3b65fbe706733d44a4b4a53d5391a4772", + "reference": "8be786b3b65fbe706733d44a4b4a53d5391a4772", "shasum": "" }, "require": { @@ -1073,20 +1073,20 @@ "export", "exporter" ], - "time": "2017-04-03T13:19:02+00:00" + "time": "2019-02-11T12:49:46+00:00" }, { "name": "sebastian/global-state", - "version": "3.0.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4" + "reference": "4b60cdb437ccff1871814451541cbe41f4b8ac58" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4", - "reference": "edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/4b60cdb437ccff1871814451541cbe41f4b8ac58", + "reference": "4b60cdb437ccff1871814451541cbe41f4b8ac58", "shasum": "" }, "require": { @@ -1127,20 +1127,20 @@ "keywords": [ "global state" ], - "time": "2019-02-01T05:30:01+00:00" + "time": "2019-02-11T12:52:08+00:00" }, { "name": "sebastian/object-enumerator", - "version": "3.0.3", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/object-enumerator.git", - "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5" + "reference": "41af86e2a7b06e7e364c81a705b1f7caf6110218" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/7cfd9e65d11ffb5af41198476395774d4c8a84c5", - "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/41af86e2a7b06e7e364c81a705b1f7caf6110218", + "reference": "41af86e2a7b06e7e364c81a705b1f7caf6110218", "shasum": "" }, "require": { @@ -1174,20 +1174,20 @@ ], "description": "Traverses array structures and object graphs to enumerate all referenced objects", "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2017-08-03T12:35:26+00:00" + "time": "2019-02-11T12:50:05+00:00" }, { "name": "sebastian/object-reflector", - "version": "1.1.1", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/object-reflector.git", - "reference": "773f97c67f28de00d397be301821b06708fca0be" + "reference": "fae17b5d19ab523c9e821e5559d27e4c8a5bdee1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", - "reference": "773f97c67f28de00d397be301821b06708fca0be", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/fae17b5d19ab523c9e821e5559d27e4c8a5bdee1", + "reference": "fae17b5d19ab523c9e821e5559d27e4c8a5bdee1", "shasum": "" }, "require": { @@ -1219,20 +1219,20 @@ ], "description": "Allows reflection of object attributes, including inherited and non-public ones", "homepage": "https://github.com/sebastianbergmann/object-reflector/", - "time": "2017-03-29T09:07:27+00:00" + "time": "2019-02-11T12:48:12+00:00" }, { "name": "sebastian/recursion-context", - "version": "3.0.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8" + "reference": "87b0893f697db6d75943e26d50bf91c82796a371" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", - "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/87b0893f697db6d75943e26d50bf91c82796a371", + "reference": "87b0893f697db6d75943e26d50bf91c82796a371", "shasum": "" }, "require": { @@ -1272,11 +1272,11 @@ ], "description": "Provides functionality to recursively process PHP variables", "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2017-03-03T06:23:57+00:00" + "time": "2019-02-11T12:48:28+00:00" }, { "name": "sebastian/resource-operations", - "version": "2.0.1", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", @@ -1361,7 +1361,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.11.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -1510,7 +1510,7 @@ } ], "aliases": [], - "minimum-stability": "stable", + "minimum-stability": "dev", "stability-flags": { "phpgt/daemon": 20 }, diff --git a/src/Application.php b/src/Application.php index e63ad21..0f99509 100644 --- a/src/Application.php +++ b/src/Application.php @@ -7,6 +7,7 @@ use Gt\Cli\Command\CommandException; use Gt\Cli\Command\HelpCommand; use Gt\Cli\Command\InvalidCommandException; +use Gt\Cli\Command\VersionCommand; use Gt\Cli\Parameter\MissingRequiredParameterException; use Gt\Cli\Parameter\MissingRequiredParameterValueException; use Gt\Cli\Argument\ArgumentList; @@ -17,6 +18,7 @@ class Application { protected $commands; protected $stream; protected $helpCommand; + protected $versionCommand; public function __construct( string $description, @@ -32,7 +34,12 @@ public function __construct( $arguments->getScript(), $this->commands ); + $this->versionCommand = new VersionCommand( + $arguments->getScript() + ); + $this->commands []= $this->helpCommand; + $this->commands []= $this->versionCommand; $this->stream = new Stream( "php://stdin", @@ -40,6 +47,7 @@ public function __construct( "php://stderr" ); $this->helpCommand->setOutput($this->stream); + $this->versionCommand->setOutput($this->stream); } public function setStream($in, $out, $error) { @@ -77,7 +85,9 @@ public function run():void { return; case "version": - $this->stream->writeLine($this->getVersion()); + $versionArgs = new ArgumentValueList(); + $versionArgs->set("command", $commandName); + $this->versionCommand->run($versionArgs); return; } } @@ -112,6 +122,8 @@ public function run():void { } protected function findCommandByName(string $name):Command { + $name = trim($name, "-"); + foreach($this->commands as $command) { if($command->getName() !== $name) { continue; @@ -122,47 +134,4 @@ protected function findCommandByName(string $name):Command { throw new InvalidCommandException($name); } - - protected function getVersion():string { - $version = "Version number not found"; - - $dir = __DIR__; - do { - $dir = dirname($dir); - $files = scandir($dir); - } - while(!in_array("vendor", $files) || strlen($dir) <= 4); - - $installedJson = implode(DIRECTORY_SEPARATOR, [ - $dir, - "vendor", - "composer", - "installed.json", - ]); - if(!is_file($installedJson)) { - return $version; - } - - $installed = json_decode(file_get_contents($installedJson)); - - $scriptName = $this->arguments->getScript(); - $scriptName = pathinfo($scriptName, PATHINFO_FILENAME); - - foreach($installed as $item) { - $binArray = $item->bin ?? []; - if(!empty($binArray)) -var_dump($binArray); - foreach($binArray as $bin) { - $bin = pathinfo($bin, PATHINFO_FILENAME); - if($bin === $scriptName) { - $version = $item->version; - break; - } - } - } - - var_dump($version);die(); - - return $version; - } } \ No newline at end of file diff --git a/src/Argument/ArgumentValueList.php b/src/Argument/ArgumentValueList.php index eaced3b..2636572 100644 --- a/src/Argument/ArgumentValueList.php +++ b/src/Argument/ArgumentValueList.php @@ -4,6 +4,8 @@ use Iterator; class ArgumentValueList implements Iterator { + const DEFAULT_ARGUMENT_VALUE = "//\\DEFAULT ARGUMENT VALUE\\//"; + /** @var ArgumentValue[] */ protected $valueList = []; protected $argumentValueMap = []; @@ -24,9 +26,14 @@ public function set(string $key, string $value = null):void { $this->argumentValueMap[$key] = $valueObject; } - public function get(string $key, string $default = null):ArgumentValue { + public function get( + string $key, + $default = self::DEFAULT_ARGUMENT_VALUE + ):ArgumentValue { +// $default is handled in this manner because we want to allow null to be a +// valid value for the default. if(!$this->contains($key)) { - if(!is_null($default)) { + if($default !== self::DEFAULT_ARGUMENT_VALUE) { return new DefaultArgumentValue($default); } diff --git a/src/Argument/DefaultArgumentValue.php b/src/Argument/DefaultArgumentValue.php index 4aa945b..114cd7e 100644 --- a/src/Argument/DefaultArgumentValue.php +++ b/src/Argument/DefaultArgumentValue.php @@ -2,7 +2,7 @@ namespace Gt\Cli\Argument; class DefaultArgumentValue extends ArgumentValue { - public function __construct(string $default) { + public function __construct(?string $default) { parent::__construct("__DEFAULT__"); $this->push($default); } diff --git a/src/Command/Command.php b/src/Command/Command.php index 66168d1..9aef5e4 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -22,6 +22,8 @@ public function setOutput(Stream $output = null) { $this->output = $output; } + abstract public function run(ArgumentValueList $arguments = null):void; + abstract public function getName():string; abstract public function getDescription():string; @@ -38,8 +40,6 @@ abstract public function getRequiredParameterList():array; /** @return Parameter[] */ abstract public function getOptionalParameterList():array; - abstract public function run(ArgumentValueList $arguments = null):void; - public function getUsage(bool $includeDocumentation = false):string { $message = ""; diff --git a/src/Command/VersionCommand.php b/src/Command/VersionCommand.php new file mode 100644 index 0000000..b649ba5 --- /dev/null +++ b/src/Command/VersionCommand.php @@ -0,0 +1,91 @@ +script = $script; + } + + public function run(ArgumentValueList $arguments = null):void { + $this->writeLine($this->getVersion($arguments->get( + "command", + null + ))); + } + + public function getName():string { + return "version"; + } + + public function getDescription():string { + return "Get the version of the application"; + } + + /** @return NamedParameter[] */ + public function getRequiredNamedParameterList():array { + return []; + } + + /** @return NamedParameter[] */ + public function getOptionalNamedParameterList():array { + return []; + } + + /** @return Parameter[] */ + public function getRequiredParameterList():array { + return []; + } + + /** @return Parameter[] */ + public function getOptionalParameterList():array { + return []; + } + + protected function getVersion(string $command = null):string { + $version = "Version number not found"; + + $nestedDirectoryCount = 25; + $dir = __DIR__; + do { + $dir = dirname($dir); + $files = scandir($dir); + $nestedDirectoryCount--; + } + while(!in_array("vendor", $files) + || $nestedDirectoryCount < 1); + + $installedJson = implode(DIRECTORY_SEPARATOR, [ + $dir, + "vendor", + "composer", + "installed.json", + ]); + if(!is_file($installedJson)) { + return $version; + } + + $installed = json_decode(file_get_contents($installedJson)); + + $scriptName = $command ?? $this->script; + $scriptName = pathinfo($scriptName, PATHINFO_FILENAME); + + foreach($installed as $item) { + $binArray = $item->bin ?? []; + foreach($binArray as $bin) { + $bin = pathinfo($bin, PATHINFO_FILENAME); + if($bin === $scriptName) { + $version = $item->version; + break; + } + } + } + + return $version; + } +} \ No newline at end of file