From cd8964fbc33b6f1e983693b2fe60f6f9e85a242f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 28 Mar 2024 18:45:00 +0100 Subject: [PATCH] Remove BC break in util.cmd.Commands --- src/main/php/util/cmd/Commands.class.php | 17 ++++++++--------- .../php/xp/command/AbstractRunner.class.php | 4 ++-- src/main/php/xp/command/CmdRunner.class.php | 2 +- src/main/php/xp/command/Runner.class.php | 2 +- .../util/cmd/unittest/CommandsTest.class.php | 10 +++++----- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/php/util/cmd/Commands.class.php b/src/main/php/util/cmd/Commands.class.php index adddc84..d3834f4 100755 --- a/src/main/php/util/cmd/Commands.class.php +++ b/src/main/php/util/cmd/Commands.class.php @@ -1,7 +1,7 @@ getName().' is not runnable'); } - return Reflection::of($class); + return $class; } /** * Return name of a given class - shortened if inside a registered package * - * @param lang.reflection.Type $type + * @param lang.XPClass $class * @return string */ - public static function nameOf($type) { - if (isset(self::$packages[$type->package()->name()])) { - $name= $type->name(); - return false === ($p= strrpos($name, '.')) ? $name : substr($name, $p + 1); + public static function nameOf($class) { + if (isset(self::$packages[$class->getPackage()->getName()])) { + return $class->getSimpleName(); } else { - return $type->name(); + return $class->getName(); } } } \ No newline at end of file diff --git a/src/main/php/xp/command/AbstractRunner.class.php b/src/main/php/xp/command/AbstractRunner.class.php index 5b92224..9821b86 100755 --- a/src/main/php/xp/command/AbstractRunner.class.php +++ b/src/main/php/xp/command/AbstractRunner.class.php @@ -2,7 +2,7 @@ use io\streams\{ConsoleInputStream, ConsoleOutputStream, InputStream, OutputStream, StringReader, StringWriter}; use lang\reflection\{InvocationFailed, Type}; -use lang\{ClassLoader, ClassNotFoundException, System, Throwable, XPClass}; +use lang\{ClassLoader, ClassNotFoundException, System, Throwable, XPClass, Reflection}; use util\cmd\{Arg, Args, Commands, Config, Console, ParamString}; use xp\runtime\Help; @@ -100,7 +100,7 @@ public function setErr(OutputStream $err) { */ protected function runCommand($command, $params, $config) { try { - $type= Commands::named($command); + $type= Reflection::type(Commands::named($command)); } catch (Throwable $e) { self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage()); return 1; diff --git a/src/main/php/xp/command/CmdRunner.class.php b/src/main/php/xp/command/CmdRunner.class.php index 4fd2ae6..fddf924 100755 --- a/src/main/php/xp/command/CmdRunner.class.php +++ b/src/main/php/xp/command/CmdRunner.class.php @@ -58,7 +58,7 @@ protected function commandUsage(Type $type) { $markdown= '# '.ltrim($headline, ' #')."\n\n"; } - $markdown.= "- Usage\n ```sh\n$ xp cmd ".Commands::nameOf($type); + $markdown.= "- Usage\n ```sh\n$ xp cmd ".Commands::nameOf($type->class()); $extra= $details= $positional= []; foreach ($type->methods()->annotated(Arg::class) as $method) { diff --git a/src/main/php/xp/command/Runner.class.php b/src/main/php/xp/command/Runner.class.php index 30636ee..35f0257 100755 --- a/src/main/php/xp/command/Runner.class.php +++ b/src/main/php/xp/command/Runner.class.php @@ -96,7 +96,7 @@ protected function commandUsage(Type $type) { // Usage asort($positional); - self::$err->write('Usage: $ xpcli ', Commands::nameOf($type), ' '); + self::$err->write('Usage: $ xpcli ', Commands::nameOf($type->class()), ' '); foreach ($positional as $name) { self::$err->write('<', $name, '> '); } diff --git a/src/test/php/util/cmd/unittest/CommandsTest.class.php b/src/test/php/util/cmd/unittest/CommandsTest.class.php index 16630cf..d3ad539 100755 --- a/src/test/php/util/cmd/unittest/CommandsTest.class.php +++ b/src/test/php/util/cmd/unittest/CommandsTest.class.php @@ -1,7 +1,7 @@ class()); + Assert::equals(self::$class, Commands::named($name)); }); } #[Test] public function unqualified_name_in_global_namespace() { - Assert::equals(self::$global, Commands::named('BatchImport')->class()); + Assert::equals(self::$global, Commands::named('BatchImport')); } #[Test, Expect(ClassNotFoundException::class), Values(['class.does.not.Exist', 'DoesNotExist'])] @@ -74,13 +74,13 @@ public function file_not_runnable() { #[Test] public function nameOf_qualified() { - Assert::equals('util.cmd.unittest.BatchImport', Commands::nameOf(Reflection::type(self::$class))); + Assert::equals('util.cmd.unittest.BatchImport', Commands::nameOf(self::$class)); } #[Test] public function nameOf_shortened_when_package_is_registered() { self::withPackage('util.cmd.unittest', function() { - Assert::equals('BatchImport', Commands::nameOf(Reflection::type(self::$class))); + Assert::equals('BatchImport', Commands::nameOf(self::$class)); }); } } \ No newline at end of file