44
55namespace Lukasojd \PureGit \CLI ;
66
7- use Lukasojd \PureGit \CLI \Command \AddCommand ;
8- use Lukasojd \PureGit \CLI \Command \BranchCommand ;
9- use Lukasojd \PureGit \CLI \Command \CheckoutCommand ;
107use Lukasojd \PureGit \CLI \Command \CliCommand ;
11- use Lukasojd \PureGit \CLI \Command \CloneCommand ;
12- use Lukasojd \PureGit \CLI \Command \CommitCommand ;
13- use Lukasojd \PureGit \CLI \Command \CommitGraphCommand ;
14- use Lukasojd \PureGit \CLI \Command \ConfigCommand ;
15- use Lukasojd \PureGit \CLI \Command \DiffCommand ;
16- use Lukasojd \PureGit \CLI \Command \FetchCommand ;
17- use Lukasojd \PureGit \CLI \Command \InitCommand ;
18- use Lukasojd \PureGit \CLI \Command \LogCommand ;
19- use Lukasojd \PureGit \CLI \Command \MergeCommand ;
20- use Lukasojd \PureGit \CLI \Command \MvCommand ;
21- use Lukasojd \PureGit \CLI \Command \PullCommand ;
22- use Lukasojd \PureGit \CLI \Command \PushCommand ;
23- use Lukasojd \PureGit \CLI \Command \ResetCommand ;
24- use Lukasojd \PureGit \CLI \Command \RmCommand ;
25- use Lukasojd \PureGit \CLI \Command \ShowCommand ;
26- use Lukasojd \PureGit \CLI \Command \StatusCommand ;
27- use Lukasojd \PureGit \CLI \Command \TagCommand ;
288
299final class Application
3010{
31- private const string VERSION = '0.1 .0 ' ;
11+ private const string VERSION = '1.0 .0 ' ;
3212
3313 /**
34- * @var array<string, CliCommand>
14+ * @var array<string, class-string< CliCommand> >
3515 */
36- private array $ commands = [];
37-
38- public function __construct ()
39- {
40- $ this ->registerCommand (new InitCommand ());
41- $ this ->registerCommand (new AddCommand ());
42- $ this ->registerCommand (new CommitCommand ());
43- $ this ->registerCommand (new StatusCommand ());
44- $ this ->registerCommand (new LogCommand ());
45- $ this ->registerCommand (new DiffCommand ());
46- $ this ->registerCommand (new BranchCommand ());
47- $ this ->registerCommand (new TagCommand ());
48- $ this ->registerCommand (new CheckoutCommand ());
49- $ this ->registerCommand (new MergeCommand ());
50- $ this ->registerCommand (new ResetCommand ());
51- $ this ->registerCommand (new ShowCommand ());
52- $ this ->registerCommand (new RmCommand ());
53- $ this ->registerCommand (new MvCommand ());
54- $ this ->registerCommand (new CommitGraphCommand ());
55- $ this ->registerCommand (new CloneCommand ());
56- $ this ->registerCommand (new FetchCommand ());
57- $ this ->registerCommand (new PullCommand ());
58- $ this ->registerCommand (new PushCommand ());
59- $ this ->registerCommand (new ConfigCommand ());
60- }
16+ private const array COMMAND_MAP = [
17+ 'init ' => Command \InitCommand::class,
18+ 'add ' => Command \AddCommand::class,
19+ 'commit ' => Command \CommitCommand::class,
20+ 'status ' => Command \StatusCommand::class,
21+ 'log ' => Command \LogCommand::class,
22+ 'diff ' => Command \DiffCommand::class,
23+ 'branch ' => Command \BranchCommand::class,
24+ 'tag ' => Command \TagCommand::class,
25+ 'checkout ' => Command \CheckoutCommand::class,
26+ 'merge ' => Command \MergeCommand::class,
27+ 'reset ' => Command \ResetCommand::class,
28+ 'show ' => Command \ShowCommand::class,
29+ 'rm ' => Command \RmCommand::class,
30+ 'mv ' => Command \MvCommand::class,
31+ 'commit-graph ' => Command \CommitGraphCommand::class,
32+ 'clone ' => Command \CloneCommand::class,
33+ 'fetch ' => Command \FetchCommand::class,
34+ 'pull ' => Command \PullCommand::class,
35+ 'push ' => Command \PushCommand::class,
36+ 'config ' => Command \ConfigCommand::class,
37+ ];
6138
6239 /**
6340 * @param list<string> $argv
6441 */
6542 public function run (array $ argv ): int
6643 {
67- array_shift ($ argv ) ?? ' puregit ' ;
44+ array_shift ($ argv );
6845
6946 if ($ argv === []) {
7047 $ this ->printUsage ();
@@ -86,13 +63,13 @@ public function run(array $argv): int
8663 return 0 ;
8764 }
8865
89- if ($ commandName === null || ! isset ($ this -> commands [$ commandName ])) {
90- fwrite (STDERR , sprintf ("puregit: '%s' is not a puregit command. See 'puregit --help'. \n" , $ commandName ?? '' ));
66+ if (! isset (self :: COMMAND_MAP [$ commandName ])) {
67+ fwrite (STDERR , sprintf ("puregit: '%s' is not a puregit command. See 'puregit --help'. \n" , $ commandName ));
9168
9269 return 1 ;
9370 }
9471
95- $ command = $ this -> commands [$ commandName ];
72+ $ command = new ( self :: COMMAND_MAP [$ commandName ])() ;
9673
9774 if (in_array ('--help ' , $ argv , true ) || in_array ('-h ' , $ argv , true )) {
9875 fwrite (STDOUT , sprintf ("Usage: puregit %s \n\n%s \n" , $ command ->usage (), $ command ->description ()));
@@ -109,18 +86,14 @@ public function run(array $argv): int
10986 }
11087 }
11188
112- private function registerCommand (CliCommand $ command ): void
113- {
114- $ this ->commands [$ command ->name ()] = $ command ;
115- }
116-
11789 private function printUsage (): void
11890 {
11991 fwrite (STDOUT , sprintf ("puregit version %s — Pure PHP Git implementation \n\n" , self ::VERSION ));
12092 fwrite (STDOUT , "Usage: puregit <command> [<args>] \n\n" );
12193 fwrite (STDOUT , "Available commands: \n" );
12294
123- foreach ($ this ->commands as $ name => $ command ) {
95+ foreach (self ::COMMAND_MAP as $ name => $ class ) {
96+ $ command = new $ class ();
12497 fwrite (STDOUT , sprintf (" %-12s %s \n" , $ name , $ command ->description ()));
12598 }
12699
0 commit comments