|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yentu; |
|
4
|
|
|
|
|
5
|
|
|
use clearice\argparser\ArgumentParser; |
|
6
|
|
|
use clearice\io\Io; |
|
7
|
|
|
use yentu\commands\Command; |
|
8
|
|
|
|
|
9
|
|
|
class Cli |
|
10
|
|
|
{ |
|
11
|
|
|
private $command; |
|
12
|
|
|
private $io; |
|
13
|
|
|
private $argumentParser; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(Io $io, ArgumentParser $argumentParser, Command $command = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->command = $command; |
|
18
|
|
|
$this->io = $io; |
|
19
|
|
|
$this->argumentParser = $argumentParser; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Display the greeting for the CLI user interface. |
|
24
|
|
|
*/ |
|
25
|
|
|
private function greet() |
|
26
|
|
|
{ |
|
27
|
|
|
$version = $this->getVersion(); |
|
28
|
|
|
$welcome = <<<WELCOME |
|
29
|
|
|
Yentu Database Migration Tool |
|
30
|
|
|
Version $version |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
WELCOME; |
|
34
|
|
|
$this->io->output($welcome); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function getVersion() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
if (defined('PHING_BUILD_VERSION')) { |
|
40
|
|
|
return PHING_BUILD_VERSION; |
|
41
|
|
|
} else { |
|
42
|
|
|
$version = new \SebastianBergmann\Version(Yentu::VERSION, dirname(__DIR__)); |
|
43
|
|
|
return $version->getVersion(); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function run() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->greet(); |
|
50
|
|
|
if($this->command) { |
|
51
|
|
|
try { |
|
52
|
|
|
$this->command->run(); |
|
53
|
|
|
} catch (\yentu\exceptions\CommandException $e) { |
|
54
|
|
|
$this->io->resetOutputLevel(); |
|
55
|
|
|
$this->io->error("Error! " . $e->getMessage() . "\n"); |
|
56
|
|
|
} catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
|
57
|
|
|
$this->io->resetOutputLevel(); |
|
58
|
|
|
$this->io->error("Database driver failed: " . $e->getMessage() . "\n"); |
|
59
|
|
|
if (isset($command)) { |
|
|
|
|
|
|
60
|
|
|
$command->reverse(); |
|
61
|
|
|
} |
|
62
|
|
|
} catch (\yentu\exceptions\DatabaseManipulatorException $e) { |
|
63
|
|
|
$this->io->resetOutputLevel(); |
|
64
|
|
|
$this->io->error("Failed to perform database action: " . $e->getMessage() . "\n"); |
|
65
|
|
|
if (isset($command)) { |
|
66
|
|
|
$command->reverse(); |
|
67
|
|
|
} |
|
68
|
|
|
} catch (\ntentan\atiaa\DescriptionException $e) { |
|
|
|
|
|
|
69
|
|
|
$this->io->resetOutputLevel(); |
|
70
|
|
|
$this->io->error("Failed to perform database action: " . $e->getMessage() . "\n"); |
|
71
|
|
|
if (isset($command)) { |
|
72
|
|
|
$command->reverse(); |
|
73
|
|
|
} |
|
74
|
|
|
} catch (\yentu\exceptions\SyntaxErrorException $e) { |
|
75
|
|
|
$this->io->resetOutputLevel(); |
|
76
|
|
|
$this->io->error("Error found in syntax: {$e->getMessage()}\n"); |
|
77
|
|
|
if (isset($command)) { |
|
78
|
|
|
$command->reverse(); |
|
79
|
|
|
} |
|
80
|
|
|
} catch (\PDOException $e) { |
|
81
|
|
|
$this->io->resetOutputLevel(); |
|
82
|
|
|
$this->io->error("Failed to connect to database: {$e->getMessage()}\n"); |
|
83
|
|
|
} catch (\ntentan\utils\exceptions\FileNotFoundException $e) { |
|
84
|
|
|
$this->io->resetOutputLevel(); |
|
85
|
|
|
$this->io->error($e->getMessage() . "\n"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} else { |
|
89
|
|
|
$this->io->error($this->argumentParser->getHelpMessage()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.