Skip to content

Commit

Permalink
[Console] WIP: Add Console Kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
il-masaru-yamagishi committed Sep 1, 2024
1 parent 7c97512 commit b0d9f35
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 1 deletion.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<testsuite name="Clock">
<directory>./src/Clock/Tests</directory>
</testsuite>
<testsuite name="Console">
<directory>./src/Console/Tests</directory>
</testsuite>
<testsuite name="Container">
<directory>./src/Container/Tests</directory>
</testsuite>
Expand Down
35 changes: 35 additions & 0 deletions src/Console/Argument.php
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 = '',
) {
}
}
24 changes: 24 additions & 0 deletions src/Console/CommandCannotBeInstanciatedException.php
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");
}
}
37 changes: 37 additions & 0 deletions src/Console/CommandInterface.php
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;
}
49 changes: 49 additions & 0 deletions src/Console/Commands/ListCommand.php
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;
}
}
46 changes: 46 additions & 0 deletions src/Console/Input/ArgvInput.php
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;
}
}
45 changes: 45 additions & 0 deletions src/Console/Input/ArrayInput.php
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;
}
}
23 changes: 23 additions & 0 deletions src/Console/InputInterface.php
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;
}
Loading

0 comments on commit b0d9f35

Please sign in to comment.