<commit_before><?php
namespace Lawoole\Server\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Lawoole\Console\OutputStyle;
use Lawoole\Server\HttpServerSocketHandler;
use Lawoole\Server\ServerExceptionHandler;
use Lawoole\Server\ServerHandler;
use Lawoole\Server\TcpServerSocketHandler;
use Lawoole\Swoole\HttpServerSocket;
use Lawoole\Swoole\ServerSocket;
use Lawoole\Swoole\WebSocketServer;
use Lawoole\Swoole\WebSocketServerSocket;

class StartCommand extends Command
{
    /**
     * 命令名
     *
     * @var string
     */
    protected $signature = 'start {--d|daemon : Run the server in background. }';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'Start the Swoole server';

    /**
     * 执行命令
     */
    public function handle()
    {
        $config = $this->laravel->make('config')->get('server', []);

        $server = new WebSocketServer($config);

        $this->configureServer($server, $config);

        $this->saveRuntime($server);

        // 启动服务
        $server->serve();
    }

    /**
     * 配置服务
     *
     * @param \Lawoole\Swoole\Server $server
     * @param array $config
     */
    protected function configureServer($server, array $config)
    {
        // 共享一个统一的输出
        $this->laravel->singleton(OutputStyle::class, new OutputStyle($this->input, $this->output));

        $server->setExceptionHandler(
            $this->laravel->make(Arr::get($config, 'exception.handler', ServerExceptionHandler::class))
        );

        $server->setEventHandler(
            $this->laravel->make(Arr::get($config, 'handler', ServerHandler::class))
        );

        // 后台运行
        if ($this->option('daemon')) {
            $server->setOptions(['daemonize' => true]);
        }

        // 端口监听
        $listens = Arr::get($config, 'listens', []);
        foreach ($listens as $listen) {
            $this->addServerSocket($server, $listen);
        }
    }

    /**
     * 添加服务 Socket
     *
     * @param \Lawoole\Swoole\Server $server
     * @param array $config<commit_msg>更新对象共享方式<commit_after><?php
namespace Lawoole\Server\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Lawoole\Console\OutputStyle;
use Lawoole\Server\HttpServerSocketHandler;
use Lawoole\Server\ServerExceptionHandler;
use Lawoole\Server\ServerHandler;
use Lawoole\Server\TcpServerSocketHandler;
use Lawoole\Swoole\HttpServerSocket;
use Lawoole\Swoole\ServerSocket;
use Lawoole\Swoole\WebSocketServer;
use Lawoole\Swoole\WebSocketServerSocket;

class StartCommand extends Command
{
    /**
     * 命令名
     *
     * @var string
     */
    protected $signature = 'start {--d|daemon : Run the server in background. }';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'Start the Swoole server';

    /**
     * 执行命令
     */
    public function handle()
    {
        $config = $this->laravel->make('config')->get('server', []);

        $server = new WebSocketServer($config);

        $this->configureServer($server, $config);

        $this->saveRuntime($server);

        // 启动服务
        $server->serve();
    }

    /**
     * 配置服务
     *
     * @param \Lawoole\Swoole\Server $server
     * @param array $config
     */
    protected function configureServer($server, array $config)
    {
        // 共享一个统一的输出
        $this->laravel->instance(OutputStyle::class, new OutputStyle($this->input, $this->output));

        $server->setExceptionHandler(
            $this->laravel->make(Arr::get($config, 'exception.handler', ServerExceptionHandler::class))
        );

        $server->setEventHandler(
            $this->laravel->make(Arr::get($config, 'handler', ServerHandler::class))
        );

        // 后台运行
        if ($this->option('daemon')) {
            $server->setOptions(['daemonize' => true]);
        }

        // 端口监听
        $listens = Arr::get($config, 'listens', []);
        foreach ($listens as $listen) {
            $this->addServerSocket($server, $listen);
        }
    }

    /**
     * 添加服务 Socket
     *
     * @param \Lawoole\Swoole\Server $server
     * @param array $config
{'fraction_non_alphanumeric': 0.14386459802538787, 'fraction_numerical': 0.0, 'mean_word_length': 2.5606694560669454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7572209', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1394, 'n_words': 231}