Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.67% |
13 / 15 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ConsoleController | |
86.67% |
13 / 15 |
|
66.67% |
2 / 3 |
8.15 | |
0.00% |
0 / 1 |
| serve | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
6.17 | |||
| help | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| version | |
100.00% |
1 / 1 |
|
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\Exception; |
| 18 | |
| 19 | /** |
| 20 | * Console controller class |
| 21 | * |
| 22 | * @category Pop\Kettle |
| 23 | * @package Pop\Kettle |
| 24 | * @author Nick Sagona, III <nick@noladev.com> |
| 25 | * @copyright Copyright (c) 2012-2025 NOLA Interactive, LLC. |
| 26 | * @license http://www.popphp.org/license New BSD License |
| 27 | * @version 2.3.4 |
| 28 | */ |
| 29 | class ConsoleController extends AbstractController |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Serve command |
| 34 | * |
| 35 | * @param array $options |
| 36 | * @param bool $test |
| 37 | * @throws Exception |
| 38 | * @return void |
| 39 | */ |
| 40 | public function serve(array $options = [], bool $test = false): void |
| 41 | { |
| 42 | if (!function_exists('exec')) { |
| 43 | throw new Exception("Error: The `exec()` function is not available. It is required to run PHP's web server."); |
| 44 | } |
| 45 | |
| 46 | $host = (isset($options['host'])) ? $options['host'] : 'localhost'; |
| 47 | $port = (isset($options['port'])) ? $options['port'] : 8000; |
| 48 | $folder = (isset($options['folder'])) ? $options['folder'] : 'public'; |
| 49 | |
| 50 | $this->console->write( |
| 51 | 'PHP web server running on the folder ' . $this->console->colorize($folder, Color::BOLD_YELLOW) .' at ' . |
| 52 | $this->console->colorize($host . ':' . $port, Color::BOLD_GREEN) . '... (Ctrl-C to stop)' |
| 53 | ); |
| 54 | $this->console->write(); |
| 55 | |
| 56 | if (!$test) { |
| 57 | exec('php -S ' . $host . ':' . $port . ' -t ' . $folder); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Help command |
| 63 | * |
| 64 | * @param array $options |
| 65 | * @return void |
| 66 | */ |
| 67 | public function help(array $options = []): void |
| 68 | { |
| 69 | $raw = isset($options['raw']); |
| 70 | $this->console->help(null, $raw); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Version command |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public function version(): void |
| 79 | { |
| 80 | $this->console->write('Version: ' . $this->console->colorize(\Pop\Kettle\Module::VERSION, Color::BOLD_GREEN)); |
| 81 | } |
| 82 | |
| 83 | } |