Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (success)
86.67%
13 / 15
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConsoleController
86.67% covered (success)
86.67%
13 / 15
66.67% covered (warning)
66.67%
2 / 3
8.15
0.00% covered (danger)
0.00%
0 / 1
 serve
83.33% covered (success)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
6.17
 help
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 version
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(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 */
15namespace Pop\Kettle\Controller;
16
17use Pop\Console\Color;
18use Pop\Kettle\Exception;
19
20/**
21 * Console 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    3.0.0
29 */
30class ConsoleController extends AbstractController
31{
32
33    /**
34     * Serve command
35     *
36     * @param  array $options
37     * @param  bool  $test
38     * @throws Exception
39     * @return void
40     */
41    public function serve(array $options = [], bool $test = false): void
42    {
43        if (!function_exists('exec')) {
44            throw new Exception("Error: The `exec()` function is not available. It is required to run PHP's web server.");
45        }
46
47        $host   = (isset($options['host']))   ? $options['host']   : 'localhost';
48        $port   = (isset($options['port']))   ? $options['port']   : 8000;
49        $folder = (isset($options['folder'])) ? $options['folder'] : 'public';
50
51        $this->console->write(
52            'PHP web server running on the folder ' . $this->console->colorize($folder, Color::BOLD_YELLOW) .' at ' .
53            $this->console->colorize($host . ':' . $port, Color::BOLD_GREEN) . '... (Ctrl-C to stop)'
54        );
55        $this->console->write();
56
57        if (!$test) {
58            exec('php -S ' . $host . ':' . $port . ' -t ' . $folder);
59        }
60    }
61
62    /**
63     * Help command
64     *
65     * @param  array $options
66     * @return void
67     */
68    public function help(array $options = []): void
69    {
70        $raw = isset($options['raw']);
71        $this->console->help(null, $raw);
72    }
73
74    /**
75     * Version command
76     *
77     * @return void
78     */
79    public function version(): void
80    {
81        $this->console->write('Version: ' . $this->console->colorize(\Pop\Kettle\Application::VERSION, Color::BOLD_GREEN));
82    }
83
84}