• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13575371463

27 Feb 2025 08:34PM UTC coverage: 28.921% (+0.6%) from 28.348%
13575371463

push

github

IlyasDeckers
Refactoring/cleaning up/getting rid of redundant helper functions

0 of 68 new or added lines in 8 files covered. (0.0%)

9 existing lines in 5 files now uncovered.

544 of 1881 relevant lines covered (28.92%)

8.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Console/Commands/Server/StartCommand.php
1
<?php
2

3
namespace Ody\Core\Console\Commands\Server;
4

5
use Ody\Core\Console\Style;
6
use Ody\Core\Server\Dependencies;
7
use Ody\Core\Server\Http;
8
use Ody\Swoole\HotReload\Watcher;
9
use Ody\Swoole\ServerState;
10
use Swoole\Process;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18

19
#[AsCommand(
20
    name: 'server:start',
×
21
    description: 'start http server'
×
22
)]
×
23
class StartCommand extends Command
24
{
25
    private ServerState $serverState;
26
    private Style $io;
27

28
    protected function configure(): void
×
29
    {
30
        $this->addOption(
×
31
            'daemonize',
×
32
            'd',
×
33
            InputOption::VALUE_NONE,
×
34
            'The program works in the background'
×
35
        )->addOption(
×
36
            'watch',
×
37
            'w',
×
38
            InputOption::VALUE_NONE,
×
39
            'If there is a change in the program code, it applies the changes instantly'
×
40
        )->addOption(
×
41
            'phpserver',
×
42
            'p',
×
43
            InputOption::VALUE_NONE,
×
44
            'Run on a build in php server'
×
45
        );
×
46
    }
47

48
    /**
49
     * @throws \Exception
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output): int
×
52
    {
53

NEW
54
        $serverState = ServerState::getInstance();
×
NEW
55
        $this->io = new Style($input, $output);
×
56

57
        if (!$this->canPhpServerRun($input) ||
×
58
            !$this->canDaemonRun($input) ||
×
59
            !$this->checkSslCertificate() ||
×
60
            !Dependencies::check($this->io)
×
61
        ) {
62
            return Command::FAILURE;
×
63
        }
64

NEW
65
        if ($serverState->websocketServerIsRunning()) {
×
66
            $this->handleRunningServer($input, $output);
×
67
        }
68

69
        /*
70
         * delete old routes closure cache files
71
         * TODO: implement routes cache
72
         */
73
//        if (file_exists(storagePath('routes'))) {
74
//            $dir = storagePath('routes');
75
//            foreach (array_diff(scandir($dir), ['.', '..']) as $file) {
76
//                unlink("$dir/$file");
77
//            }
78
//        } else {
79
//            mkdir(storagePath('routes'));
80
//        }
81

82
        /*
83
         * create listen message
84
         */
85
        $protocol = !is_null(config('server.ssl.ssl_cert_file')) && !is_null(config('server.ssl.ssl_cert_file')) ? 'https' : 'http';
×
86
        $listenMessage = "listen on $protocol://" . config('server.host') . ':' . config('server.port');
×
87

88
        /*
89
         * send running server
90
         * send listen message
91
         */
92
        $this->io->success('http server running…');
×
93
        $this->io->info($listenMessage, true);
×
94

95
        /*
96
         * check if exist daemonize not send general information
97
         */
98
        if (!$input->getOption('daemonize') && !$input->getOption('phpserver')) {
×
99
            /*
100
             * create socket type of server
101
             */
102
            $serverSocketType = match (config('server.sock_type')) {
×
103
                SWOOLE_SOCK_TCP => 'TCP',
×
104
                SWOOLE_SOCK_UDP => 'UDP',
×
105
                default => 'other type'
×
106
            };
×
107

108
            /*
109
             * create general information table
110
             */
111
            $table = new Table($output);
×
112
            $table
×
113
                ->setHeaderTitle('general information')
×
114
                ->setHeaders([
×
115
                    '<fg=#FFCB8B;options=bold> PHP VERSION </>',
×
116
                    '<fg=#FFCB8B;options=bold> ODY VERSION </>',
×
117
                    '<fg=#FFCB8B;options=bold> WORKER COUNT </>',
×
118
                    '<fg=#FFCB8B;options=bold> SOCKET TYPE </>',
×
119
                    '<fg=#FFCB8B;options=bold> WATCH MODE </>'
×
120
                ])
×
121
                ->setRows([
×
122
                    [
×
123
                        '<options=bold> ' . PHP_VERSION . '</>',
×
124
                        '<options=bold> ' . ODY_VERSION . ' </>',
×
125
                        '<options=bold> ' . config('server.additional.worker_num') . '</>',
×
126
                        "<options=bold> $serverSocketType</>",
×
127
                        $input->getOption('watch') ? '<fg=#C3E88D;options=bold> ACTIVE </>' : "<fg=#FF5572;options=bold> DEACTIVE </>"
×
128
                    ],
×
129
                ]);
×
130
            $table->setHorizontal();
×
131
            $table->render();
×
132

133
            /*
134
             * send info message for stop server
135
             */
136
            $this->io->info('Press Ctrl+C to stop the server');
×
137

138
            /*
139
             * create watcher server
140
             */
141
            if ($input->getOption('watch')) {
×
NEW
142
                (new Process(function (Process $process) use ($serverState) {
×
NEW
143
                    $serverState->setWatcherProcessId($process->pid);
×
144
                    (new Watcher())->start();
×
145
                }))->start();
×
146
            }
147
        }
148

149
        /*
150
         * create and start server
151
         */
152
        (new Http($input->getOption('phpserver')))
×
153
            ->init($input->getOption('daemonize'));
×
154

155
        return Command::SUCCESS;
×
156
    }
