Passed
Push — master ( b95edc...42d472 )
by Timm
02:04
created

AppParser::createSpecs()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 17
rs 9.9666
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
use Exception;
8
use GetOptionKit\ContinuousOptionParser;
9
use GetOptionKit\OptionResult;
10
11
class AppParser {
12
13
    public static function run(App $app): void {
14
        global $argv;
15
16
        try {
17
            $cmd = self::route($app, $argv);
18
19
            if ($cmd !== null) {
20
21
                $cmd->handleOptionParseException();
22
23
                $callable = $cmd->getCallable();
24
25
                if ($callable !== null) {
26
                    $callable($cmd);
27
                    return;
28
                }
29
30
                $methodName = $cmd->getMethodName();
31
32
                if (method_exists($app, $methodName)) {
33
                    $app->$methodName($cmd);
34
                    return;
35
                }
36
37
38
            }
39
        } catch (Exception $e) {
40
41
            App::eol();
42
            App::echo('Uups, someting went wrong!', Color::FOREGROUND_COLOR_RED);
43
            App::eol();
44
            App::echo($e->getMessage(), Color::FOREGROUND_COLOR_RED);
45
            App::eol();
46
        }
47
48
    }
49
50
    /**
51
     * @param App $app
52
     * @param array $argv
53
     * @return Cmd|null
54
     */
55
    public static function route(App $app, array $argv): ?Cmd {
56
57
        $cmd = $app->setup();
58
59
        $appspecs = $cmd->getOptionCollection();
60
61
        $parser = new ContinuousOptionParser($appspecs);
62
63
        try {
64
            $cmd->optionResult = $parser->parse($argv);
65
        } catch (Exception $e) {
66
            $cmd->optionParseException = $e;
67
            return $cmd;
68
        }
69
70
        while (!$parser->isEnd()) {
71
72
            $currentArgument = $parser->getCurrentArgument();
73
74
            if ($cmd->existsSubCmd($currentArgument)) {
75
76
                $parser->advance();
77
78
                $cmd = $cmd->getSubCmd($currentArgument);
79
            }
80
81
            if ($cmd instanceof Cmd) {
82
83
                $options_parsed = $cmd->optionResult !== null;
84
85
                if (!$options_parsed) {
86
87
                    $cmd->optionResult = new OptionResult();
88
89
                    if (!empty($cmd->optionSpecs)) {
90
91
                        $specs = $cmd->getOptionCollection();
92
93
                        $parser->setSpecs($specs);
94
95
                        try {
96
                            $cmd->optionResult = $parser->continueParse();
97
                        } catch (Exception $e) {
98
                            $cmd->optionParseException = $e;
99
                            return $cmd;
100
                        }
101
102
                        continue;
103
                    }
104
                }
105
106
                if ($options_parsed) {
107
                    $cmd->arguments[] = $parser->advance();
108
                }
109
            }
110
111
        }
112
113
        return $cmd;
114
    }
115
116
117
}