Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2017
1 parent 60bbdf0 commit c3bb66c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 18 deletions.
21 changes: 21 additions & 0 deletions examples/liteApp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env php
<?php

define('PROJECT_PATH', dirname(__DIR__));

require __DIR__ . '/s-autoload.php';

// create app instance
$app = new \inhere\console\LiteApp;

// register commands
$app->addCommand('test', function () {
echo "hello\n";
}, 'the description text for the command: test');

$app->addCommand('test1', function () {
echo "hello\n";
}, 'the description text for the command: test1');

// run
$app->run();
106 changes: 88 additions & 18 deletions src/LiteApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LiteApp
/**
* @param bool $exit
*/
public function dispatch($exit = true)
public function run($exit = true)
{
$this->parseCliArgv();

Expand All @@ -60,6 +60,14 @@ public function dispatch($exit = true)
unset($this->args[0]);
}

$this->dispatch($exit);
}

/**
* @param bool $exit
*/
public function dispatch($exit = true)
{
if (!$command = $this->command) {
$this->showCommands();
}
Expand All @@ -73,22 +81,22 @@ public function dispatch($exit = true)
$this->showCommands("The command {$command} not exists!");
}
} catch (\Throwable $e) {
$text = sprintf(
"Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n",
$e->getCode(),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
);
exit($text);
$status = $this->handleException($e);
}

if ($exit) {
exit((int)$status);
$this->stop($status);
}
}

/**
* @param int $code
*/
public function stop($code = 0)
{
exit((int)$code);
}

/**
* @param $command
* @param $handler
Expand Down Expand Up @@ -120,6 +128,26 @@ public function runHandler($command, $handler)
throw new \InvalidArgumentException("Invalid handler of the command: $command");
}

/**
* @param \Throwable $e
* @return int
*/
protected function handleException(\Throwable $e)
{
$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()
);

return $code;
}

/**
* parseCliArgv
*/
Expand Down Expand Up @@ -158,18 +186,40 @@ public function parseCliArgv()
/**
* @param string $command
* @param string|\Closure $handler
* @param string $desc
* @param string $description
*/
public function addCommand($command, $handler, $desc = '')
public function addCommand($command, $handler, $description = '')
{
if (!$command || !$handler) {
throw new \InvalidArgumentException('Invalid arguments');
}

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

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

if (is_array($handler)) {
$conf = array_values($handler);
$handler = $conf[0];
$des = $conf[1] ?? '';
}

$this->addCommand($command, $handler, $des);
}
}

///////////////////////////////////////////////////////////////////////////////////
/// helper methods
///////////////////////////////////////////////////////////////////////////////////

/**
* @param string $err
*/
Expand All @@ -179,10 +229,11 @@ public function showCommands($err = '')
echo "ERROR: $err\n\n";
}

$help = "Available Commands:\n";
$commandWidth = 12;
$help = "Welcome to the Lite Console Application.\n\nAvailable Commands:\n";

foreach ($this->messages as $command => $desc) {
$command = str_pad($command, 18, ' ');
$command = str_pad($command, $commandWidth, ' ');
$desc = $desc ?: 'No description for the command';
$help .= " $command $desc\n";
}
Expand All @@ -191,11 +242,30 @@ public function showCommands($err = '')
exit(0);
}

/**
* @param string $name
* @param mixed $default
* @return mixed|null
*/
public function getArg($name, $default = null)
{
return $this->args[$name] ?? $default;
}

/**
* @param string $name
* @param mixed $default
* @return mixed|null
*/
public function getOpt($name, $default = null)
{
return $this->opts[$name] ?? $default;
}

///////////////////////////////////////////////////////////////////////////////////
/// helper methods
/// getter/setter methods
///////////////////////////////////////////////////////////////////////////////////


/**
* @return array
*/
Expand Down

0 comments on commit c3bb66c

Please sign in to comment.