源碼解讀:php artisan serve

原文來自:https://www.codecasts.com/blo...php

在學習 Laravel 的時候,可能不少人接觸的第一個 artisan 的命令就是:php artisan serve,這樣咱們就能夠跑起第一個 Laravel 的應用。本文來嘗試解讀一下這個命令行的源碼。laravel

代碼在哪

跟以前的源碼解讀文章同樣,我推薦你們能夠使用編輯器的搜索功能,直接搜ServeCommand 就能夠直接打開這個命令的源碼文件,位於 Illuminate\Foundation\Console\ServeCommand,像其餘的 artisan 命令同樣,咱們仍是關注 fire() 方法:服務器

public function fire(){
        chdir($this->laravel->publicPath());

        $this->line("<info>Laravel development server started:</info> <http://{$this->host()}:{$this->port()}>");

        passthru($this->serverCommand());
}

第一步

首先使用 chdir() 將目錄改變至 public/ 目錄,這是根據 $this->laravel->publicPath() 代碼的 publicPath() 來的,這個方法的源碼位於 Illuminate\Foundation\Application 中:編輯器

public function publicPath()
    {
        return $this->basePath.DIRECTORY_SEPARATOR.'public';
    }

上面的 $this->basePath 就是項目的根目錄啦!函數

第二步

打印信息,這是經過 fire() 的第二行代碼實現的:post

$this->line("<info>Laravel development server started:</info> <http://{$this->host()}:{$this->port()}>");

具體效果就是這樣:
圖片描述學習

第三步

執行命令,這個命令是從 passthru($this->serverCommand()) 的原生函數 passthru() 來實現的,其中 $this->serverCommand() 負責返回一個能夠執行的字符串命令,具體代碼就在同文件的 serverCommand() 中:網站

protected function serverCommand()
    {
        return sprintf('%s -S %s:%s %s/server.php',
            ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false)),
            $this->host(),
            $this->port(),
            ProcessUtils::escapeArgument($this->laravel->basePath())
        );
    }

sprintf() 的四個字符串佔位符會被後面傳入的四個參數替換,最終打印出一個能夠執行的 PHP 命令,其中這四個分別對應的是:this

1.下面代碼返回 php 的可執行全路徑,spa

ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false))

2.下面代碼返回 host ,默認爲 127.0.0.1

$this->host()

這部分能夠直接查看 host() 方法:

protected function host()
    {
        return $this->input->getOption('host');
    }

這樣看來,咱們在使用 serve 命令的時候實際上是能夠傳入 host 參數的。

3.下面代碼返回 port ,跟 host 是如出一轍的:

$this->port()

代碼就在 port() 方法中,(使用 serve 命令的時候也是能夠直接傳入端口號的!)

protected function port()
    {
        return $this->input->getOption('port');
    }

上面的 2 和 3 能夠直接這樣考證 : 執行php artisan help serve
圖片描述

4.下面的代碼返回執行代碼的全路徑:

ProcessUtils::escapeArgument($this->laravel->basePath())

因此,最後 serverCommand() 返回的是什麼呢?咱們能夠直接打印看看:

圖片描述圖片描述

這樣一看,php artisan serve 命令就是直接使用 php 執行 server.php 文件,這個文件就在項目的根目錄下,好比就是至關於咱們直接在命令行執行下面這個命令:

php -S 127.0.0.1:8000 server.php

在這基礎上,記得指定 public/ 爲網站根目錄。

最後

php artisan serve 命令背後其實也就是使用了 php 去直接執行文件,跟咱們在使用 php -S php -S 127.0.0.1:8000 PHP的內置服務器沒有太大的區別。

相關文章
相關標籤/搜索