Serve::execute()   B
last analyzed

Complexity

Conditions 11
Paths 16

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 44
ccs 0
cts 17
cp 0
crap 132
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Server;
13
14
use BlitzPHP\Cli\Console\Command;
15
16
/**
17
 * Lancer le serveur de développement PHP
18
 *
19
 * Non testable, car il lance phpunit pour une boucle :-/
20
 *
21
 * @codeCoverageIgnore
22
 */
23
class Serve extends Command
24
{
25
    /**
26
     * @var string Groupe
27
     */
28
    protected $group = 'BlitzPHP';
29
30
    /**
31
     * @var string Nom
32
     */
33
    protected $name = 'serve';
34
35
    /**
36
     * @var string Description
37
     */
38
    protected $description = 'Lance le serveur de développement BlitzPHP.';
39
40
    /**
41
     * @var string Usage
42
     */
43
    protected $usage = 'php klinge serve';
44
45
    /**
46
     * @var string
47
     */
48
    protected $service = 'Service de lancement du serveur de developpement';
49
50
    /**
51
     * @var array Options
52
     */
53
    protected $options = [
54
        '--php'  => ['Le binaire PHP [défaut: "PHP_BINARY"]', PHP_BINARY],
55
        '--host' => ['L\'hôte HTTP [défaut: "localhost"]', 'localhost'],
56
        '--port' => ['Le port de l\'hôte HTTP [défaut: "3300"]', 3300],
57
    ];
58
59
    /**
60
     * Le décalage de port actuel.
61
     *
62
     * @var int
63
     */
64
    protected $portOffset = 0;
65
66
    /**
67
     * Le nombre maximum de ports à partir desquels tenter de servir
68
     *
69
     * @var int
70
     */
71
    protected $tries = 10;
72
73
    /**
74
     * Chemin de base dans lequel sera lancer le server
75
     *
76
     * @var string
77
     */
78
    protected $rootDirectory = WEBROOT;
0 ignored issues
show
Bug introduced by
The constant BlitzPHP\Cli\Commands\Server\WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
80
    /**
81
     * Liste des messages des taches
82
     *
83
     * @var array
84
     */
85
    protected $taskMessages = [
86
        'demarrage' => '', // Message a afficher lors du demarrage du serveur
87
        'demarrer'  => '', // Message a afficher lorsque le serveur a demarré
88
    ];
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function execute(array $params)
94
    {
95
        $options = [
96
            'php'  => PHP_BINARY,
97
            'host' => 'localhost',
98
            'port' => 3300,
99
        ];
100
101
        if (isset($this->options['--php'])) {
102
            $options['php'] = $this->options['--php'][1] ?? PHP_BINARY;
103
        }
104
        if (isset($this->options['--host'])) {
105
            $options['host'] = $this->options['--host'][1] ?? 'localhost';
106
        }
107
        if (isset($this->options['--port'])) {
108
            $options['port'] = $this->options['--port'][1] ?? 3300;
109
        }
110
111
        $php  = escapeshellarg($params['php'] ?: $options['php']);
112
        $host = $params['host'] ?: $options['host'];
113
        $port = (int) ($params['port'] ?: $options['port']) + $this->portOffset;
114
115
        $this->task($this->taskMessages['demarrage'] ?: 'Demarrage du serveur de developpement');
116
        sleep(2);
117
118
        $this->io->ok($this->taskMessages['demarrer'] ?: 'Le serveur de développement BlitzPHP a démarré sur ');
119
        $this->writer->boldGreen('http://' . $host . ':' . $port, true);
120
        $this->write("Appuyez sur Control-C pour arrêter.\n", true);
121
122
        // Définissez le chemin d’accès du contrôleur frontal sur Racine du document.
123
        $docroot = escapeshellarg($this->rootDirectory);
124
125
        // Imitez la fonctionnalité mod_rewrite d’Apache avec les paramètres utilisateur.
126
        $rewrite = escapeshellarg(__DIR__ . '/rewrite.php');
127
128
        // Appelez le serveur Web intégré de PHP, en veillant à définir notre
129
        // chemin de base vers le dossier public et pour utiliser le fichier de réécriture
130
        // pour s'assurer que notre environnement est défini et qu'il simule le mod_rewrite de base.
131
        passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);
132
133
        if ($status && $this->portOffset < $this->tries) {
134
            $this->portOffset++;
135
136
            $this->execute($params);
137
        }
138
    }
139
}
140