Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.43% |
43 / 99 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseController | |
43.43% |
43 / 99 |
|
55.56% |
5 / 9 |
243.23 | |
0.00% |
0 / 1 |
| install | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| config | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| test | |
52.00% |
13 / 25 |
|
0.00% |
0 / 1 |
15.08 | |||
| createSeed | |
47.06% |
8 / 17 |
|
0.00% |
0 / 1 |
14.27 | |||
| seed | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| export | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| import | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| reset | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| clear | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (http://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/pop-bootstrap |
| 6 | * @author Nick Sagona, III <nick@noladev.com> |
| 7 | * @copyright Copyright (c) 2012-2025 NOLA Interactive, LLC. |
| 8 | * @license http://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Kettle\Controller; |
| 15 | |
| 16 | use Pop\Console\Color; |
| 17 | use Pop\Kettle\Model; |
| 18 | use Pop\Db\Sql\Seeder; |
| 19 | |
| 20 | /** |
| 21 | * Console database controller class |
| 22 | * |
| 23 | * @category Pop\Kettle |
| 24 | * @package Pop\Kettle |
| 25 | * @author Nick Sagona, III <nick@noladev.com> |
| 26 | * @copyright Copyright (c) 2012-2025 NOLA Interactive, LLC. |
| 27 | * @license http://www.popphp.org/license New BSD License |
| 28 | * @version 2.3.4 |
| 29 | */ |
| 30 | class DatabaseController extends AbstractController |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Install command |
| 35 | * |
| 36 | * @param ?string $database |
| 37 | * @return void |
| 38 | */ |
| 39 | public function install(?string $database = 'default'): void |
| 40 | { |
| 41 | if ($database === null) { |
| 42 | $database = 'default'; |
| 43 | } |
| 44 | |
| 45 | $location = getcwd(); |
| 46 | $dbModel = new Model\Database(); |
| 47 | $dbModel->configure($this->console, $location, $database) |
| 48 | ->seed($this->console, $location, $database); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Config command |
| 53 | * |
| 54 | * @param ?string $database |
| 55 | * @return void |
| 56 | */ |
| 57 | public function config(?string $database = 'default'): void |
| 58 | { |
| 59 | if ($database === null) { |
| 60 | $database = 'default'; |
| 61 | } |
| 62 | |
| 63 | $dbModel = new Model\Database(); |
| 64 | $dbModel->configure($this->console, getcwd(), $database); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test command |
| 69 | * |
| 70 | * @param ?string $database |
| 71 | * @return void |
| 72 | */ |
| 73 | public function test(?string $database = 'default'): void |
| 74 | { |
| 75 | $location = getcwd(); |
| 76 | $dbModel = new Model\Database(); |
| 77 | |
| 78 | if ($database === null) { |
| 79 | $databases = ['default']; |
| 80 | } else if ($database == 'all') { |
| 81 | $databases = array_filter(scandir($location . '/database/migrations'), function($value) { |
| 82 | return (($value != '.') && ($value != '..')); |
| 83 | }); |
| 84 | } else { |
| 85 | $databases = [$database]; |
| 86 | } |
| 87 | |
| 88 | if (!file_exists($location . '/app/config/database.php')) { |
| 89 | $this->console->write($this->console->colorize( |
| 90 | 'The database configuration was not found.', Color::BOLD_RED |
| 91 | )); |
| 92 | } else { |
| 93 | foreach ($databases as $db) { |
| 94 | $dbConfig = include $location . '/app/config/database.php'; |
| 95 | if (!isset($dbConfig[$db])) { |
| 96 | $this->console->write($this->console->colorize( |
| 97 | "The database configuration was not found for '" . $db . "'.", Color::BOLD_RED |
| 98 | )); |
| 99 | } else { |
| 100 | $result = $dbModel->test($dbConfig[$db]); |
| 101 | if ($result !== true) { |
| 102 | $this->console->write($this->console->colorize($result, Color::BOLD_RED)); |
| 103 | } else { |
| 104 | $this->console->write($this->console->colorize( |
| 105 | "Database configuration test for '" . $db . "' passed.", Color::BOLD_GREEN |
| 106 | )); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create seed command |
| 115 | * |
| 116 | * @param string $seed |
| 117 | * @param ?string $database |
| 118 | * @return void |
| 119 | */ |
| 120 | public function createSeed(string $seed, ?string $database = 'default'): void |
| 121 | { |
| 122 | $location = getcwd(); |
| 123 | |
| 124 | if ($database === null) { |
| 125 | $databases = ['default']; |
| 126 | } else if ($database == 'all') { |
| 127 | $databases = array_filter(scandir($location . '/database/migrations'), function($value) { |
| 128 | return (($value != '.') && ($value != '..')); |
| 129 | }); |
| 130 | } else { |
| 131 | $databases = [$database]; |
| 132 | } |
| 133 | |
| 134 | foreach ($databases as $db) { |
| 135 | if (!file_exists($location . '/database/seeds/' . $db)) { |
| 136 | mkdir($location . '/database/seeds/' . $db); |
| 137 | } |
| 138 | |
| 139 | if (str_ends_with(strtolower($seed), '.sql')) { |
| 140 | touch($location . '/database/seeds/' . $db .'/' . $seed); |
| 141 | file_put_contents($location . '/database/seeds/' . $db .'/' . $seed, "-- Seed data for '" . $seed . "'" . PHP_EOL . PHP_EOL); |
| 142 | $this->console->write("Database seed file '" . $seed . "' created for '" . $db . "'."); |
| 143 | } else { |
| 144 | Seeder::create($seed, $location . '/database/seeds/' . $db); |
| 145 | $this->console->write("Database seed class '" . $seed . "' created for '" . $db . "'."); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Seed command |
| 152 | * |
| 153 | * @param ?string $database |
| 154 | * @return void |
| 155 | */ |
| 156 | public function seed(?string $database = 'default'): void |
| 157 | { |
| 158 | if ($database === null) { |
| 159 | $database = 'default'; |
| 160 | } |
| 161 | |
| 162 | $dbModel = new Model\Database(); |
| 163 | $dbModel->seed($this->console, getcwd(), $database); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Export command |
| 168 | * |
| 169 | * @param ?string $database |
| 170 | * @return void |
| 171 | */ |
| 172 | public function export(?string $database = 'default'): void |
| 173 | { |
| 174 | if ($database === null) { |
| 175 | $database = 'default'; |
| 176 | } |
| 177 | |
| 178 | $disabled = explode(',', ini_get('disable_functions')); |
| 179 | |
| 180 | if (in_array('exec', $disabled)) { |
| 181 | $this->console->write($this->console->colorize( |
| 182 | "The 'exec' function is not enabled.", Color::BOLD_RED |
| 183 | )); |
| 184 | } else { |
| 185 | $output = null; |
| 186 | exec('which mysqldump', $output); |
| 187 | |
| 188 | if (empty($output)) { |
| 189 | $this->console->write($this->console->colorize( |
| 190 | "The 'mysqldump' program was not found.", Color::BOLD_RED |
| 191 | )); |
| 192 | } else { |
| 193 | $dbModel = new Model\Database(); |
| 194 | $dbModel->export($this->console, getcwd(), $database); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Import command |
| 201 | * |
| 202 | * @param string $importFile |
| 203 | * @param ?string $database |
| 204 | * @return void |
| 205 | */ |
| 206 | public function import(string $importFile, ?string $database = 'default'): void |
| 207 | { |
| 208 | if ($database === null) { |
| 209 | $database = 'default'; |
| 210 | } |
| 211 | |
| 212 | $location = getcwd(); |
| 213 | $disabled = explode(',', ini_get('disable_functions')); |
| 214 | |
| 215 | if (in_array('exec', $disabled)) { |
| 216 | $this->console->write($this->console->colorize( |
| 217 | "The 'exec' function is not enabled.", Color::BOLD_RED |
| 218 | )); |
| 219 | } else if (!file_exists($location . '/' . $importFile)) { |
| 220 | $this->console->write($this->console->colorize( |
| 221 | 'The database import file was not found.', Color::BOLD_RED |
| 222 | )); |
| 223 | } else { |
| 224 | $output = null; |
| 225 | exec('which mysql', $output); |
| 226 | |
| 227 | if (empty($output)) { |
| 228 | $this->console->write($this->console->colorize( |
| 229 | "The 'mysql' program was not found.", Color::BOLD_RED |
| 230 | )); |
| 231 | } else { |
| 232 | $dbModel = new Model\Database(); |
| 233 | $dbModel->import($this->console, getcwd(), $importFile, $database); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Reset command |
| 240 | * |
| 241 | * @param ?string $database |
| 242 | * @return void |
| 243 | */ |
| 244 | public function reset(?string $database = 'default'): void |
| 245 | { |
| 246 | if ($database === null) { |
| 247 | $database = 'default'; |
| 248 | } |
| 249 | |
| 250 | $dbModel = new Model\Database(); |
| 251 | $dbModel->reset($this->console, getcwd(), $database); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Clear command |
| 256 | * |
| 257 | * @param ?string $database |
| 258 | * @return void |
| 259 | */ |
| 260 | public function clear(?string $database = 'default'): void |
| 261 | { |
| 262 | if ($database === null) { |
| 263 | $database = 'default'; |
| 264 | } |
| 265 | |
| 266 | $dbModel = new Model\Database(); |
| 267 | $dbModel->clear($this->console, getcwd(), $database); |
| 268 | } |
| 269 | |
| 270 | } |