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\NonReversibleCommandException $e) { |
54
|
|
|
$this->io->resetOutputLevel(); |
55
|
|
|
$this->io->error("\nError: " . $e->getMessage() . "\n"); |
56
|
|
|
} catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
57
|
|
|
$this->io->resetOutputLevel(); |
58
|
|
|
$this->io->error("\nDatabase error: " . $e->getMessage() . "\n"); |
59
|
|
|
$this->command->reverse(); |
60
|
|
|
} catch (\yentu\exceptions\YentuException $e) { |
61
|
|
|
$this->io->resetOutputLevel(); |
62
|
|
|
$this->io->error("\nError: " . $e->getMessage() . "\n"); |
63
|
|
|
$this->command->reverse(); |
64
|
|
|
} catch (\PDOException $e) { |
65
|
|
|
$this->io->resetOutputLevel(); |
66
|
|
|
$this->io->error("\nFailed to connect to database: {$e->getMessage()}\n"); |
67
|
|
|
} catch (\ntentan\utils\exceptions\FileNotFoundException $e) { |
68
|
|
|
$this->io->resetOutputLevel(); |
69
|
|
|
$this->io->error($e->getMessage() . "\n"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
$this->io->error($this->argumentParser->getHelpMessage()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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
@return
annotation as described here.