Skip to content

Commit

Permalink
Merge pull request #4 from inhere/master
Browse files Browse the repository at this point in the history
Some small adjustments
  • Loading branch information
inhere authored Mar 19, 2018
2 parents 6348c3b + 5dfec9b commit 8fa3450
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 76 deletions.
22 changes: 21 additions & 1 deletion src/Bean/Annotation/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class Command
*/
private $name = '';

/**
* @var bool
*/
private $enabled = true;

/**
* @var bool
*/
Expand All @@ -35,12 +40,19 @@ public function __construct(array $values)
if (isset($values['value'])) {
$this->name = $values['value'];
}

if (isset($values['name'])) {
$this->name = $values['name'];
}

if (isset($values['coroutine'])) {
$this->coroutine = $values['coroutine'];
}

if (isset($values['enabled'])) {
$this->enabled = (bool)$values['enabled'];
}

if (isset($values['server'])) {
$this->server = $values['server'];
}
Expand Down Expand Up @@ -69,4 +81,12 @@ public function isServer(): bool
{
return $this->server;
}
}

/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
}
5 changes: 3 additions & 2 deletions src/Bean/Collector/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommandCollector implements CollectorInterface
* collect
*
* @param string $className
* @param object $objectAnnotation
* @param mixed $objectAnnotation
* @param string $propertyName
* @param string $methodName
* @param null $propertyValue
Expand Down Expand Up @@ -54,6 +54,7 @@ private static function collectCommand(string $className, Command $objectAnnotat
$server = $objectAnnotation->isServer();

self::$commandMapping[$className]['name'] = $commandName;
self::$commandMapping[$className]['enabled'] = $objectAnnotation->isEnabled();
self::$commandMapping[$className]['coroutine'] = $coroutine;
self::$commandMapping[$className]['server'] = $server;
}
Expand Down Expand Up @@ -94,4 +95,4 @@ public static function getCollector(): array
{
return self::$commandMapping;
}
}
}
13 changes: 7 additions & 6 deletions src/Bean/Parser/CommandParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
class CommandParser extends AbstractParser
{
/**
* @param string $className
* @param string $className
* @param Command $objectAnnotation
* @param string $propertyName
* @param string $methodName
* @param string $propertyName
* @param string $methodName
*
* @param null $propertyValue
* @return mixed
*/
public function parser(string $className, $objectAnnotation = null, string $propertyName = "", string $methodName = "", $propertyValue = null)
public function parser(string $className, $objectAnnotation = null, string $propertyName = '', string $methodName = '', $propertyValue = null)
{
$beanName = $className;
$scope = Scope::SINGLETON;

CommandCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);

return [$beanName, $scope, ""];
return [$beanName, $scope, ''];
}
}
}
7 changes: 4 additions & 3 deletions src/Bean/Parser/MappingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ class MappingParser extends AbstractParser
* @param Mapping $objectAnnotation
* @param string $propertyName
* @param string $methodName
* @param null $propertyValue
* @return void
*/
public function parser(
string $className,
$objectAnnotation = null,
string $propertyName = "",
string $methodName = "",
string $propertyName = '',
string $methodName = '',
$propertyValue = null
) {
CommandCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);
}
}
}
69 changes: 28 additions & 41 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Swoft\Bean\Annotation\Bean;
use Swoft\Console\Bean\Collector\CommandCollector;
use Swoft\Console\Helper\DocBlockHelper;
use Swoft\Helper\DocumentHelper;
use Swoft\Console\Router\HandlerAdapter;
use Swoft\Console\Router\HandlerMapping;

Expand Down Expand Up @@ -37,6 +36,7 @@ public function annotationVars(): array

/**
* @return void
* @throws \InvalidArgumentException
* @throws \ReflectionException
*/
public function run()
Expand All @@ -55,7 +55,7 @@ public function run()
list($className, $method) = $handler;

if ($router->isDefaultCommand($method)) {
$this->indexComamnd($className);
$this->indexCommand($className);

return;
}
Expand All @@ -77,7 +77,7 @@ public function run()
* @throws \ReflectionException
* @return void
*/
private function indexComamnd(string $className)
private function indexCommand(string $className)
{
/* @var HandlerMapping $router */
$router = App::getBean('commandRoute');
Expand All @@ -87,7 +87,7 @@ private function indexComamnd(string $className)

$reflectionClass = new \ReflectionClass($className);
$classDocument = $reflectionClass->getDocComment();
$classDocAry = DocumentHelper::tagList($classDocument);
$classDocAry = DocBlockHelper::getTags($classDocument);
$classDesc = $classDocAry['Description'];

$methodCommands = [];
Expand All @@ -106,7 +106,7 @@ private function indexComamnd(string $className)
}
$reflectionMethod = $reflectionClass->getMethod($methodName);
$methodDocument = $reflectionMethod->getDocComment();
$methodDocAry = DocumentHelper::tagList($methodDocument);
$methodDocAry = DocBlockHelper::getTags($methodDocument);
$methodCommands[$mappedName] = $methodDocAry['Description'];
}

