This repository has been archived by the owner on Apr 6, 2024. It is now read-only.
generated from Muetze42/console-app-template
-
-
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.
Merge pull request #13 from Muetze42/development
feat: catch command not found exception
- Loading branch information
Showing
8 changed files
with
144 additions
and
19 deletions.
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
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
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
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,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; | ||
} | ||
} |
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