Skip to content

Commit

Permalink
move some class to Components from Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 18, 2017
1 parent 371e02f commit 376d634
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 345 deletions.
32 changes: 14 additions & 18 deletions src/Base/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Traits\InputOutputTrait;
use Inhere\Console\Traits\InputOutputAwareTrait;
use Inhere\Console\Traits\SimpleEventTrait;
use Inhere\Console\Style\Style;
use Inhere\Console\Utils\FormatUtil;
use Inhere\Console\Utils\Helper;

/**
Expand All @@ -21,7 +22,7 @@
*/
abstract class AbstractApplication implements ApplicationInterface
{
use InputOutputTrait, SimpleEventTrait;
use InputOutputAwareTrait, SimpleEventTrait;

/**
* @var array
Expand Down Expand Up @@ -183,7 +184,7 @@ protected function afterRun()
if ($this->isProfile()) {
$title = '---------- Runtime Stats(profile=true) ----------';
$stats = $this->meta['_stats'];
$this->meta['_stats'] = Helper::runtime($stats['startTime'], $stats['startMemory'], $stats);
$this->meta['_stats'] = FormatUtil::runtime($stats['startTime'], $stats['startMemory'], $stats);
$this->output->write('');
$this->output->aList($this->meta['_stats'], $title);
}
Expand Down Expand Up @@ -409,6 +410,7 @@ public function showCommandList($quit = true)
$commands = $this->commands;
$commandArr[] = PHP_EOL . '- <cyan>Independent Commands</cyan>';
ksort($commands);

foreach ($commands as $name => $command) {
$desc = $desPlaceholder;
$hasCommand = true;
Expand All @@ -434,26 +436,18 @@ public function showCommandList($quit = true)
// built in commands
$internalCommands = static::$internalCommands;
ksort($internalCommands);
array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
// array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");

// built in options
$internalOptions = FormatUtil::commandOptions(self::$internalOptions);

$this->output->mList([
//'There are all console controllers and independent commands.',
'Usage:' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
'Options:' => self::$internalOptions,
'Available Commands:' => array_merge($controllerArr, $commandArr, $internalCommands),
//'Independent Commands:' => $commandArr ?: '... No register any independent command',
// 'Internal Commands:' => $internalCommands,
'Options:' => $internalOptions,
'Internal Commands:' => $internalCommands,
'Available Commands:' => array_merge($controllerArr, $commandArr),
]);

// $this->output->mList([
// //'There are all console controllers and independent commands.',
// 'Usage:' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
// 'Options:' => self::$internalOptions,
// 'Group Commands:' => $controllerArr ?: '... No register any group command(controller)',
// 'Independent Commands:' => $commandArr ?: '... No register any independent command',
// 'Internal Commands:' => $internalCommands,
// ]);

unset($controllerArr, $commandArr, $internalCommands);
$this->output->write("More command information, please use: <cyan>$script {command} -h</cyan>");

Expand Down Expand Up @@ -535,6 +529,7 @@ public function getCommandNames()

/**
* @param array $controllers
* @throws \InvalidArgumentException
*/
public function setControllers(array $controllers)
{
Expand Down Expand Up @@ -566,6 +561,7 @@ public function isController($name)

/**
* @param array $commands
* @throws \InvalidArgumentException
*/
public function setCommands(array $commands)
{
Expand Down
9 changes: 5 additions & 4 deletions src/Base/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\InputDefinition;
use Inhere\Console\IO\Output;
use Inhere\Console\Traits\InputOutputTrait;
use Inhere\Console\Traits\InputOutputAwareTrait;
use Inhere\Console\Traits\UserInteractTrait;
use Inhere\Console\Utils\Annotation;

/**
* Class AbstractCommand
* @package Inhere\Console
*/
abstract class AbstractCommand implements CommandInterface
abstract class AbstractCommand implements BaseCommandInterface
{
use InputOutputTrait, UserInteractTrait;
use InputOutputAwareTrait, UserInteractTrait;

// name -> {$name}
const OK = 0;
// name -> {name}
const ANNOTATION_VAR = '{%s}'; // '{$%s}';

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Base/BaseCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,38 @@

namespace Inhere\Console\Base;

use Inhere\Console\IO\InputDefinition;

/**
* Interface BaseCommandInterface
* @package Inhere\Console\Base
*/
interface BaseCommandInterface
{
/**
* run command
* @param string $command
* @return int
*/
public function run($command = '');

/**
* @return InputDefinition
*/
public function getDefinition();

/**
* @return ApplicationInterface
*/
public function getApp(): ApplicationInterface;

/**
* @return string
*/
public static function getName(): string;

/**
* @return string
*/
public static function getDescription();
}
30 changes: 1 addition & 29 deletions src/Base/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,10 @@

namespace Inhere\Console\Base;

use Inhere\Console\IO\InputDefinition;

/**
* Interface CommandInterface
* @package Inhere\Console\Base
*/
interface CommandInterface
interface CommandInterface extends BaseCommandInterface
{
/**
* run command
* @param string $command
* @return int
*/
public function run($command = '');

/**
* @return InputDefinition
*/
public function getDefinition();

/**
* @return ApplicationInterface
*/
public function getApp(): ApplicationInterface;

/**
* @return string
*/
public static function getName(): string;

/**
* @return string
*/
public static function getDescription();
}
3 changes: 3 additions & 0 deletions src/Base/ControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface ControllerInterface
*/
public function helpCommand();

/**
* show command list of the controller class
*/
public function showCommandList();

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
namespace Inhere\Console;

use Inhere\Console\Base\AbstractCommand;
use Inhere\Console\Base\CommandInterface;

/**
* Class Command
* @package Inhere\Console
*/
abstract class Command extends AbstractCommand
abstract class Command extends AbstractCommand implements CommandInterface
{
/*
* do execute
Expand Down
6 changes: 4 additions & 2 deletions src/Components/AnsiCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* Time: 9:35
*/

namespace Inhere\Console\Utils;
namespace Inhere\Console\Components;

use Inhere\Console\Utils\Show;

/**
* Class AnsiCode terminal
* @package Inhere\Console\Utils
* @package Inhere\Console\Components
*
* 2K 清除本行
* \x0D = \r = 13 回车,回到行首
Expand Down
4 changes: 2 additions & 2 deletions src/Components/ArtFont.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 9:23
*/

namespace Inhere\Console\Utils;
namespace Inhere\Console\Components;

/**
* Class ArtFont
* @package Inhere\Console\Utils
* @package Inhere\Console\Components
*/
class ArtFont
{
Expand Down
16 changes: 11 additions & 5 deletions src/Components/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Time: 19:08
*/

namespace Inhere\Console\Utils;
namespace Inhere\Console\Components;

use Inhere\Console\Utils\Show;

/**
* Class Download
* @package Inhere\Console\Utils
* @package Inhere\Console\Components
*/
class Download
final class Download
{
const PROGRESS_TEXT = 'text';
const PROGRESS_BAR = 'bar';
Expand Down Expand Up @@ -65,6 +67,10 @@ public function __construct(string $url, string $saveAs, $type = self::PROGRESS_
$this->showType = $type === self::PROGRESS_BAR ? self::PROGRESS_BAR : self::PROGRESS_TEXT;
}

/**
* start download
* @return $this
*/
public function start()
{
if (!$this->url || !$this->saveAs) {
Expand Down Expand Up @@ -113,7 +119,7 @@ public function start()
* @param int $transferredBytes Have been transferred bytes
* @param int $maxBytes Target max length bytes
*/
protected function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
public function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
{
$msg = '';

Expand Down Expand Up @@ -160,7 +166,7 @@ protected function progressShow($notifyCode, $severity, $message, $messageCode,
* @param $transferredBytes
* @return string
*/
protected function showProgressByType($transferredBytes)
public function showProgressByType($transferredBytes)
{
if ($transferredBytes <= 0) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/Components/StrBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 9:17
*/

namespace Inhere\Console\Utils;
namespace Inhere\Console\Components;

/**
* Class StrBuffer
* @package Inhere\Console\Utils
* @package Inhere\Console\Components
*/
class StrBuffer
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Inhere\Console\Base\ControllerInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Utils\FormatUtil;
use Inhere\Console\Utils\Helper;
use Inhere\Console\Utils\Annotation;

Expand Down Expand Up @@ -74,7 +75,7 @@ protected function configure()
*/
protected function execute($input, $output)
{
$action = Helper::camelCase(trim($this->action ?: $this->defaultAction, $this->delimiter));
$action = FormatUtil::camelCase(trim($this->action ?: $this->defaultAction, $this->delimiter));
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;

// the action method exists and only allow access public method.
Expand Down Expand Up @@ -145,7 +146,7 @@ final public function helpCommand()
return 0;
}

$action = Helper::camelCase($action);
$action = FormatUtil::camelCase($action);
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;

// show help info for a command.
Expand Down Expand Up @@ -250,7 +251,7 @@ public function getAction(): string
public function setAction(string $action)
{
if ($action) {
$this->action = Helper::camelCase($action);
$this->action = FormatUtil::camelCase($action);
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/FormatOutputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function dump(...$vars)
/**
* @param array ...$vars
*/
public function print(...$vars)
public function prints(...$vars)
{
Show::write(Helper::printVars(...$vars));
}
Expand Down
Loading

0 comments on commit 376d634

Please sign in to comment.