PHP简单分布式锁-PHP服务器

2018-09-20  本文已影响0人  鹅鹅鹅的天歌

接之前的文章,首先我们要实现一个支持http的服务器。百度大法一下,就有了答案。传送门 .

接下来开始上代码

核心类

class Server
{
    const EXIT_CODE_NO_DOCUMENT_ROOT = 2;
    const EXIT_CODE_NO_ROUTING_FILE = 3;
    const EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_SERVER = 4;
    const EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS = 5;

    /**
     * @var int port to serve on.
     */
    public $port = 8081;

    public $docroot = '';

    /**
     * @var string path to router script.
     * See https://secure.php.net/manual/en/features.commandline.webserver.php
     */
    public $router;

    
    public function setRouter($router)
    {
        $this->router = $router;
    }


    /**
     * 设置文档跟目录 
     */
    public function setDocroot($root)
    {
        $this->docroot = $root;
    }


    public function streamSupportsAnsiColors($stream)
    {
        return DIRECTORY_SEPARATOR === '\\'
            ? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON'
            : function_exists('posix_isatty') && @posix_isatty($stream);
    }



    public function ansiFormat($string, $format = [])
    {
        $code = implode(';', $format);

        return "\033[0m" . ($code !== '' ? "\033[" . $code . 'm' : '') . $string . "\033[0m";
    }


    public function stdout($string)
    {
        if ($this->isColorEnabled()) {
            $args = func_get_args();
            array_shift($args);
            $string = $this->ansiFormat($string, $args);
        }
        return fwrite(\STDOUT, $string);
    }

    public function isColorEnabled($stream = \STDOUT)
    {
        return $this->color === null ? $this->streamSupportsAnsiColors($stream) : $this->color;
    }


    /**
     * Runs PHP built-in web server
     *
     * @param string $address address to serve on. Either "host" or "host:port".
     *
     * @return int
     */
    public function run($address = 'localhost')
    {
        $documentRoot = $this->docroot;

        if (strpos($address, ':') === false) {
            $address = $address . ':' . $this->port;
        }

        if (!is_dir($documentRoot)) {
            $this->stdout("Document root \"$documentRoot\" does not exist.\n", Console::FG_RED);
            return self::EXIT_CODE_NO_DOCUMENT_ROOT;
        }

        if ($this->isAddressTaken($address)) {
            $this->stdout("http://$address is taken by another process.\n", Console::FG_RED);
            return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS;
        }

        if ($this->router !== null && !file_exists($this->router)) {
            $this->stdout("Routing file \"$this->router\" does not exist.\n", Console::FG_RED);
            return self::EXIT_CODE_NO_ROUTING_FILE;
        }

        $this->stdout("Server started on http://{$address}/\n");
        $this->stdout("Document root is \"{$documentRoot}\"\n");
        if ($this->router) {
            $this->stdout("Routing file is \"$this->router\"\n");
        }
        $this->stdout("Quit the server with CTRL-C or COMMAND-C.\n");

        passthru('"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\" $this->router");
    }


    /**
     * @param string $address server address
     * @return bool if address is already in use
     */
    protected function isAddressTaken($address)
    {
        list($hostname, $port) = explode(':', $address);
        $fp = @fsockopen($hostname, $port, $errno, $errstr, 3);
        if ($fp === false) {
            return false;
        }
        fclose($fp);
        return true;
    }
}

使用方法

require 'Server.php';

$s = new Server();
$s->setDocroot('/project/zlh/testapi'); //设置文档根目录
$s->setRouter('router.php'); //设置路由文件
$s->run('localhost:9090'); //启动

控制台显示下面信息就成功了


image.png
上一篇 下一篇

猜你喜欢

热点阅读