Skip to content

Commit

Permalink
fix: CLI request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
linc committed Dec 10, 2022
1 parent f731567 commit 0193ea3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
9 changes: 5 additions & 4 deletions bin/porter
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ require_once dirname(__DIR__) . '/bootstrap.php';

// Parse CLI request.
$input = \Porter\Request::instance()->parseCli();
\Porter\Request::instance()->load($input);

// Router.
$command = \Porter\Router::run(\Porter\Request::instance());
call_user_func($command, \Porter\Request::instance());
// Run if a request was found.
if (count($input)) {
\Porter\Request::instance()->load($input);
\Porter\Controller::run(\Porter\Request::instance());
}

// Console output should end in newline.
echo "\n";
6 changes: 2 additions & 4 deletions src/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ class Render
{
/**
* Output help to the CLI.
*
* @param Request $request
*/
public static function cliHelp(Request $request)
public static function cliHelp()
{
$options = $request->getAllOptions(true);
$options = Request::instance()->getAllOptions(true);

$output = '';

Expand Down
8 changes: 4 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ public function getOptCodes(array $options): array
*/
public function parseCli(): array
{
// Short-circuit the help flag.
global $argv;
$command = $argv[1];

if ($command !== 'run') {
return [$command => 1];
if ($argv[1] === '--help') {
Render::cliHelp();
return [];
}

// Get the options for 'run'.
Expand Down
11 changes: 8 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

class Router
{
public static function run($request): callable
/**
* Simple web router.
*
* @param Request $request
*
* @return callable
*/
public static function run(Request $request): callable
{
switch (true) {
case $request->get('showsource'): // Single source feature list.
Expand All @@ -14,8 +21,6 @@ public static function run($request): callable
return '\Porter\Render::viewSourcesTable';
case $request->get('targets'): // Overview table.
return '\Porter\Render::viewTargetsTable';
case $request->get('help'): // CLI help.
return '\Porter\Render::cliHelp';
case $request->get('package'): // Main export process.
return '\Porter\Controller::run';
default: // Starting Web UI (index).
Expand Down

0 comments on commit 0193ea3

Please sign in to comment.