Skip to content

Commit

Permalink
breaking: remove more not used methods and class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 26, 2021
1 parent 3e06b07 commit 71d41b1
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 345 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function commands(): array
* --long,-s option description 1
* --opt option description 2
*
* @param $input
* @param Input $input
* @param Output $output
*/
public function execute(Input $input, Output $output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 下午8:14
*/

namespace Inhere\Console\IO;
namespace Deprecated;

use Inhere\Console\Util\Helper;
use InvalidArgumentException;
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ abstract class AbstractApplication implements ApplicationInterface
/** @var array Application config data */
protected $config = [
'name' => 'My Console Application',
'description' => 'This is my console application',
'desc' => 'This is my console application',
'version' => '0.5.1',
'homepage' => '', // can provide you app homepage url
'publishAt' => '2017.03.24',
Expand Down
80 changes: 39 additions & 41 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Inhere\Console;

use Closure;
use Inhere\Console\Contract\ApplicationInterface;
use Inhere\Console\Contract\ControllerInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
Expand All @@ -17,6 +18,7 @@
use RuntimeException;
use SplFileInfo;
use Throwable;
use Toolkit\PFlag\SFlags;
use Toolkit\Stdlib\Helper\DataHelper;
use function array_unshift;
use function class_exists;
Expand All @@ -40,8 +42,8 @@ class Application extends AbstractApplication
/**
* Class constructor.
*
* @param array $config
* @param Input|null $input
* @param array $config
* @param Input|null $input
* @param Output|null $output
*/
public function __construct(array $config = [], Input $input = null, Output $output = null)
Expand All @@ -58,46 +60,40 @@ public function __construct(array $config = [], Input $input = null, Output $out
/**
* {@inheritdoc}
*/
public function controller(string $name, $class = null, $option = null)
public function controller(string $name, $class = null, array $config = []): ApplicationInterface
{
if (is_string($option)) {
$option = [
'description' => $option,
];
}

$this->logf(Console::VERB_CRAZY, 'load group controller: %s', $name);
$this->router->addGroup($name, $class, (array)$option);
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
$this->router->addGroup($name, $class, $config);

return $this;
}

/**
* Add group/controller
*
* @param string $name
* @param string $name
* @param string|ControllerInterface|null $class The controller class
* @param null|array|string $option
* @param array $config
*
* @return Application|Contract\ApplicationInterface
* @see controller()
*/
public function addGroup(string $name, $class = null, $option = null)
public function addGroup(string $name, $class = null, array $config = []): ApplicationInterface
{
return $this->controller($name, $class, $option);
return $this->controller($name, $class, $config);
}

/**
* @param string $name
* @param string $name
* @param string|ControllerInterface|null $class The controller class
* @param null|array|string $option
* @param array $config
*
* @return Application|Contract\ApplicationInterface
* @see controller()
*/
public function addController(string $name, $class = null, $option = null)
public function addController(string $name, $class = null, array $config = []): ApplicationInterface
{
return $this->controller($name, $class, $option);
return $this->controller($name, $class, $config);
}

/**
Expand Down Expand Up @@ -134,16 +130,10 @@ public function addControllers(array $controllers): void
/**
* {@inheritdoc}
*/
public function command(string $name, $handler = null, $option = null)
public function command(string $name, $handler = null, array $config = [])
{
if (is_string($option)) {
$option = [
'description' => $option,
];
}

$this->logf(Console::VERB_CRAZY, 'load application command: %s', $name);
$this->router->addCommand($name, $handler, (array)$option);
$this->logf(Console::VERB_CRAZY, 'register alone command: %s', $name);
$this->router->addCommand($name, $handler, $config);

return $this;
}
Expand All @@ -152,15 +142,15 @@ public function command(string $name, $handler = null, $option = null)
* add command
*
* @param string $name
* @param null $handler
* @param null $option
* @param null|mixed $handler
* @param array $config
*
* @return Application
* @see command()
*/
public function addCommand(string $name, $handler = null, $option = null): self
public function addCommand(string $name, $handler = null, array $config = []): self
{
return $this->command($name, $handler, $option);
return $this->command($name, $handler, $config);
}

/**
Expand Down Expand Up @@ -260,7 +250,7 @@ protected function getFileFilter(): callable

/**
* @param string $name command name or command ID or command path.
* @param array $args
* @param array $args
*
* @return int|mixed
* @throws Throwable
Expand Down Expand Up @@ -322,21 +312,27 @@ public function dispatch(string $name, array $args = [])
/**
* run a independent command
*
* @param string $name Command name
* @param string $name Command name
* @param Closure|string $handler Command class or handler func
* @param array $options
* @param array $args
* @param array $options
* @param array $args
*
* @return mixed
* @throws Throwable
*/
protected function runCommand(string $name, $handler, array $options, array $args)
{
if (is_object($handler) && method_exists($handler, '__invoke')) {
if ($this->input->getSameOpt(['h', 'help'])) {
$desc = $options['description'] ?? 'No command description message';
$fs = SFlags::new();
$fs->addOptsByRules(GlobalOption::getAloneOptions());
$desc = $options['desc'] ?? 'No command description message';
$fs->setDesc($desc);

return $this->output->write($desc);
// save to input object
$this->input->setFs($fs);

if (!$fs->parse($args)) {
return 0; // render help
}

$result = $handler($this->input, $this->output);
Expand Down Expand Up @@ -364,18 +360,20 @@ protected function runCommand(string $name, $handler, array $options, array $arg
* Execute an action in a group command(controller)
*
* @param array $info Matched route info
*
* @psalm-param array{action: string} $info Matched route info
*
* @param array $options
* @param array $args
* @param bool $detachedRun
* @param bool $detachedRun
*
* @return mixed
* @throws Throwable
*/
protected function runAction(array $info, array $options, array $args, bool $detachedRun = false)
{
$controller = $this->createController($info);
$controller::setDesc($options['description'] ?? '');
$controller::setDesc($options['desc'] ?? '');

if ($detachedRun) {
$controller->setDetached();
Expand Down
12 changes: 6 additions & 6 deletions src/Component/Formatter/HelpPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HelpPanel extends MessageFormatter
/**
* help panel keys
*/
public const DESC = 'description';
public const DESC = 'desc';

public const USAGE = 'usage';

Expand Down Expand Up @@ -86,8 +86,8 @@ public static function show(array $config): void
'indentDes' => ' ',
];
$config = array_merge([
'description' => '',
'usage' => '',
'desc' => '',
'usage' => '',

'commands' => [],
'arguments' => [],
Expand All @@ -108,9 +108,9 @@ public static function show(array $config): void
}

// description
if ($config['description']) {
$parts[] = "{$option['indentDes']}{$config['description']}\n";
unset($config['description']);
if ($config['desc']) {
$parts[] = "{$option['indentDes']}{$config['desc']}\n";
unset($config['desc']);
}

// now, render usage,commands,arguments,options,examples ...
Expand Down
10 changes: 3 additions & 7 deletions src/Concern/ApplicationHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use function str_replace;
use function strpos;
use function strtr;
use function vdump;
use const PHP_EOL;
use const PHP_OS;
use const PHP_VERSION;
Expand Down Expand Up @@ -123,8 +122,6 @@ public function showHelpInfo(string $command = ''): void

$this->debugf('display application help by input -h, --help');
$this->fire(ConsoleEvent::BEFORE_RENDER_APP_HELP, $this);
$delimiter = $this->delimiter;
$binName = $in->getScriptName();

// built in options
// $globalOptions = self::$globalOptions;
Expand All @@ -138,19 +135,18 @@ public function showHelpInfo(string $command = ''): void
$globalOptions['--gen-file'] = 'The output file for generate auto completion script';
}

$binName = $in->getScriptName();
$helpInfo = [
'Usage' => "$binName <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
'Options' => FormatUtil::alignOptions($globalOptions),
'Example' => [
'- run a command/subcommand:',
"$binName test run a independent command",
"$binName home index run a subcommand of the group",
sprintf("$binName home%sindex run a subcommand of the group", $delimiter),
'',
'- display help for command:',
"$binName help COMMAND see a command help information",
"$binName home index -h see a subcommand help of the group",
sprintf("$binName home%sindex -h see a subcommand help of the group", $delimiter),
],
'Help' => [
'Generate shell auto completion scripts:',
Expand Down Expand Up @@ -244,7 +240,7 @@ public function showCommandList(): void
/** @var AbstractHandler $command */
if (is_subclass_of($command, CommandInterface::class)) {
$desc = $command::getDescription() ?: $placeholder;
} elseif ($msg = $options['description'] ?? '') {
} elseif ($msg = $options['desc'] ?? '') {
$desc = $msg;
} elseif (is_string($command)) {
$desc = 'A handler : ' . $command;
Expand Down Expand Up @@ -274,7 +270,7 @@ public function showCommandList(): void
ksort($internalCommands);
Console::startBuffer();

if ($appDesc = $this->getParam('description', '')) {
if ($appDesc = $this->getParam('desc', '')) {
$appVer = $this->getParam('version', '');
Console::writeln(sprintf('%s%s' . PHP_EOL, $appDesc, $appVer ? " (Version: <info>$appVer</info>)" : ''));
}
Expand Down
Loading

0 comments on commit 71d41b1

Please sign in to comment.