日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

Laravel php artisan optimize 源码解读

發(fā)布時(shí)間:2025/5/22 php 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel php artisan optimize 源码解读 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文:https://www.codecasts.com/blo...

在部署 Laravel 項(xiàng)目的時(shí)候,我們經(jīng)常會(huì)使用到一個(gè)提升性能的命令:

php artisan optimize

本文來看看這個(gè)命令執(zhí)行背后的源碼:

首先我們可以使用編輯器搜 OptimizeCommand,應(yīng)該就可以找到該命令源碼的所在:
Illuminate\Foundation\Console\OptimizeCommand,我們關(guān)注其中的 fire() 方法:

public function fire(){$this->info('Generating optimized class loader');if ($this->option('psr')) {$this->composer->dumpAutoloads();} else {$this->composer->dumpOptimized();}$this->call('clear-compiled');}

fire() 方法,默認(rèn)情況下,會(huì)執(zhí)行$this->composer->dumpOptimized(),而這行代碼觸發(fā)的其實(shí)就是composer dump-autoload --optimize,源代碼可以在Illuminate\Support\Composer 的 dumpOptimized() 找到:

public function dumpOptimized(){$this->dumpAutoloads('--optimize');}

最后,optimize 命令還執(zhí)行了call('clear-compiled'),其實(shí)就是觸發(fā)php artisan clear-compiled,而很巧的是,我們也是可以直接使用編輯器搜ClearCompiledCommand 來找到源碼,位于 Illuminate\Foundation\Console\ClearCompiledCommand 中,這里的 fire() 方法其實(shí)關(guān)鍵的一步就是刪除了一下 cache 下的文件,我們來看:

public function fire(){$servicesPath = $this->laravel->getCachedServicesPath();if (file_exists($servicesPath)) {@unlink($servicesPath);}$this->info('The compiled services file has been removed.');}

通過確定 $servicesPath 的位置,再使用 @unlink($servicesPath); 刪除。

確定 $servicesPath 的代碼 $this->laravel->getCachedServicesPath() 位于 Illuminate\Foundation\Application 的 getCachedServicesPath 中:

public function getCachedServicesPath(){return $this->bootstrapPath().'/cache/services.php';}

這樣一看,其實(shí)就是將 bootstrap/cache/services.php 文件刪除,而這個(gè) services.php 是 Laravel 會(huì)自動(dòng)生成的一個(gè)數(shù)組文件,這里指定了每個(gè) Providers 和 Facades 的位置和命名空間的全路徑等,在啟動(dòng) Laravel 項(xiàng)目的時(shí)候,可以直接讀取使用。

所以這個(gè)命令可以拆為兩步:

1.composer dump-autoload --optimize // composer 層面優(yōu)化加載速度 2.php artisan clear-compiled // 刪除 bootstrap/cache/services.php

很清晰。

總結(jié)

以上是生活随笔為你收集整理的Laravel php artisan optimize 源码解读的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。