1 | <?php |
||
2 | |||
3 | namespace yentu; |
||
4 | |||
5 | use clearice\argparser\ArgumentParser; |
||
6 | use clearice\io\Io; |
||
7 | use yentu\commands\Command; |
||
8 | use SebastianBergmann\Version; |
||
9 | |||
10 | class Cli |
||
11 | { |
||
12 | private ?Command $command; |
||
13 | private Io $io; |
||
14 | private ArgumentParser $argumentParser; |
||
15 | private const string VERSION = "v0.4.0"; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
16 | |||
17 | public function __construct(Io $io, ArgumentParser $argumentParser, ?Command $command = null) |
||
18 | { |
||
19 | $this->command = $command; |
||
20 | $this->io = $io; |
||
21 | $this->argumentParser = $argumentParser; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Display the greeting for the CLI user interface. |
||
26 | */ |
||
27 | private function greet(): void |
||
28 | { |
||
29 | $version = $this->getVersion(); |
||
30 | $welcome = <<<WELCOME |
||
31 | Yentu Database Migration Tool |
||
32 | Version $version |
||
33 | |||
34 | |||
35 | WELCOME; |
||
36 | $this->io->output($welcome); |
||
37 | } |
||
38 | |||
39 | private function getVersion(): string |
||
40 | { |
||
41 | if (defined('PHING_BUILD_VERSION')) { |
||
42 | return PHING_BUILD_VERSION; |
||
43 | } else { |
||
44 | $version = new Version(self::VERSION, dirname(__DIR__)); |
||
45 | return $version->asString(); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | public function run(): int |
||
50 | { |
||
51 | $this->greet(); |
||
52 | $status = 0; |
||
53 | |||
54 | if($this->command === null) { |
||
55 | $this->io->error($this->argumentParser->getHelpMessage()); |
||
56 | } else { |
||
57 | try { |
||
58 | $this->command->run(); |
||
59 | } catch (\yentu\exceptions\NonReversibleCommandException $e) { |
||
60 | $this->io->resetOutputLevel(); |
||
61 | $this->io->error("\nError: " . $e->getMessage() . "\n"); |
||
62 | $status = 1; |
||
63 | } |
||
64 | catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
||
65 | $this->io->resetOutputLevel(); |
||
66 | $this->io->error("\nDatabase error: " . $e->getMessage() . "\n"); |
||
67 | $this->command->reverse(); |
||
68 | $status = 2; |
||
69 | } |
||
70 | catch (\yentu\exceptions\YentuException $e) { |
||
71 | $this->io->resetOutputLevel(); |
||
72 | $this->io->error("\nError: " . $e->getMessage() . "\n"); |
||
73 | $this->command->reverse(); |
||
74 | $status = 3; |
||
75 | } catch (\PDOException $e) { |
||
76 | $this->io->resetOutputLevel(); |
||
77 | $this->io->error("\nFailed to connect to database: {$e->getMessage()}\n"); |
||
78 | $status = 4; |
||
79 | } catch (\ntentan\utils\exceptions\FileNotFoundException $e) { |
||
80 | $this->io->resetOutputLevel(); |
||
81 | $this->io->error($e->getMessage() . "\n"); |
||
82 | $status = 5; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return $status; |
||
87 | } |
||
88 | } |
||
89 |