ci4

Форк
0
119 строк · 2.7 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Commands\Server;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18

19
/**
20
 * Launch the PHP development server
21
 *
22
 * Not testable, as it throws phpunit for a loop :-/
23
 *
24
 * @codeCoverageIgnore
25
 */
26
class Serve extends BaseCommand
27
{
28
    /**
29
     * Group
30
     *
31
     * @var string
32
     */
33
    protected $group = 'CodeIgniter';
34

35
    /**
36
     * Name
37
     *
38
     * @var string
39
     */
40
    protected $name = 'serve';
41

42
    /**
43
     * Description
44
     *
45
     * @var string
46
     */
47
    protected $description = 'Launches the CodeIgniter PHP-Development Server.';
48

49
    /**
50
     * Usage
51
     *
52
     * @var string
53
     */
54
    protected $usage = 'serve';
55

56
    /**
57
     * Arguments
58
     *
59
     * @var array<string, string>
60
     */
61
    protected $arguments = [];
62

63
    /**
64
     * The current port offset.
65
     *
66
     * @var int
67
     */
68
    protected $portOffset = 0;
69

70
    /**
71
     * The max number of ports to attempt to serve from
72
     *
73
     * @var int
74
     */
75
    protected $tries = 10;
76

77
    /**
78
     * Options
79
     *
80
     * @var array<string, string>
81
     */
82
    protected $options = [
83
        '--php'  => 'The PHP Binary [default: "PHP_BINARY"]',
84
        '--host' => 'The HTTP Host [default: "localhost"]',
85
        '--port' => 'The HTTP Host Port [default: "8080"]',
86
    ];
87

88
    /**
89
     * Run the server
90
     */
91
    public function run(array $params)
92
    {
93
        // Collect any user-supplied options and apply them.
94
        $php  = escapeshellarg(CLI::getOption('php') ?? PHP_BINARY);
95
        $host = CLI::getOption('host') ?? 'localhost';
96
        $port = (int) (CLI::getOption('port') ?? 8080) + $this->portOffset;
97

98
        // Get the party started.
99
        CLI::write('CodeIgniter development server started on http://' . $host . ':' . $port, 'green');
100
        CLI::write('Press Control-C to stop.');
101

102
        // Set the Front Controller path as Document Root.
103
        $docroot = escapeshellarg(FCPATH);
104

105
        // Mimic Apache's mod_rewrite functionality with user settings.
106
        $rewrite = escapeshellarg(SYSTEMPATH . 'rewrite.php');
107

108
        // Call PHP's built-in webserver, making sure to set our
109
        // base path to the public folder, and to use the rewrite file
110
        // to ensure our environment is set and it simulates basic mod_rewrite.
111
        passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);
112

113
        if ($status && $this->portOffset < $this->tries) {
114
            $this->portOffset++;
115

116
            $this->run($params);
117
        }
118
    }
119
}
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.