Skip to content

Commit

Permalink
refactor: move input flag class to Flag namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 12, 2021
1 parent 485d65f commit 0646bc8
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 33 deletions.
17 changes: 11 additions & 6 deletions src/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ abstract class AbstractHandler implements CommandHandlerInterface
'help' => true,
];

/**
* @var bool
*/
private $initialized = false;

/**
* @var InputDefinition|null
*/
Expand Down Expand Up @@ -134,10 +139,11 @@ public static function aliases(): array
/**
* Command constructor.
*
* @param Input $input
* @param Output $output
* @param Input $input
* @param Output $output
* @param InputDefinition|null $definition
*/
// TODO public function __construct(Input $input = null, Output $output = null, InputDefinition $definition = null)
public function __construct(Input $input, Output $output, InputDefinition $definition = null)
{
$this->input = $input;
Expand All @@ -147,15 +153,14 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
$this->definition = $definition;
}

$this->commentsVars = $this->annotationVars();

$this->init();

$this->afterInit();
}

protected function init(): void
{
$this->commentsVars = $this->annotationVars();

$this->afterInit();
}

protected function afterInit(): void
Expand Down
4 changes: 3 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
abstract class Command extends AbstractHandler implements CommandInterface
{
public const METHOD = 'execute';

/**
* @var Command
*/
Expand Down Expand Up @@ -73,7 +75,7 @@ protected function showHelp(): bool
return true;
}

$execMethod = 'execute';
$execMethod = self::METHOD;

$this->logf(Console::VERB_CRAZY, "display help info for the command: %s", self::getName());

Expand Down
2 changes: 1 addition & 1 deletion src/Concern/StyledOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ trait StyledOutputAwareTrait
{
/**
* @param string $text
* @param string $tag
* @param string $tag all tag please {@see Color::STYLES}
*
* @return int
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ public static function setApp(Application $app): void
self::$app = $app;
}

/**
* @return Input
*/
public static function getInput(): Input
{
return self::$app->getInput();
}

/**
* @return Output
*/
public static function getOutput(): Output
{
return self::$app->getOutput();
}

/**
* @param array $config
* @param Input|null $input
Expand Down
15 changes: 15 additions & 0 deletions src/Contract/InputFlagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
*/
interface InputFlagInterface
{
// fixed args and opts for a command/controller-command
public const ARG_REQUIRED = 1;

public const ARG_OPTIONAL = 2;

public const ARG_IS_ARRAY = 4;

public const OPT_BOOLEAN = 1; // eq symfony InputOption::VALUE_NONE

public const OPT_REQUIRED = 2;

public const OPT_OPTIONAL = 4;

public const OPT_IS_ARRAY = 8;

/**
* @param int $mode
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 10:33
*/

namespace Inhere\Console\IO\Input;
namespace Inhere\Console\Flag;

use Inhere\Console\IO\Input;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* Time: 10:11
*/

namespace Inhere\Console\IO\Input;
namespace Inhere\Console\Flag;

/**
* Class InputArguments
* - input arguments builder
*
* @package Inhere\Console\IO\Input
* @package Inhere\Console\Flag
*/
class InputArguments
{
Expand Down
35 changes: 17 additions & 18 deletions src/IO/Input/InputFlag.php → src/Flag/InputFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
* Time: 10:28
*/

namespace Inhere\Console\IO\Input;
namespace Inhere\Console\Flag;

use Inhere\Console\Contract\InputFlagInterface;
use Inhere\Console\IO\Input;

/**
* Class InputFlag
* - definition a input item(option|argument)
* Class Flag
* - - definition a input flag item(option|argument)
*
* @package Inhere\Console\IO\Input
*/
Expand All @@ -27,7 +26,7 @@ abstract class InputFlag implements InputFlagInterface
/**
* @var string
*/
private $description;
private $desc;

/**
* @var int
Expand All @@ -51,33 +50,33 @@ abstract class InputFlag implements InputFlagInterface
/**
* @param string $name
* @param int $mode see Input::ARG_* or Input::OPT_*
* @param string $description
* @param null $default
* @param string $desc
* @param mixed|null $default
*
* @return static
*/
public static function make(string $name, int $mode = 0, string $description = '', $default = null)
public static function make(string $name, int $mode = 0, string $desc = '', $default = null)
{
return new static($name, $mode, $description, $default);
return new static($name, $mode, $desc, $default);
}

/**
* Class constructor.
*
* @param string $name
* @param int $mode see Input::ARG_* or Input::OPT_*
* @param string $description
* @param string $desc
* @param mixed $default The default value
* - for Input::ARG_OPTIONAL mode only
* - must be null for InputOption::OPT_BOOL
*/
public function __construct(string $name, int $mode = 0, string $description = '', $default = null)
public function __construct(string $name, int $mode = 0, string $desc = '', $default = null)
{
$this->name = $name;
$this->mode = $mode;

$this->default = $default;
$this->setDescription($description);
$this->setDesc($desc);
}

/******************************************************************
Expand Down Expand Up @@ -166,17 +165,17 @@ public function setDefault($default): void
/**
* @return string
*/
public function getDescription(): string
public function getDesc(): string
{
return $this->description;
return $this->desc;
}

/**
* @param string $description
* @param string $desc
*/
public function setDescription(string $description): void
public function setDesc(string $desc): void
{
$this->description = $description;
$this->desc = $desc;
}

/**
Expand All @@ -192,7 +191,7 @@ public function toArray(): array
'isArray' => $this->isArray(),
'isOptional' => $this->isOptional(),
'isRequired' => $this->isRequired(),
'description' => $this->description,
'description' => $this->desc,
];
}
}
4 changes: 2 additions & 2 deletions src/IO/Input/InputOption.php → src/Flag/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 10:28
*/

namespace Inhere\Console\IO\Input;
namespace Inhere\Console\Flag;

use Inhere\Console\IO\Input;
use function implode;
Expand All @@ -15,7 +15,7 @@
* Class InputOption
* - definition a input option
*
* @package Inhere\Console\IO\Input
* @package Inhere\Console\Flag
*/
class InputOption extends InputFlag
{
Expand Down
4 changes: 2 additions & 2 deletions src/IO/Input/InputOptions.php → src/Flag/InputOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* Time: 10:10
*/

namespace Inhere\Console\IO\Input;
namespace Inhere\Console\Flag;

/**
* Class InputOptions
* - input options builder
*
* @package Inhere\Console\IO\Input
* @package Inhere\Console\Flag
*/
class InputOptions
{
Expand Down
60 changes: 60 additions & 0 deletions src/Handler/CallableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);


namespace Inhere\Console\Handler;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;

/**
* Class CallableCommand - wrap an callable as Command
*
* @package Inhere\Console\Handler
*/
class CallableCommand extends Command
{
/**
* @var callable
*/
private $callable;

// /**
// * @param callable $cb
// *
// * @return static
// */
// public static function wrap(callable $cb): self
// {
// return (new self())->setCallable($cb);
// }

/**
* @param callable $callable
*
* @return CallableCommand
*/
public function setCallable(callable $callable): self
{
$this->callable = $callable;
return $this;
}

/**
* Do execute command
*
* @param Input $input
* @param Output $output
*
* @return int|mixed
*/
protected function execute($input, $output)
{
if (!$call = $this->callable) {
throw new \BadMethodCallException('The callable property is empty');
}

// call custom callable
return $call($input, $output);
}
}

0 comments on commit 0646bc8

Please sign in to comment.