Skip to content

Commit

Permalink
Migrate to use Package class from reflection library
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 28, 2024
1 parent 54e4e5e commit 05d69c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down
32 changes: 16 additions & 16 deletions src/main/php/util/cmd/Commands.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace util\cmd;

use lang\reflect\Package;
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};
use lang\reflection\Package;
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException, Runnable};

/**
* Commands factory. Loads classes, files and named commands by using
Expand All @@ -12,9 +12,7 @@
final class Commands {
private static $packages= [];

/**
* Prevent instantiation
*/
/** Prevent instantiation */
private function __construct() { }

/**
Expand All @@ -24,7 +22,7 @@ private function __construct() { }
* @return void
*/
public static function registerPackage($package) {
self::$packages[$package]= Package::forName($package);
self::$packages[$package]= new Package($package);
}

/**
Expand All @@ -40,21 +38,23 @@ public static function removePackage($package) {
/**
* Gets all registered packages
*
* @return lang.reflect.Package[]
* @return [:lang.reflection.Package]
*/
public static function allPackages() {
return array_values(self::$packages);
return self::$packages;
}

/**
* Locates a named command
*
* @param lang.IClassLoader $cl
* @param string $name
* @return lang.reflect.Package or NULL if nothing is found
* @return ?string
*/
private static function locateNamed($name) {
private static function locateNamed($cl, $name) {
foreach (self::$packages as $package) {
if ($package->providesClass($name)) return $package;
$class= $package->name().'.'.$name;
if ($cl->providesClass($class)) return $class;
}
return null;
}
Expand All @@ -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;
Expand Down
17 changes: 8 additions & 9 deletions src/main/php/xp/command/CmdRunner.class.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php namespace xp\command;

use io\streams\{ConsoleInputStream, ConsoleOutputStream, InputStream, OutputStream, StringReader, StringWriter};
use lang\reflect\{Package, Modifiers};
use lang\reflection\{InvocationFailed, Type};
use lang\reflection\{InvocationFailed, Type, Package};
use lang\{ClassLoader, ClassNotFoundException, Throwable, Reflection};
use util\cmd\{Arg, Args, Commands, Config, Console, ParamString};
use util\cmd\{Arg, Args, Command, Commands, Config, Console, ParamString};
use util\{Properties, PropertyAccess, PropertyManager};
use xp\runtime\Help;

Expand Down Expand Up @@ -164,9 +163,9 @@ protected function selfUsage() {
protected function listCommands() {
$commandsIn= function($package) {
$markdown= '';
foreach ($package->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)*';
Expand All @@ -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, []);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/util/cmd/unittest/CommandsTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace util\cmd\unittest;

use lang\reflect\Package;
use lang\reflection\Package;
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};
use test\{Assert, Before, Expect, Test, Values};
use util\cmd\{Command, Commands};
Expand Down Expand Up @@ -41,7 +41,7 @@ public function packages_initially_empty() {
#[Test]
public function register_package() {
self::withPackage('util.cmd.unittest', function($package) {
Assert::equals([Package::forName($package)], Commands::allPackages());
Assert::equals([$package => new Package($package)], Commands::allPackages());
});
}

Expand Down

0 comments on commit 05d69c4

Please sign in to comment.