Skip to content

Commit

Permalink
inspact and reformat codes
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 24, 2019
1 parent d1ed40c commit 30eb0f1
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 202 deletions.
1 change: 0 additions & 1 deletion libs/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
];

spl_autoload_register(function ($class) use ($map) {
$file = null;
foreach ($map as $np => $dir) {
if (0 === strpos($class, $np)) {
$path = str_replace('\\', '/', substr($class, strlen($np)));
Expand Down
3 changes: 2 additions & 1 deletion libs/cli-utils/example/down.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* demo for cli download file.
*/

use Toolkit\Cli\App;
use Toolkit\Cli\Download;

require dirname(__DIR__) . '/test/boot.php';

$app = new \Toolkit\Cli\App();
$app = new App();
$url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2';
$down = Download::create($url);

Expand Down
3 changes: 2 additions & 1 deletion libs/cli-utils/example/high.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Time: 下午3:13
*/

use Toolkit\Cli\Cli;
use Toolkit\Cli\Highlighter;

require dirname(__DIR__) . '/test/boot.php';
Expand All @@ -15,4 +16,4 @@
// this is an comment
$rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));

\Toolkit\Cli\Cli::write($rendered);
Cli::write($rendered);
88 changes: 55 additions & 33 deletions libs/cli-utils/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@

namespace Toolkit\Cli;

use InvalidArgumentException;
use Throwable;
use function array_shift;
use function array_values;
use function class_exists;
use function function_exists;
use function getcwd;
use function is_array;
use function is_string;
use function method_exists;
use function str_pad;
use function strlen;
use function trim;

/**
* Class App - A lite CLI Application
*
* @package Inhere\Console
*/
class App
Expand All @@ -27,10 +42,14 @@ class App
*/
private $opts = [];

/** @var string */
/**
* @var string
*/
private $script;

/** @var string */
/**
* @var string
*/
private $command = '';

/**
Expand All @@ -50,24 +69,26 @@ class App

/**
* Class constructor.
*
* @param array|null $argv
*/
public function __construct(array $argv = null)
{
// get current dir
$this->pwd = \getcwd();
$this->pwd = getcwd();

// parse cli argv
$argv = $argv ?? (array)$_SERVER['argv'];
// get script file
$this->script = \array_shift($argv);
$this->script = array_shift($argv);
// parse flags
[$this->args, $this->opts] = Flags::simpleParseArgv($argv);
[$this->args, $this->opts] = Flags::parseArgv($argv);
}

/**
* @param bool $exit
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*/
public function run(bool $exit = true): void
{
Expand All @@ -81,7 +102,8 @@ public function run(bool $exit = true): void

/**
* @param bool $exit
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*/
public function dispatch(bool $exit = true): void
{
Expand All @@ -98,7 +120,7 @@ public function dispatch(bool $exit = true): void
} else {
$this->displayHelp("The command {$command} not exists!");
}
} catch (\Throwable $e) {
} catch (Throwable $e) {
$status = $this->handleException($e);
}

Expand All @@ -118,51 +140,47 @@ public function stop($code = 0): void
/**
* @param string $command
* @param $handler
*
* @return mixed
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function runHandler(string $command, $handler)
{
if (\is_string($handler)) {
if (is_string($handler)) {
// function name
if (\function_exists($handler)) {
if (function_exists($handler)) {
return $handler($this);
}

if (\class_exists($handler)) {
if (class_exists($handler)) {
$handler = new $handler;

// $handler->execute()
if (\method_exists($handler, 'execute')) {
if (method_exists($handler, 'execute')) {
return $handler->execute($this);
}
}
}

// a \Closure OR $handler->__invoke()
if (\method_exists($handler, '__invoke')) {
if (method_exists($handler, '__invoke')) {
return $handler($this);
}

throw new \InvalidArgumentException("Invalid handler of the command: $command");
throw new InvalidArgumentException("Invalid handler of the command: $command");
}

/**
* @param \Throwable $e
* @param Throwable $e
*
* @return int
*/
protected function handleException(\Throwable $e): int
protected function handleException(Throwable $e): int
{
$code = $e->getCode() !== 0 ? $e->getCode() : 133;

printf(
"Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n",
$code,
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
);
printf("Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", $code, $e->getMessage(), $e->getFile(),
$e->getLine(), $e->getTraceAsString());

return $code;
}
Expand All @@ -171,33 +189,35 @@ protected function handleException(\Throwable $e): int
* @param string $command
* @param callable $handler
* @param string $description
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*/
public function addCommand(string $command, callable $handler, string $description = ''): void
{
if (!$command || !$handler) {
throw new \InvalidArgumentException('Invalid arguments');
throw new InvalidArgumentException('Invalid arguments');
}

if (($len = \strlen($command)) > $this->keyWidth) {
if (($len = strlen($command)) > $this->keyWidth) {
$this->keyWidth = $len;
}

$this->commands[$command] = $handler;
$this->messages[$command] = \trim($description);
$this->messages[$command] = trim($description);
}

/**
* @param array $commands
* @throws \InvalidArgumentException
*
* @throws InvalidArgumentException
*/
public function commands(array $commands): void
{
foreach ($commands as $command => $handler) {
$des = '';

if (\is_array($handler)) {
$conf = \array_values($handler);
if (is_array($handler)) {
$conf = array_values($handler);
$handler = $conf[0];
$des = $conf[1] ?? '';
}
Expand All @@ -224,7 +244,7 @@ public function displayHelp(string $err = ''): void
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";

foreach ($this->messages as $command => $desc) {
$command = \str_pad($command, $commandWidth, ' ');
$command = str_pad($command, $commandWidth, ' ');
$desc = $desc ?: 'No description for the command';
$help .= " $command $desc\n";
}
Expand All @@ -236,6 +256,7 @@ public function displayHelp(string $err = ''): void
/**
* @param string|int $name
* @param mixed $default
*
* @return mixed|null
*/
public function getArg($name, $default = null)
Expand All @@ -246,6 +267,7 @@ public function getArg($name, $default = null)
/**
* @param string $name
* @param mixed $default
*
* @return mixed|null
*/
public function getOpt(string $name, $default = null)
Expand Down
Loading

0 comments on commit 30eb0f1

Please sign in to comment.