Skip to content

Commit

Permalink
Merge branch 'master' of github.com:swoft-cloud/swoft-component
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Oct 16, 2019
2 parents 8bac72d + d408ab6 commit 2f30071
Show file tree
Hide file tree
Showing 39 changed files with 464 additions and 443 deletions.
4 changes: 1 addition & 3 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ return PhpCsFixer\Config::create()
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('public')
->exclude('resource')
->exclude('config')
->exclude('test')
->exclude('runtime')
->exclude('vendor')
->in(__DIR__)
Expand Down
2 changes: 1 addition & 1 deletion script/Command/GenVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __invoke(App $app): void
}

if ($this->updated > 0 && $app->getBoolOpt('c')) {
self::gitCommit('update: update the version to composer.json');
self::gitCommit("update: add {$this->version} for all component composer.json");
}

echo Color::render("Complete\n", 'cyan');
Expand Down
9 changes: 5 additions & 4 deletions src/bean/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,13 +1048,14 @@ private function newProperty(
$propertyValue = $this->newPropertyArray($propertyValue, $id);
}

// Refer config or bean
if ($propertyInject->isRef()) {
$propertyValue = $this->getRefValue($propertyValue, $id);
}

// Optimize: Value not exists, skip call setter
if ($propertyValue === null) {
continue;
// Optimize: Value not exists, skip call setter
if ($propertyValue === null) {
continue;
}
}