Expand Down Expand Up @@ -137,7 +137,6 @@ private function showCommandHelp(string $controllerClass, string $commandMethod)
$reflectionMethod = $reflectionClass->getMethod($commandMethod);
$document = $reflectionMethod->getDocComment();
$document = $this->parseAnnotationVars($document, $this->annotationVars());
// $docs = DocumentHelper::tagList($document);
$docs = DocBlockHelper::getTags($document);

$commands = [];
Expand Down Expand Up @@ -173,7 +172,7 @@ private function showCommandHelp(string $controllerClass, string $commandMethod)
}

/**
* help list
* show all commands for the console app
*
* @throws \ReflectionException
*/
Expand All @@ -182,19 +181,19 @@ private function showCommandList()
$commands = $this->parserCmdAndDesc();

$commandList = [];
$script = input()->getFullScript();
$commandList['Usage:'] = ["php $script"];
$script = \input()->getFullScript();
$commandList['Usage:'] = ["php $script {command} [arguments] [options]"];
$commandList['Commands:'] = $commands;
$commandList['Options:'] = [
'-h, --help' => 'show help information',
'-v, --version' => 'show version',
'-h, --help' => 'Display help information',
'-v, --version' => 'Display version information',
];

// show logo
output()->writeLogo();
\output()->writeLogo();

// output list
output()->writeList($commandList, 'comment', 'info');
\output()->writeList($commandList, 'comment', 'info');
}

/**
Expand All @@ -208,9 +207,11 @@ private function showVersion()
$swooleVersion = SWOOLE_VERSION;

// 显示面板
output()->writeLogo();
output()->writeln("swoft: <info>$swoftVersion</info>, php: <info>$phpVersion</info>, swoole: <info>$swooleVersion</info>", true);
output()->writeln('');
\output()->writeLogo();
\output()->writeln(
"swoft: <info>$swoftVersion</info>, php: <info>$phpVersion</info>, swoole: <info>$swooleVersion</info>\n",
true
);
}

/**
Expand All @@ -227,17 +228,23 @@ private function parserCmdAndDesc(): array
/* @var \Swoft\Console\Router\HandlerMapping $route */
$route = App::getBean('commandRoute');

foreach ($collector as $className => $comamnd) {
foreach ($collector as $className => $command) {
if (!$command['enabled']) {
continue;
}

$rc = new \ReflectionClass($className);
$docComment = $rc->getDocComment();
$docAry = DocumentHelper::tagList($docComment);
$desc = $docAry['Description'];
$docAry = DocBlockHelper::getTags($docComment);

$prefix = $comamnd['name'];
$prefix = $command['name'];
$prefix = $route->getPrefix($prefix, $className);
$commands[$prefix] = $desc;
$commands[$prefix] = \ucfirst($docAry['Description']);
}

// sort commands
ksort($commands);

return $commands;
}

Expand All @@ -258,26 +265,6 @@ private function baseCommand()
$this->showCommandList();
}

/**
* 解析命令key和描述
*
* @param string $document 注解文档
* @return array
*/
private function parserKeyAndDesc(string $document): array
{
$keyAndDesc = [];
$items = explode("\n", $document);
foreach ($items as $item) {
$pos = strpos($item, ' ');
$key = substr($item, 0, $pos);
$desc = substr($item, $pos + 1);
$keyAndDesc[$key] = $desc;
}

return $keyAndDesc;
}

/**
* 替换注解中的变量为对应的值
* @param string $str
Expand Down
4 changes: 2 additions & 2 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function run()
$command = App::getBean('command');
$command->run();
} catch (\Throwable $e) {
output()->writeln(sprintf('<error>%s</error>', $e->getMessage()), true, false);
output()->writeln(sprintf('<error>%s</error>', $e->getTraceAsString()), true, true);
\output()->writeln(sprintf('<error>%s</error>', $e->getMessage()), true, false);
\output()->writeln(sprintf("Trace:\n%s", $e->getTraceAsString()), true, true);
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/Helper/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,49 @@ public static function nextIsValue($val): bool
// it isn't option or named argument
return $val{0} !== '-' && false === strpos($val, '=');
}


/**
* Returns true if STDOUT supports colorization.
* This code has been copied and adapted from
* \Symfony\Component\Console\Output\OutputStream.
* @return boolean
*/
public static function supportColor(): bool
{
if (DIRECTORY_SEPARATOR === '\\') {
return
'10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD ||
// 0 == strpos(PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . PHP_WINDOWS_VERSION_BUILD, '10.') ||
false !== getenv('ANSICON') ||
'ON' === getenv('ConEmuANSI') ||
'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
;
}

if (!\defined('STDOUT')) {
return false;
}

return self::isInteractive(STDOUT);
}

/**
* @return bool
*/
public static function isSupport256Color(): bool
{
return DIRECTORY_SEPARATOR === '/' && strpos(getenv('TERM'), '256color') !== false;
}

/**
* Returns if the file descriptor is an interactive terminal or not.
* @param int|resource $fileDescriptor
* @return boolean
*/
public static function isInteractive($fileDescriptor): bool
{
return \function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
}

}
Loading

0 comments on commit 8fa3450

Please sign in to comment.