Passed
Pull Request — 8.0 (#2920)
by wj
11:07
created

RunServer::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 6
rs 9.7333
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: Slince <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\console\command;
14
15
use think\console\Command;
16
use think\console\Input;
17
use think\console\input\Option;
18
use think\console\Output;
19
20
class RunServer extends Command
21
{
22
    public function configure()
23
    {
24
        $this->setName('run')
25
            ->addOption(
26
                'host',
27
                'H',
28
                Option::VALUE_OPTIONAL,
29
                'The host to server the application on',
30
                '0.0.0.0'
31
            )
32
            ->addOption(
33
                'port',
34
                'p',
35
                Option::VALUE_OPTIONAL,
36
                'The port to server the application on',
37
                8000
38
            )
39
            ->addOption(
40
                'root',
41
                'r',
42
                Option::VALUE_OPTIONAL,
43
                'The document root of the application',
44
                ''
45
            )
46
            ->setDescription('PHP Built-in Server for ThinkPHP');
47
    }
48
49
    public function execute(Input $input, Output $output)
50
    {
51
        $host = $input->getOption('host');
52
        $port = $input->getOption('port');
53
        $root = $input->getOption('root');
54
        if (empty($root)) {
55
            $root = $this->app->getRootPath() . 'public';
56
        }
57
58
        $command = sprintf(
59
            '%s -S %s:%d -t %s %s',
60
            $this->getPhpBinary(),
61
            $host,
0 ignored issues
show
Bug introduced by
It seems like $host can also be of type think\console\input\Option; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            /** @scrutinizer ignore-type */ $host,
Loading history...
62
            $port,
63
            escapeshellarg($root),
0 ignored issues
show
Bug introduced by
It seems like $root can also be of type think\console\input\Option; however, parameter $arg of escapeshellarg() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            escapeshellarg(/** @scrutinizer ignore-type */ $root),
Loading history...
64
            escapeshellarg($root . DIRECTORY_SEPARATOR . 'router.php')
0 ignored issues
show
Bug introduced by
Are you sure $root of type mixed|string|think\console\input\Option can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            escapeshellarg(/** @scrutinizer ignore-type */ $root . DIRECTORY_SEPARATOR . 'router.php')
Loading history...
65
        );
66
67
        $output->writeln(sprintf('ThinkPHP Development server is started On <http://%s:%s/>', $host, $port));
68
        $output->writeln(sprintf('You can exit with <info>`CTRL-C`</info>'));
69
        $output->writeln(sprintf('Document root is: %s', $root));
70
        passthru($command);
71
    }
72
73
    /**
74
     * 获取 PHP 路径
75
     * @return string
76
     */
77
    protected function getPhpBinary(): string
78
    {
79
        if (is_executable(PHP_BINARY)) {
80
            return PHP_BINARY;
81
        }
82
83
        $path = PHP_BINDIR . DIRECTORY_SEPARATOR . 'php';
84
        if (file_exists($path) && is_executable(PHP_BINARY)) {
85
            return $path;
86
        }
87
88
        return 'php';
89
    }
90
91
}
92