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