Skip to content

Commit 376d634

Browse files
committed
move some class to Components from Utils
1 parent 371e02f commit 376d634

19 files changed

+390
-345
lines changed

src/Base/AbstractApplication.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
use Inhere\Console\IO\Input;
1212
use Inhere\Console\IO\Output;
13-
use Inhere\Console\Traits\InputOutputTrait;
13+
use Inhere\Console\Traits\InputOutputAwareTrait;
1414
use Inhere\Console\Traits\SimpleEventTrait;
1515
use Inhere\Console\Style\Style;
16+
use Inhere\Console\Utils\FormatUtil;
1617
use Inhere\Console\Utils\Helper;
1718

1819
/**
@@ -21,7 +22,7 @@
2122
*/
2223
abstract class AbstractApplication implements ApplicationInterface
2324
{
24-
use InputOutputTrait, SimpleEventTrait;
25+
use InputOutputAwareTrait, SimpleEventTrait;
2526

2627
/**
2728
* @var array
@@ -183,7 +184,7 @@ protected function afterRun()
183184
if ($this->isProfile()) {
184185
$title = '---------- Runtime Stats(profile=true) ----------';
185186
$stats = $this->meta['_stats'];
186-
$this->meta['_stats'] = Helper::runtime($stats['startTime'], $stats['startMemory'], $stats);
187+
$this->meta['_stats'] = FormatUtil::runtime($stats['startTime'], $stats['startMemory'], $stats);
187188
$this->output->write('');
188189
$this->output->aList($this->meta['_stats'], $title);
189190
}
@@ -409,6 +410,7 @@ public function showCommandList($quit = true)
409410
$commands = $this->commands;
410411
$commandArr[] = PHP_EOL . '- <cyan>Independent Commands</cyan>';
411412
ksort($commands);
413+
412414
foreach ($commands as $name => $command) {
413415
$desc = $desPlaceholder;
414416
$hasCommand = true;
@@ -434,26 +436,18 @@ public function showCommandList($quit = true)
434436
// built in commands
435437
$internalCommands = static::$internalCommands;
436438
ksort($internalCommands);
437-
array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
439+
// array_unshift($internalCommands, "\n- <cyan>Internal Commands</cyan>");
440+
441+
// built in options
442+
$internalOptions = FormatUtil::commandOptions(self::$internalOptions);
438443

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

448-
// $this->output->mList([
449-
// //'There are all console controllers and independent commands.',
450-
// 'Usage:' => "$script {command} [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
451-
// 'Options:' => self::$internalOptions,
452-
// 'Group Commands:' => $controllerArr ?: '... No register any group command(controller)',
453-
// 'Independent Commands:' => $commandArr ?: '... No register any independent command',
454-
// 'Internal Commands:' => $internalCommands,
455-
// ]);
456-
457451
unset($controllerArr, $commandArr, $internalCommands);
458452
$this->output->write("More command information, please use: <cyan>$script {command} -h</cyan>");
459453

@@ -535,6 +529,7 @@ public function getCommandNames()
535529

536530
/**
537531
* @param array $controllers
532+
* @throws \InvalidArgumentException
538533
*/
539534
public function setControllers(array $controllers)
540535
{
@@ -566,6 +561,7 @@ public function isController($name)
566561

567562
/**
568563
* @param array $commands
564+
* @throws \InvalidArgumentException
569565
*/
570566
public function setCommands(array $commands)
571567
{

src/Base/AbstractCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
use Inhere\Console\IO\Input;
1313
use Inhere\Console\IO\InputDefinition;
1414
use Inhere\Console\IO\Output;
15-
use Inhere\Console\Traits\InputOutputTrait;
15+
use Inhere\Console\Traits\InputOutputAwareTrait;
1616
use Inhere\Console\Traits\UserInteractTrait;
1717
use Inhere\Console\Utils\Annotation;
1818

1919
/**
2020
* Class AbstractCommand
2121
* @package Inhere\Console
2222
*/
23-
abstract class AbstractCommand implements CommandInterface
23+
abstract class AbstractCommand implements BaseCommandInterface
2424
{
25-
use InputOutputTrait, UserInteractTrait;
25+
use InputOutputAwareTrait, UserInteractTrait;
2626

27-
// name -> {$name}
27+
const OK = 0;
28+
// name -> {name}
2829
const ANNOTATION_VAR = '{%s}'; // '{$%s}';
2930

3031
/**

src/Base/BaseCommandInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,38 @@
88

99
namespace Inhere\Console\Base;
1010

11+
use Inhere\Console\IO\InputDefinition;
1112

13+
/**
14+
* Interface BaseCommandInterface
15+
* @package Inhere\Console\Base
16+
*/
1217
interface BaseCommandInterface
1318
{
19+
/**
20+
* run command
21+
* @param string $command
22+
* @return int
23+
*/
24+
public function run($command = '');
25+
26+
/**
27+
* @return InputDefinition
28+
*/
29+
public function getDefinition();
30+
31+
/**
32+
* @return ApplicationInterface
33+
*/
34+
public function getApp(): ApplicationInterface;
35+
36+
/**
37+
* @return string
38+
*/
39+
public static function getName(): string;
1440

41+
/**
42+
* @return string
43+
*/
44+
public static function getDescription();
1545
}

src/Base/CommandInterface.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,10 @@
88

99
namespace Inhere\Console\Base;
1010

11-
use Inhere\Console\IO\InputDefinition;
12-
1311
/**
1412
* Interface CommandInterface
1513
* @package Inhere\Console\Base
1614
*/
17-
interface CommandInterface
15+
interface CommandInterface extends BaseCommandInterface
1816
{
19-
/**
20-
* run command
21-
* @param string $command
22-
* @return int
23-
*/
24-
public function run($command = '');
25-
26-
/**
27-
* @return InputDefinition
28-
*/
29-
public function getDefinition();
30-
31-
/**
32-
* @return ApplicationInterface
33-
*/
34-
public function getApp(): ApplicationInterface;
35-
36-
/**
37-
* @return string
38-
*/
39-
public static function getName(): string;
40-
41-
/**
42-
* @return string
43-
*/
44-
public static function getDescription();
4517
}

src/Base/ControllerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ interface ControllerInterface
1919
*/
2020
public function helpCommand();
2121

22+
/**
23+
* show command list of the controller class
24+
*/
2225
public function showCommandList();
2326

2427
/**

src/Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
namespace Inhere\Console;
1010

1111
use Inhere\Console\Base\AbstractCommand;
12+
use Inhere\Console\Base\CommandInterface;
1213

1314
/**
1415
* Class Command
1516
* @package Inhere\Console
1617
*/
17-
abstract class Command extends AbstractCommand
18+
abstract class Command extends AbstractCommand implements CommandInterface
1819
{
1920
/*
2021
* do execute

src/Components/AnsiCode.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* Time: 9:35
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
10+
11+
use Inhere\Console\Utils\Show;
1012

1113
/**
1214
* Class AnsiCode terminal
13-
* @package Inhere\Console\Utils
15+
* @package Inhere\Console\Components
1416
*
1517
* 2K 清除本行
1618
* \x0D = \r = 13 回车,回到行首

src/Components/ArtFont.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Time: 9:23
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
1010

1111
/**
1212
* Class ArtFont
13-
* @package Inhere\Console\Utils
13+
* @package Inhere\Console\Components
1414
*/
1515
class ArtFont
1616
{

src/Components/Download.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
* Time: 19:08
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
10+
11+
use Inhere\Console\Utils\Show;
1012

1113
/**
1214
* Class Download
13-
* @package Inhere\Console\Utils
15+
* @package Inhere\Console\Components
1416
*/
15-
class Download
17+
final class Download
1618
{
1719
const PROGRESS_TEXT = 'text';
1820
const PROGRESS_BAR = 'bar';
@@ -65,6 +67,10 @@ public function __construct(string $url, string $saveAs, $type = self::PROGRESS_
6567
$this->showType = $type === self::PROGRESS_BAR ? self::PROGRESS_BAR : self::PROGRESS_TEXT;
6668
}
6769

70+
/**
71+
* start download
72+
* @return $this
73+
*/
6874
public function start()
6975
{
7076
if (!$this->url || !$this->saveAs) {
@@ -113,7 +119,7 @@ public function start()
113119
* @param int $transferredBytes Have been transferred bytes
114120
* @param int $maxBytes Target max length bytes
115121
*/
116-
protected function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
122+
public function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
117123
{
118124
$msg = '';
119125

@@ -160,7 +166,7 @@ protected function progressShow($notifyCode, $severity, $message, $messageCode,
160166
* @param $transferredBytes
161167
* @return string
162168
*/
163-
protected function showProgressByType($transferredBytes)
169+
public function showProgressByType($transferredBytes)
164170
{
165171
if ($transferredBytes <= 0) {
166172
return '';

src/Components/StrBuffer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Time: 9:17
77
*/
88

9-
namespace Inhere\Console\Utils;
9+
namespace Inhere\Console\Components;
1010

1111
/**
1212
* Class StrBuffer
13-
* @package Inhere\Console\Utils
13+
* @package Inhere\Console\Components
1414
*/
1515
class StrBuffer
1616
{

0 commit comments

Comments
 (0)