-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c97512
commit b0d9f35
Showing
17 changed files
with
598 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Console Argument | ||
* @package Rayleigh\Console | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
final /* readonly */ class Argument | ||
{ | ||
/** | ||
* Argument constructor | ||
* @param string $name argument name | ||
* @param bool $required default: false | ||
* @param null|string $default default value, default: null | ||
* @param string $description text description, default: '' | ||
*/ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly bool $required = false, | ||
public readonly ?string $default = null, | ||
public readonly string $description = '', | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Console CommandCannotBeInstanciatedException | ||
* @package Rayleigh\Console | ||
*/ | ||
final class CommandCannotBeInstanciatedException extends RuntimeException | ||
{ | ||
public function __construct(string $commandName) | ||
{ | ||
parent::__construct("Command {$commandName} cannot be instanciated"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console; | ||
|
||
/** | ||
* Console CommandInterface | ||
* @package Rayleigh\Console | ||
*/ | ||
interface CommandInterface | ||
{ | ||
/** | ||
* Get command name | ||
* @return string e.g. help | ||
*/ | ||
public function getCommandName(): string; | ||
|
||
/** | ||
* Get command description | ||
* @return string e.g. This is a help command | ||
*/ | ||
public function getCommandDescription(): string; | ||
|
||
/** | ||
* Execute command | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return int exit code, 0 when success | ||
*/ | ||
public function execute(InputInterface $input, OutputInterface $output): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console\Commands; | ||
|
||
use Rayleigh\Console\CommandInterface; | ||
use Rayleigh\Console\InputInterface; | ||
use Rayleigh\Console\OutputInterface; | ||
|
||
/** | ||
* Console ListCommand | ||
* @package Rayleigh\Console\Commands | ||
*/ | ||
final class ListCommand implements CommandInterface | ||
{ | ||
/** | ||
* ListCommand constructor | ||
* @param CommandInterface[] $commands | ||
*/ | ||
public function __construct( | ||
private readonly array $commands, | ||
) { | ||
} | ||
|
||
public function getCommandName(): string | ||
{ | ||
return 'list'; | ||
} | ||
|
||
public function getCommandDescription(): string | ||
{ | ||
return 'List all commands'; | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
foreach ($this->commands as $command) { | ||
$output->writeLine($command->getCommandName() . ' - ' . $command->getCommandDescription()); | ||
} | ||
$output->writeLine('list - List all commands'); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console\Input; | ||
|
||
use Rayleigh\Console\InputInterface; | ||
|
||
/** | ||
* Console ArgvInput | ||
* @package Rayleigh\Console | ||
* example: | ||
* ```php | ||
* $input = new ArgvInput(); | ||
* ``` | ||
*/ | ||
final class ArgvInput implements InputInterface | ||
{ | ||
/** @var array<array-key, scalar> $input */ | ||
public readonly array $input; | ||
|
||
/** | ||
* ArrayInput constructor | ||
* @param null|array<array-key, scalar> $input | ||
*/ | ||
public function __construct( | ||
?array $input = null, | ||
) { | ||
$this->input = $input ?? $_SERVER['argv'] ?? []; | ||
} | ||
|
||
public function getCommandName(): ?string | ||
{ | ||
foreach ($this->input as $key => $value) { | ||
if (\is_int($key) && \is_string($value) && !\str_starts_with($value, '-')) { | ||
return $value; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console\Input; | ||
|
||
use Rayleigh\Console\InputInterface; | ||
|
||
/** | ||
* Console ArrayInput | ||
* @package Rayleigh\Console | ||
* example: | ||
* ```php | ||
* $input = new ArrayInput([ | ||
* 'help', | ||
* '--verbose' => 'true', | ||
* ]); | ||
* ``` | ||
*/ | ||
final readonly class ArrayInput implements InputInterface | ||
{ | ||
/** | ||
* ArrayInput constructor | ||
* @param array<array-key, scalar> $input | ||
*/ | ||
public function __construct( | ||
public array $input, | ||
) { | ||
} | ||
|
||
public function getCommandName(): ?string | ||
{ | ||
foreach ($this->input as $key => $value) { | ||
if (\is_int($key) && \is_string($value) && !\str_starts_with($value, '-')) { | ||
return $value; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Masaru Yamagishi <[email protected]> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
namespace Rayleigh\Console; | ||
|
||
/** | ||
* Console InputInterface | ||
* @package Rayleigh\Console | ||
*/ | ||
interface InputInterface | ||
{ | ||
/** | ||
* Get main command name to execute | ||
* @return null|string null when no name provided | ||
*/ | ||
public function getCommandName(): ?string; | ||
} |
Oops, something went wrong.