// Parser property type
Expand Down
2 changes: 1 addition & 1 deletion src/console/src/Advanced/Formatter/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function show($data, string $title = 'Information Panel', array $o

// output panel top border
if ($borderChar) {
$border = str_pad($borderChar, $panelWidth + (3 * 3), $borderChar);
$border = str_pad($borderChar, $panelWidth + (3 * 4), $borderChar);
Console::write(' ' . $border);
}

Expand Down
54 changes: 39 additions & 15 deletions src/console/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Swoft\Stdlib\Helper\ObjectHelper;
use Throwable;
use function array_merge;
use function input;
use function implode;
use function output;
use function strpos;
use function strtr;
use function trim;
Expand Down Expand Up @@ -59,7 +61,7 @@ class Application implements ConsoleInterface
/**
* @var string
*/
private $name = 'My Application';
private $name = 'Swoft Application';

/**
* @var string
Expand All @@ -69,7 +71,14 @@ class Application implements ConsoleInterface
/**
* @var string
*/
private $description = 'Console application description';
private $description = 'Swoft 2.0 console application';

/**
* Console font logo text
*
* @var string
*/
private $logoText = '';

/**
* @var array
Expand All @@ -94,25 +103,26 @@ public function __construct(array $options = [])
*/
public function commentsVars(): array
{
$script = input()->getScript();
$script = $this->input->getScriptFile();
$fullCmd = $this->input->getFullCommand();

return [
'name' => $this->getName(),
'description' => $this->getDescription(),
// 'group' => self::getName(),
'workDir' => \input()->getPwd(),
'workDir' => $this->input->getPwd(),
'script' => $script, // bin/app
'binFile' => $script,
'command' => \input()->getCommand(), // demo OR home:test
'fullCmd' => \input()->getFullCommand(),
'fullCommand' => \input()->getFullCommand(),
'command' => $this->input->getCommand(), // demo OR home:test
'fullCmd' => $fullCmd,
'fullCommand' => $fullCmd,
];
}

protected function prepare(): void
{
$this->input = \input();
$this->output = \output();
$this->input = input();
$this->output = output();

// load builtin comments vars
$this->setCommentsVars($this->commentsVars());
Expand All @@ -124,15 +134,13 @@ protected function prepare(): void
public function run(): void
{
try {
Swoft::trigger(ConsoleEvent::RUN_BEFORE, $this);

// Prepare
// Prepare for run
$this->prepare();

// Get input command
$inputCommand = $this->input->getCommand();
Swoft::trigger(ConsoleEvent::RUN_BEFORE, $this);

if (!$inputCommand) {
// Get input command
if (!$inputCommand = $this->input->getCommand()) {
$this->filterSpecialOption();
} else {
$this->doRun($inputCommand);
Expand Down Expand Up @@ -383,4 +391,20 @@ public function setDescription(string $description): void
{
$this->description = trim($description);
}

/**
* @return string
*/
public function getLogoText(): string
{
return $this->logoText;
}

/**
* @param string $logoText
*/
public function setLogoText(string $logoText): void
{
$this->logoText = $logoText;
}
}
14 changes: 6 additions & 8 deletions src/console/src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Swoft\Console;

use Swoft;
use Swoft\Console\Router\Router;
use Swoft\Helper\ComposerJSON;
use Swoft\SwoftComponent;
Expand All @@ -13,13 +14,10 @@
*/
final class AutoLoader extends SwoftComponent
{
/**
* @return bool
*/
public function enable(): bool
{
return true;
}
// public function enable(): bool
// {
// return true;
// }

/**
* Get namespace and dirs
Expand Down Expand Up @@ -53,7 +51,7 @@ public function beans(): array
return [
'cliApp' => [
'class' => Application::class,
'version' => '2.0.0'
'version' => Swoft::VERSION,
],
'cliRouter' => [
'class' => Router::class,
Expand Down
32 changes: 22 additions & 10 deletions src/console/src/Concern/RenderHelpInfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Swoft;
use Swoft\Console\Console;
use Swoft\Console\ConsoleEvent;
use Swoft\Console\Helper\FormatUtil;
use Swoft\Console\Helper\Show;
use Swoft\Console\Output\Output;
Expand All @@ -13,7 +14,6 @@
use function array_shift;
use function explode;
use function implode;
use function input;
use function is_array;
use function is_bool;
use function is_scalar;
Expand Down Expand Up @@ -53,7 +53,8 @@ protected function showVersionInfo(): void
$swooleVersion = SWOOLE_VERSION;

// Display logo
$output->colored(' ' . ltrim(Swoft::FONT_LOGO));
$logoText = $this->logoText ?: Swoft::FONT_LOGO;
$output->colored(ltrim($logoText, "\n"));

// Display some information
$output->writef(
Expand All @@ -69,12 +70,17 @@ protected function showVersionInfo(): void
*/
protected function showApplicationHelp(bool $showLogo = true): void
{
Swoft::trigger(ConsoleEvent::SHOW_HELP_BEFORE, 'app');

// show logo
if ($showLogo) {
Console::colored(Swoft::FONT_LOGO, 'cyan');
$logoText = $this->logoText ?: Swoft::FONT_LOGO;
Console::colored(ltrim($logoText, "\n"), 'cyan');
}

$script = input()->getScriptName();
/** @var Swoft\Console\Input\Input $input */
$input = $this->input;
$script = $input->getScriptFile();
// Global options
$globalOptions = self::$globalOptions;
// Append expand option
Expand All @@ -95,7 +101,7 @@ protected function showApplicationHelp(bool $showLogo = true): void

/* @var Router $router */
$router = Swoft::getBean('cliRouter');
$expand = input()->getBoolOpt('expand');
$expand = $input->getBoolOpt('expand');
$keyWidth = $router->getKeyWidth($expand ? 2 : -4);

Console::writeln('<comment>Available Commands:</comment>');
Expand Down Expand Up @@ -134,18 +140,20 @@ protected function showGroupHelp(string $group, array $info = []): void
{
/* @var Router $router */
$router = Swoft::getBean('cliRouter');
$script = input()->getScriptName();
$info = $info ?: $router->getGroupInfo($group);

if (!$info) {
$info = $router->getGroupInfo($group);
}
Swoft::trigger(ConsoleEvent::SHOW_HELP_BEFORE, 'group', $info);

// $class = $groupInfo['class'];
$names = $info['names'];
sort($names);
$keyWidth = $router->getKeyWidth(-4);
$groupName = sprintf('%s%s', $group, $info['alias'] ? " (alias: <cyan>{$info['alias']}</cyan>)" : '');

/** @var Swoft\Console\Input\Input $input */
$input = $this->input;
$script = $input->getScriptFile();

Console::startBuffer();
Console::writeln($info['desc'] . PHP_EOL);
Console::writeln("<comment>Group:</comment> $groupName");
Expand Down Expand Up @@ -186,7 +194,11 @@ protected function showGroupHelp(string $group, array $info = []): void
*/
protected function showCommandHelp(array $info): void
{
$script = input()->getScriptName();
Swoft::trigger(ConsoleEvent::SHOW_HELP_BEFORE, 'command', $info);

/** @var Swoft\Console\Input\Input $input */
$input = $this->input;
$script = $input->getScriptFile();
$usage = sprintf('%s %s [arg ...] [--opt ...]', $script, $info['cmdId']);

// If has been custom usage.
Expand Down
19 changes: 8 additions & 11 deletions src/console/src/ConsoleDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,39 @@

namespace Swoft\Console;

use function defined;
use ReflectionException;
use ReflectionType;
use function srun;
use Swoft;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Console\Input\Input;
use Swoft\Console\Output\Output;
use Swoft\Context\Context;
use Swoft\Contract\DispatcherInterface;
use Swoft\Stdlib\Helper\PhpHelper;
use Swoft\SwoftEvent;
use Swoole\Runtime;
use Throwable;
use function defined;
use function get_class;
use function get_parent_class;
use function srun;

/**
* Class ConsoleDispatcher
*
* @since 2.0
* @Bean("cliDispatcher")
*/
class ConsoleDispatcher implements DispatcherInterface
class ConsoleDispatcher // implements DispatcherInterface
{
/**
* @param array $params
* @param array $route
*
* @return void
* @throws ReflectionException
* @throws Throwable
*/
public function dispatch(...$params): void
public function dispatch(array $route): void
{
$route = $params[0];
// Handler info
[$className, $method] = $route['handler'];

Expand Down Expand Up @@ -87,7 +84,7 @@ public function executeByCo($beanObject, string $method, array $bindParams): voi
$this->after($method);
} catch (Throwable $e) {
/** @var ConsoleErrorDispatcher $errDispatcher */
$errDispatcher = BeanFactory::getSingleton(ConsoleErrorDispatcher::class);
$errDispatcher = Swoft::getSingleton(ConsoleErrorDispatcher::class);

// Handle request error
$errDispatcher->run($e);
Expand Down Expand Up @@ -130,9 +127,9 @@ private function getBindParams(string $class, string $method): array
$type = $paramType->getName();

if ($type === Output::class) {
$bindParams[] = \output();
$bindParams[] = Swoft::getBean('input');
} elseif ($type === Input::class) {
$bindParams[] = \input();
$bindParams[] = Swoft::getBean('output');
} else {
$bindParams[] = null;
}
Expand Down
Loading

0 comments on commit 2f30071

Please sign in to comment.