Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input Layer #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Command/CommandContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Minicli\Framework\Command;

use Minicli\Framework\Input\Input;

interface CommandContract
{
/**
* Executes the command logic.
*/
public function handle(Input $input): void;

/**
* Called after the command is executed successfully.
*/
public function teardown(): void;
}
19 changes: 19 additions & 0 deletions src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Minicli\Framework\Command;

use Minicli\Framework\Input\Input;

final class HelpCommand implements CommandContract
{
public function handle(Input $input): void
{
// TODO: Implement handle() method.
}

public function teardown(): void
{
}
}
125 changes: 125 additions & 0 deletions src/Input/Input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace Minicli\Framework\Input;

final class Input
{
private const DEFAULT_COMMAND = 'help';

/**
* The command to be called.
*/
private string $command = '';

/**
* The raw arguments passed to the command.
*
* @var array<int,string> $rawArgs
*/
private array $rawArgs = [];

/**
* The raw arguments passed to the command.
*
* @var array<int,string> $args
*/
private array $args = [];

/**
* The parsed parameters passed to the command.
*
* @var array<string,string> $params
*/
private array $params = [];

/**
* The parsed flags passed to the command.
*
* @var array<int, string> $flags
*/
private array $flags = [];

public function __construct(array $argv)
{
$this->rawArgs = $argv;

$this->parseInput();

if ($this->args[1]) {
$this->command = $this->args[1];
}

if ($this->args[2]) {
$this->command .= " {$this->args[2]}";
}

if ('' === $this->command) {
$this->command = self::DEFAULT_COMMAND;
}
}

public function command(): string
{
return $this->command;
}

/**
* @return array<string,string>
*/
public function params(): array
{
return $this->params;
}

public function param(string $param, ?string $default = null): ?string
{
return $this->params[$param] ?? $default;
}

public function hasParam(string $param): bool
{
return isset($this->params[$param]);
}

/**
* @return array<int,string>
*/
public function flags(): array
{
return $this->flags;
}

public function hasFlag(string $flag): bool
{
return in_array($flag, $this->flags) || in_array("--{$flag}", $this->flags);
}

/**
* @return array<int,string>
*/
public function getRawArgs(): array
{
return $this->rawArgs;
}

private function parseInput(): void
{
foreach ($this->rawArgs as $arg) {
$parts = explode('=', $arg);

if (count($parts) >= 2) {
$this->params[$parts[0]] = join('=', array_slice($parts, 1));
continue;
}

if (str_starts_with($arg, '--')) {
$this->flags[] = $arg;
continue;
}

$this->args[] = $arg;
}
}
}
36 changes: 36 additions & 0 deletions src/Minicli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Minicli\Framework;

use Minicli\Framework\Command\CommandContract;
use Minicli\Framework\Command\HelpCommand;
use Minicli\Framework\Configuration\Config;
use Minicli\Framework\Input\Input;
use Minicli\Framework\Theme\ThemeContract;

final class Minicli
Expand All @@ -14,6 +17,7 @@ final class Minicli
public function __construct(
protected readonly Config $config,
) {
$this->loadCommands();
}

/**
Expand All @@ -37,4 +41,36 @@ public static function boot(
config: $config
);
}

/**
* Runs the application with the given input.
*
* @param array<int,string> $argv
* @return void
*/
public function run(array $argv = []): void
{
$input = new Input($argv);
$command = $this->getCommand($input->command());

$command->handle($input);
$command->teardown();
}

/**
* Loads commands into the DI container.
*/
private function loadCommands(): void
{
// TODO: Implement
}

/**
* Gets command from DI container by name or returns the default one.
*/
private function getCommand(string $command): CommandContract
{
// TODO: Implement
return new HelpCommand();
}
}