From 05d69c4fb5b8a79b9ad1fa123344bc5f5b1a6628 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 28 Mar 2024 19:57:46 +0100 Subject: [PATCH] Migrate to use Package class from reflection library --- composer.json | 2 +- src/main/php/util/cmd/Commands.class.php | 32 +++++++++---------- src/main/php/xp/command/CmdRunner.class.php | 17 +++++----- .../util/cmd/unittest/CommandsTest.class.php | 4 +-- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index e804299..4e1b4e2 100755 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "keywords": ["module", "xp"], "require" : { "xp-framework/core": "^12.0 | ^11.0 | ^10.15", - "xp-framework/reflection": "^3.0 | ^2.9", + "xp-framework/reflection": "dev-feature/global-package as 3.1.0", "php" : ">=7.4.0" }, "require-dev" : { diff --git a/src/main/php/util/cmd/Commands.class.php b/src/main/php/util/cmd/Commands.class.php index d3834f4..5a90bef 100755 --- a/src/main/php/util/cmd/Commands.class.php +++ b/src/main/php/util/cmd/Commands.class.php @@ -1,7 +1,7 @@ providesClass($name)) return $package; + $class= $package->name().'.'.$name; + if ($cl->providesClass($class)) return $class; } return null; } @@ -71,17 +71,17 @@ public static function named($name) { $cl= ClassLoader::getDefault(); if (is_file($name)) { $class= $cl->loadUri($name); - } else if (strstr($name, '.')) { + } else if (strpos($name, '.')) { $class= $cl->loadClass($name); - } else if ($package= self::locateNamed($name)) { - $class= $package->loadClass($name); + } else if ($named= self::locateNamed($cl, $name)) { + $class= $cl->loadClass($named); } else { $class= $cl->loadClass($name); } // Check whether class is runnable - if (!$class->isSubclassOf('lang.Runnable')) { - throw new IllegalArgumentException($class->getName().' is not runnable'); + if (!$class->isSubclassOf(Command::class)) { + throw new IllegalArgumentException($class->getName().' is not a command'); } return $class; diff --git a/src/main/php/xp/command/CmdRunner.class.php b/src/main/php/xp/command/CmdRunner.class.php index fa62281..3da97c6 100755 --- a/src/main/php/xp/command/CmdRunner.class.php +++ b/src/main/php/xp/command/CmdRunner.class.php @@ -1,10 +1,9 @@ getClasses() as $class) { - if ($class->isSubclassOf('util.cmd.Command') && !Modifiers::isAbstract($class->getModifiers())) { - $markdown.= ' $ xp cmd '.$class->getSimpleName()."\n"; + foreach ($package->types() as $type) { + if ($type->is(Command::class) && $type->instantiable()) { + $markdown.= ' $ xp cmd '.substr($type->name(), strlen($package->name()) + 1)."\n"; } } return $markdown ?: ' *(no commands)*'; @@ -175,13 +174,13 @@ protected function listCommands() { $markdown= "# Named commands\n\n"; if ($packages= Commands::allPackages()) { - foreach ($packages as $package) { - $markdown.= '* In package **'.$package->getName()."**\n\n".$commandsIn($package); + foreach ($packages as $name => $package) { + $markdown.= '* In package **'.$name."**\n\n".$commandsIn($package); } $markdown.= "\n"; } - $markdown.= "* In global package\n\n".$commandsIn(Package::forName(null)); + $markdown.= "* In global package\n\n".$commandsIn(new Package()); Help::render(self::$err, $markdown, []); } diff --git a/src/test/php/util/cmd/unittest/CommandsTest.class.php b/src/test/php/util/cmd/unittest/CommandsTest.class.php index d3ad539..39d72bb 100755 --- a/src/test/php/util/cmd/unittest/CommandsTest.class.php +++ b/src/test/php/util/cmd/unittest/CommandsTest.class.php @@ -1,6 +1,6 @@ new Package($package)], Commands::allPackages()); }); }