Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from Muetze42/development
Browse files Browse the repository at this point in the history
feat: catch command not found exception
  • Loading branch information
Muetze42 authored Oct 17, 2023
2 parents a58cab7 + c113ca5 commit a34a849
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 19 deletions.
10 changes: 0 additions & 10 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11889,16 +11889,6 @@ class View extends \Illuminate\Support\Facades\View
}

class Installer extends \NormanHuth\ConsoleApp\LuraInstaller{
public static function installerName(): string
{
return 'Norman Huth';
}

public static function installerAuthor(): string
{
return 'Lura';
}

public function runLura(mixed $command)
{
//
Expand Down
16 changes: 14 additions & 2 deletions bin/lura
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ $autoloader = file_exists(__DIR__ . '/../../../autoload.php') ?
require $autoloader;

use NormanHuth\ConsoleApp\App;
use Symfony\Component\Console\Exception\CommandNotFoundException;

try {
new App('Lura by Norman Huth', 2, 'install');
new App('Lura', 2, 'install');
} catch (CommandNotFoundException) {

$message = [
"\033[31mCommand not Found.\033[0m",
"Enter \033[32m`lura list`\033[0m to list available commands for the application.",
"Or enter \033[32m`lura`\033[0m to start a Installation process.",
];

echo implode("\n", $message);
exit();
} catch (Exception $exception) {
die('Error: ' . $exception);
echo "\033[31m$exception \033[0m\n";
exit();
}
9 changes: 6 additions & 3 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ class App
*
* @throws Exception
*/
public function __construct(string $appName = 'Console App', int|string $version = null, string $defaultCommand = null)
{
$this->container = new Container;
public function __construct(
string $appName = 'Console App',
int|string $version = null,
string $defaultCommand = null
) {
$this->container = new Container();
$this->events = new Dispatcher($this->container);
$this->artisan = new Application($this->container, $this->events, $this->setVersion($version));

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CacheClearCommand extends LuraCommand
*
* @var string
*/
protected $description = 'Flush the application cache';
protected $description = 'Flush the application cache.';

/**
* Execute the console command.
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ConfigFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConfigFileCommand extends LuraCommand
*
* @var string
*/
protected $description = 'Get the path to the local config file';
protected $description = 'Get the path to the local config file.';

/**
* Execute the console command.
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InstallCommand extends LuraCommand
*
* @var string
*/
protected $description = 'Create a new app';
protected $description = 'Create a new app with installer.';

/**
* Execute the console command.
Expand Down
120 changes: 120 additions & 0 deletions src/Console/Commands/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace NormanHuth\ConsoleApp\Console\Commands;

use NormanHuth\ConsoleApp\LuraCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\ApplicationDescription;
use Symfony\Component\Console\Helper\Helper;

class ListCommand extends LuraCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'list';

/**
* Names of the commands that should not be listed.
*
* @var array
*/
protected array $exceptCommands = [
'completion',
'help',
'install',
];

/**
* The console command description.
*
* @var string
*/
protected $description = 'ListCommand displays the list of all available commands for the application.';

/**
* Execute the console command.
*/
public function handle(): void
{
$description = new ApplicationDescription($this->getApplication());
$commands = $description->getCommands();
$namespaces = $description->getNamespaces();

$this->info('' . $this->getApplication()->getName() . '“ Commands');
$this->comment('Available commands:');

$width = $this->getColumnWidth(
array_merge(
...array_values(
array_map(
fn($namespace) => array_intersect(
$namespace['commands'],
array_keys($commands)
),
array_values($namespaces)
)
)
)
);

foreach ($namespaces as $namespace) {
$namespace['commands'] = array_filter($namespace['commands'], fn($name) => isset($commands[$name]));

if (!$namespace['commands']) {
continue;
}
foreach ($namespace['commands'] as $name) {
$spacingWidth = $width - Helper::width($name);
$command = $commands[$name];
if (in_array($name, $this->exceptCommands)) {
continue;
}
$commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : '';

$this->line(sprintf(
' <info>%s</info>%s%s',
$name,
str_repeat(' ', $spacingWidth),
$commandAliases . $command->getDescription()
));
}
}
}

/**
* @param array<Command|string> $commands
*/
protected function getColumnWidth(array $commands): int
{
foreach ($commands as $command) {
if ($command instanceof Command) {
$widths[] = Helper::width($command->getName());
foreach ($command->getAliases() as $alias) {
$widths[] = Helper::width($alias);
}
} else {
$widths[] = Helper::width($command);
}
}

return $widths ? max($widths) + 2 : 0;
}

/**
* Formats command aliases to show them in the command description.
*/
protected function getCommandAliasesText(Command $command): string
{
$text = '';
$aliases = $command->getAliases();

if ($aliases) {
$text = '[' . implode('|', $aliases) . '] ';
}

return $text;
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/RegisterInstallerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RegisterInstallerCommand extends LuraCommand
*
* @var string
*/
protected $description = 'Register a Installer repository';
protected $description = 'Register a Installer repository.';

/**
* Execute the console command.
Expand Down

0 comments on commit a34a849

Please sign in to comment.