157

158
    private function handleRunningServer(InputInterface $input, OutputInterface $output): void
×
159
    {
NEW
160
        $serverState = ServerState::getInstance();
×
UNCOV
161
        $this->io->error('failed to listen server port[' . config('server.host') . ':' . config('server.port') . '], Error: Address already', true);
×
162

163
        $helper = $this->getHelper('question');
×
164
        $question = new ChoiceQuestion(
×
165
            'Do you want the server to terminate? (defaults to no)',
×
166
            ['no', 'yes'],
×
167
            0
×
168
        );
×
169
        $question->setErrorMessage('Your selection is invalid.');
×
170

171
        $answer = $helper->ask($input, $output, $question);
×
172

173

174
        if ($answer != 'yes') {
×
175
            return;
×
176
        }
177

NEW
178
        posix_kill($serverState->getMasterProcessId(), SIGTERM);
×
NEW
179
        posix_kill($serverState->getManagerProcessId(), SIGTERM);
×
180

NEW
181
        $watcherProcessId = $serverState->getWatcherProcessId();
×
182
        if (!is_null($watcherProcessId) && posix_kill($watcherProcessId, SIG_DFL)) {
×
183
            posix_kill($watcherProcessId, SIGTERM);
×
184
        }
185

NEW
186
        foreach ($serverState->getWorkerProcessIds() as $processId) {
×
187
            posix_kill($processId, SIGTERM);
×
188
        }
189

190
        sleep(1);
×
191
    }
192

193
    private function canDaemonRun(InputInterface $input): bool
×
194
    {
195
        if ($input->getOption('daemonize') && $input->getOption('watch')) {
×
196
            $this->io->error('Cannot use watcher in daemonize mode', true);
×
197
            
198
            return false;
×
199
        }
200
        
201
        return true;
×
202
    }
203

204
    private function canPhpServerRun(InputInterface $input): bool
×
205
    {
206
        if ($input->getOption('daemonize') && $input->getOption('phpserver')) {
×
207
            $this->io->error('Cannot use th PHP server in daemonize mode', true);
×
208

209
            return false;
×
210
        }
211

212
        return true;
×
213
    }
214

215
    private function checkSslCertificate(): bool
×
216
    {
217
        if (!is_null(config('server.ssl.ssl_cert_file')) && !file_exists(config('server.ssl.ssl_cert_file'))) {
×
218
            $this->io->error("ssl certificate file is not found", true);
×
219
            return false;
×
220
        }
221

222
        if (!is_null(config('server.ssl.ssl_cert_file')) && !file_exists(config('server.ssl.ssl_cert_file'))) {
×
223
            $this->io->error("ssl key file is not found", true);
×
224
            return false;
×
225
        }
226

227
        return true;
×
228
    }
229
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc