Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.11% |
156 / 159 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ApplicationController | |
98.11% |
156 / 159 |
|
88.89% |
8 / 9 |
46 | |
0.00% |
0 / 1 |
| init | |
94.34% |
50 / 53 |
|
0.00% |
0 / 1 |
12.03 | |||
| env | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| status | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| down | |
100.00% |
52 / 52 |
|
100.00% |
1 / 1 |
14 | |||
| up | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| createCommand | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createController | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| createModel | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createView | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Event; |
| 19 | use Pop\Kettle\Model; |
| 20 | use Pop\App; |
| 21 | |
| 22 | /** |
| 23 | * Console application controller class |
| 24 | * |
| 25 | * @category Pop\Kettle |
| 26 | * @package Pop\Kettle |
| 27 | * @author Nick Sagona, III <nick@noladev.com> |
| 28 | * @copyright Copyright (c) 2012-2025 NOLA Interactive, LLC. |
| 29 | * @license http://www.popphp.org/license New BSD License |
| 30 | * @version 3.0.0 |
| 31 | */ |
| 32 | class ApplicationController extends AbstractController |
| 33 | { |
| 34 | |
| 35 | /** |
| 36 | * Init command |
| 37 | * |
| 38 | * @param ?string $namespace |
| 39 | * @param array $options |
| 40 | * @return void |
| 41 | */ |
| 42 | public function init(?string $namespace = null, array $options = []): void |
| 43 | { |
| 44 | if (empty($namespace)) { |
| 45 | $namespace = 'MyApp'; |
| 46 | } |
| 47 | |
| 48 | $web = (isset($options['web'])); |
| 49 | $api = (isset($options['api'])); |
| 50 | $cli = (isset($options['cli'])); |
| 51 | $envs = [ |
| 52 | 'local' => 1, |
| 53 | 'dev' => 2, |
| 54 | 'testing' => 3, |
| 55 | 'staging' => 4, |
| 56 | 'production' => 5, |
| 57 | ]; |
| 58 | |
| 59 | $name = $this->console->prompt('What is the name of your app? [' . $namespace . '] ', null, true); |
| 60 | if ($name == '') { |
| 61 | $name = $namespace; |
| 62 | } else if (str_contains($name, ' ') && !str_starts_with($name, '"') && !str_ends_with($name, '"')) { |
| 63 | $name = '"' . $name . '"'; |
| 64 | } |
| 65 | |
| 66 | $this->console->write(); |
| 67 | foreach ($envs as $env => $i) { |
| 68 | $this->console->write($i . ': ' . $env); |
| 69 | } |
| 70 | $this->console->write(); |
| 71 | |
| 72 | $e = $this->console->prompt('Please select an app environment from above: ', $envs); |
| 73 | $env = array_search($e, $envs); |
| 74 | $url = ''; |
| 75 | |
| 76 | if (($web) || ($api)) { |
| 77 | $url = $this->console->prompt('What is the URL of your app? [http://localhost] ', null, true); |
| 78 | if ($url == '') { |
| 79 | $url = 'http://localhost'; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $cliApp = false; |
| 84 | if ($cli) { |
| 85 | $this->console->write(); |
| 86 | $createCliApp = $this->console->prompt( |
| 87 | 'Initialize a stand-alone CLI application? [Y/N] ', ['y', 'n'] |
| 88 | ); |
| 89 | $this->console->write(); |
| 90 | $cliApp = (strtolower($createCliApp) == 'y'); |
| 91 | } |
| 92 | |
| 93 | $appModel = new Model\Application(); |
| 94 | $dbModel = new Model\Database(); |
| 95 | $location = getcwd(); |
| 96 | |
| 97 | $configDb = $this->console->prompt( |
| 98 | 'Would you like to configure a database? [Y/N] ', ['y', 'n'] |
| 99 | ); |
| 100 | $createDb = (strtolower($configDb) == 'y'); |
| 101 | |
| 102 | $appModel->init($location, $namespace, $web, $api, $cli, $name, $env, $url, $cliApp, $createDb); |
| 103 | |
| 104 | $this->console->write(); |
| 105 | $this->console->write("Installing files for '" . $namespace ."'..."); |
| 106 | $this->console->write(); |
| 107 | |
| 108 | if ($createDb) { |
| 109 | $this->console->write("Configuring database for '" . $namespace ."'..."); |
| 110 | $this->console->write(); |
| 111 | $dbModel->configure($this->console, $location); |
| 112 | $this->console->write(); |
| 113 | } |
| 114 | |
| 115 | $this->console->write('Done!'); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Env command |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | public function env(): void |
| 124 | { |
| 125 | if (App::isProduction()) { |
| 126 | $this->console->alertWarning('Application in Production', 40); |
| 127 | } else if (App::isStaging()) { |
| 128 | $this->console->alertPrimary('Application in Staging', 40); |
| 129 | } else if (App::isTesting()) { |
| 130 | $this->console->alertSecondary('Application in Testing', 40); |
| 131 | } else if (App::isDev()) { |
| 132 | $this->console->alertDark('Application in Dev', 40); |
| 133 | } else if (App::isLocal()) { |
| 134 | $this->console->alertLight('Application in Local', 40); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Status command |
| 140 | * |
| 141 | * @return void |
| 142 | */ |
| 143 | public function status(): void |
| 144 | { |
| 145 | if (App::isUp()) { |
| 146 | $this->console->alertSuccess('Application is Live', 40); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Down command |
| 152 | * |
| 153 | * @param array $options |
| 154 | * @return void |
| 155 | */ |
| 156 | public function down(array $options = []): void |
| 157 | { |
| 158 | $location = getcwd(); |
| 159 | $secret = null; |
| 160 | |
| 161 | if (array_key_exists('secret', $options)) { |
| 162 | $secret = (($options['secret'] === null) || ($options['secret'] == '')) ? sha1((string)time()) : $options['secret']; |
| 163 | } |
| 164 | |
| 165 | if (file_exists($location . '/.env') && (App::isUp())) { |
| 166 | $e = file_get_contents($location . '/.env'); |
| 167 | $e = str_replace( |
| 168 | [ |
| 169 | 'MAINTENANCE_MODE=false', |
| 170 | 'MAINTENANCE_MODE="false"', |
| 171 | 'MAINTENANCE_MODE=(false)', |
| 172 | ], |
| 173 | 'MAINTENANCE_MODE=true', |
| 174 | $e |
| 175 | ); |
| 176 | |
| 177 | $currentSecret = App::env('MAINTENANCE_MODE_SECRET'); |
| 178 | if (!empty($secret)) { |
| 179 | $e = str_replace( |
| 180 | [ |
| 181 | 'MAINTENANCE_MODE_SECRET=' . $currentSecret, |
| 182 | 'MAINTENANCE_MODE_SECRET="' . $currentSecret . '"', |
| 183 | ], |
| 184 | 'MAINTENANCE_MODE_SECRET=' . $secret, |
| 185 | $e |
| 186 | ); |
| 187 | } else if (!empty($currentSecret)) { |
| 188 | $secret = $currentSecret; |
| 189 | } |
| 190 | |
| 191 | file_put_contents($location . '/.env', $e); |
| 192 | |
| 193 | $this->console->alertInfo('Application in Maintenance', 40); |
| 194 | $this->console->write('Application has been switched to maintenance mode.'); |
| 195 | if (!empty($secret)) { |
| 196 | $this->console->write(''); |
| 197 | $this->console->write('The secret is ' . $this->console->colorize($secret, Color::BOLD_GREEN)); |
| 198 | } |
| 199 | } else if (file_exists($location . '/.env') && (App::isDown())) { |
| 200 | $this->console->write('Application is currently in maintenance mode. No action to take.'); |
| 201 | $currentSecret = App::env('MAINTENANCE_MODE_SECRET'); |
| 202 | if (!empty($secret)) { |
| 203 | $e = str_replace( |
| 204 | [ |
| 205 | 'MAINTENANCE_MODE_SECRET=' . $currentSecret, |
| 206 | 'MAINTENANCE_MODE_SECRET="' . $currentSecret . '"', |
| 207 | ], |
| 208 | 'MAINTENANCE_MODE_SECRET=' . $secret, |
| 209 | file_get_contents($location . '/.env') |
| 210 | ); |
| 211 | |
| 212 | file_put_contents($location . '/.env', $e); |
| 213 | } else if (!empty($currentSecret)) { |
| 214 | $secret = $currentSecret; |
| 215 | } |
| 216 | if (!empty($secret)) { |
| 217 | $this->console->write(''); |
| 218 | $this->console->write('The secret is ' . $this->console->colorize($secret, Color::BOLD_GREEN)); |
| 219 | } |
| 220 | } else { |
| 221 | $this->console->write('No .env file found.'); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Up command |
| 227 | * |
| 228 | * @return void |
| 229 | */ |
| 230 | public function up(): void |
| 231 | { |
| 232 | $location = getcwd(); |
| 233 | |
| 234 | if (file_exists($location . '/.env') && (App::isDown())) { |
| 235 | $e = str_replace( |
| 236 | [ |
| 237 | 'MAINTENANCE_MODE=true', |
| 238 | 'MAINTENANCE_MODE="true"', |
| 239 | 'MAINTENANCE_MODE=(true)', |
| 240 | ], |
| 241 | 'MAINTENANCE_MODE=false', |
| 242 | file_get_contents($location . '/.env') |
| 243 | ); |
| 244 | |
| 245 | file_put_contents($location . '/.env', $e); |
| 246 | |
| 247 | $this->console->alertSuccess('Application is Live', 40); |
| 248 | $this->console->write('Application has been made live.'); |
| 249 | } else if (App::isUp()) { |
| 250 | $this->console->alertSuccess('Application is Live', 40); |
| 251 | $this->console->write('Application is currently live. No action to take.'); |
| 252 | } else { |
| 253 | $this->console->write('No .env file found.'); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Create custom command |
| 259 | * |
| 260 | * @param string $command |
| 261 | * @return void |
| 262 | */ |
| 263 | public function createCommand(string $command, array $options = []): void |
| 264 | { |
| 265 | $appModel = new Model\Application(); |
| 266 | $appModel->createCommand($command, getcwd(), (isset($options['app']))); |
| 267 | |
| 268 | $this->console->write("Command '" . $command ."' created."); |
| 269 | |
| 270 | $this->console->write(); |
| 271 | $this->console->write('Done!'); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Create controller command |
| 276 | * |
| 277 | * @param string $ctrl |
| 278 | * @param array $options |
| 279 | * @return void |
| 280 | */ |
| 281 | public function createController(string $ctrl, array $options = []): void |
| 282 | { |
| 283 | $web = (isset($options['web'])) ? true : null; |
| 284 | $api = (isset($options['api'])) ? true : null; |
| 285 | $cli = (isset($options['cli'])) ? true : null; |
| 286 | |
| 287 | $appModel = new Model\Application(); |
| 288 | $ctrlClasses = $appModel->createController($ctrl, getcwd(), $web, $api, $cli); |
| 289 | |
| 290 | foreach ($ctrlClasses as $ctrlClass) { |
| 291 | $this->console->write("Controller class '" . $ctrlClass ."' created."); |
| 292 | } |
| 293 | |
| 294 | $this->console->write(); |
| 295 | $this->console->write('Done!'); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Create model command |
| 300 | * |
| 301 | * @param string $model |
| 302 | * @param array $options |
| 303 | * @return void |
| 304 | */ |
| 305 | public function createModel(string $model, array $options = []): void |
| 306 | { |
| 307 | $appModel = new Model\Application(); |
| 308 | $modelClass = $appModel->createModel($model, getcwd(), (isset($options['data']))); |
| 309 | |
| 310 | $this->console->write("Model class '" . $modelClass ."' created."); |
| 311 | $this->console->write(); |
| 312 | $this->console->write('Done!'); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Create view command |
| 317 | * |
| 318 | * @param string $view |
| 319 | * @return void |
| 320 | */ |
| 321 | public function createView(string $view): void |
| 322 | { |
| 323 | $appModel = new Model\Application(); |
| 324 | $viewFile = $appModel->createView($view, getcwd()); |
| 325 | |
| 326 | $this->console->write("View file '" . $viewFile ."' created."); |
| 327 | $this->console->write(); |
| 328 | $this->console->write('Done!'); |
| 329 | } |
| 330 | |
| 331 | } |