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