Skip to content

Commit

Permalink
Remove BC break in util.cmd.Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 28, 2024
1 parent d0ce2db commit cd8964f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
17 changes: 8 additions & 9 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, Reflection};
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};

/**
* Commands factory. Loads classes, files and named commands by using
Expand Down Expand Up @@ -63,7 +63,7 @@ private static function locateNamed($name) {
* Find a command by a given name
*
* @param string $name
* @return lang.reflection.Type
* @return lang.XPClass
* @throws lang.ClassNotFoundException
* @throws lang.IllegalArgumentException if class is not runnable
*/
Expand All @@ -84,21 +84,20 @@ public static function named($name) {
throw new IllegalArgumentException($class->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();
}
}
}
4 changes: 2 additions & 2 deletions src/main/php/xp/command/AbstractRunner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/xp/command/CmdRunner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/xp/command/Runner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '> ');
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/php/util/cmd/unittest/CommandsTest.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace util\cmd\unittest;

use lang\reflect\Package;
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException, Reflection};
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};
use test\{Assert, Before, Expect, Test, Values};
use util\cmd\{Command, Commands};

Expand Down Expand Up @@ -48,13 +48,13 @@ public function register_package() {
#[Test, Values(['BatchImport', 'util.cmd.unittest.BatchImport'])]
public function named($name) {
self::withPackage('util.cmd.unittest', function() use($name) {
Assert::equals(self::$class, Commands::named($name)->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'])]
Expand All @@ -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));
});
}
}

0 comments on commit cd8964f

Please sign in to comment.