|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2014 James Ekow Abaka Ainooson |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace yentu\commands; |
|
28
|
|
|
|
|
29
|
|
|
use clearice\io\Io; |
|
30
|
|
|
use yentu\factories\DatabaseManipulatorFactory; |
|
31
|
|
|
use yentu\Migrations; |
|
32
|
|
|
use yentu\Parameters; |
|
33
|
|
|
use yentu\commands\Reversible; |
|
34
|
|
|
use yentu\exceptions\CommandException; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The init command class. This command intiates a project for yentu migration |
|
38
|
|
|
* by creating the required directories and configuration files. It also goes |
|
39
|
|
|
* ahead to create the history table which exists in the database. |
|
40
|
|
|
*/ |
|
41
|
|
|
class Init extends Command implements Reversible |
|
42
|
|
|
{ |
|
43
|
|
|
private $io; |
|
44
|
|
|
private $migrations; |
|
45
|
|
|
private $manipulatorFactory; |
|
46
|
|
|
|
|
47
|
41 |
|
public function __construct(Migrations $migrations, DatabaseManipulatorFactory $manipulatorFactory, Io $io) |
|
48
|
|
|
{ |
|
49
|
41 |
|
$this->migrations = $migrations; |
|
50
|
41 |
|
$this->io = $io; |
|
51
|
41 |
|
$this->manipulatorFactory = $manipulatorFactory; |
|
52
|
41 |
|
} |
|
53
|
|
|
|
|
54
|
32 |
|
private function getParams() |
|
55
|
|
|
{ |
|
56
|
32 |
|
if (isset($this->options['interractive'])) { |
|
57
|
3 |
|
$params['driver'] = $this->io->getResponse('Database type', ['required' => true, 'answers' => ['postgresql', 'mysql', 'sqlite']]); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
3 |
|
if ($params['driver'] === 'sqlite') { |
|
60
|
1 |
|
$params['file'] = $this->io->getResponse('Database file', ['required' => true]); |
|
61
|
|
|
} else { |
|
62
|
2 |
|
$params['host'] = $this->io->getResponse('Database host', ['default' => 'localhost']); |
|
63
|
2 |
|
$params['port'] = $this->io->getResponse('Database port'); |
|
64
|
2 |
|
$params['dbname'] = $this->io->getResponse('Database name',['required' => true]); |
|
65
|
2 |
|
$params['user'] = $this->io->getResponse('Database user name', ['required' => true]); |
|
66
|
3 |
|
$params['password'] = $this->io->getResponse('Database password', ['required' => FALSE]); |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
29 |
|
$params = []; |
|
70
|
29 |
|
foreach(['driver', 'file', 'host', 'port', 'dbname', 'user', 'password'] as $key) { |
|
71
|
29 |
|
if(isset($this->options[$key])) { |
|
72
|
23 |
|
$params[$key] = $this->options[$key]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
32 |
|
return $params; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
29 |
|
public function createConfigFile($params) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
29 |
|
$params = Parameters::wrap( |
|
82
|
29 |
|
$params, ['port', 'file', 'host', 'dbname', 'user', 'password'] |
|
83
|
|
|
); |
|
84
|
29 |
|
mkdir($this->migrations->getPath('')); |
|
85
|
29 |
|
mkdir($this->migrations->getPath('config')); |
|
86
|
29 |
|
mkdir($this->migrations->getPath('migrations')); |
|
87
|
|
|
|
|
88
|
29 |
|
$configFile = new \yentu\CodeWriter(); |
|
89
|
29 |
|
$configFile->add('return ['); |
|
90
|
29 |
|
$configFile->addIndent(); |
|
91
|
29 |
|
$configFile->add("'db' => ["); |
|
92
|
29 |
|
$configFile->addIndent(); |
|
93
|
29 |
|
$configFile->add("'driver' => '{$params['driver']}',"); |
|
94
|
29 |
|
$configFile->add("'host' => '{$params['host']}',"); |
|
95
|
29 |
|
$configFile->add("'port' => '{$params['port']}',"); |
|
96
|
29 |
|
$configFile->add("'dbname' => '{$params['dbname']}',"); |
|
97
|
29 |
|
$configFile->add("'user' => '{$params['user']}',"); |
|
98
|
29 |
|
$configFile->add("'password' => '{$params['password']}',"); |
|
99
|
29 |
|
$configFile->add("'file' => '{$params['file']}',"); |
|
100
|
29 |
|
$configFile->decreaseIndent(); |
|
101
|
29 |
|
$configFile->add(']'); |
|
102
|
29 |
|
$configFile->decreaseIndent(); |
|
103
|
29 |
|
$configFile->add('];'); |
|
104
|
|
|
|
|
105
|
29 |
|
file_put_contents($this->migrations->getPath("config/default.conf.php"), $configFile); |
|
106
|
29 |
|
return $params; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws CommandException |
|
111
|
|
|
*/ |
|
112
|
38 |
|
public function run() |
|
113
|
|
|
{ |
|
114
|
38 |
|
$home = $this->migrations->getPath(''); |
|
115
|
38 |
|
if (file_exists($home)) { |
|
116
|
3 |
|
throw new CommandException("Could not initialize yentu. Your project has already been initialized with yentu."); |
|
117
|
35 |
|
} else if (!is_writable(dirname($home))) { |
|
118
|
3 |
|
throw new CommandException("Your home directory ($home) could not be created."); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
32 |
|
$params = $this->getParams(); |
|
122
|
|
|
|
|
123
|
32 |
|
if (count($params) == 0 && defined('STDOUT')) { |
|
124
|
6 |
|
throw new CommandException( |
|
125
|
|
|
"You didn't provide any parameters for initialization. Please execute yentu " |
|
126
|
|
|
. "with `init -i` to initialize yentu interractively. " |
|
127
|
6 |
|
. "You can also try `init --help` for more information." |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
26 |
|
$config = $this->createConfigFile($params); |
|
132
|
26 |
|
$db = $this->manipulatorFactory->createManipulatorWithConfig($config); |
|
133
|
|
|
|
|
134
|
26 |
|
if ($db->getAssertor()->doesTableExist('yentu_history')) { |
|
135
|
|
|
throw new CommandException("Could not initialize yentu. Your database has already been initialized with yentu."); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
26 |
|
$db->createHistory(); |
|
139
|
26 |
|
$db->disconnect(); |
|
140
|
|
|
|
|
141
|
26 |
|
$this->io->output("Yentu successfully initialized.\n"); |
|
142
|
26 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function reverseActions() |
|
145
|
|
|
{ |
|
146
|
|
|
unlink($this->migrations->getPath("config/default.conf.php")); |
|
147
|
|
|
rmdir($this->migrations->getPath("config")); |
|
148
|
|
|
rmdir($this->migrations->getPath("migrations")); |
|
149
|
|
|
rmdir($this->migrations->getPath("")); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.