Laravel 5.1 SAE环境移植概述
本实例详细解析了 Laravel 5.1 在 SAE 环境中的开发实践,旨在为开发者提供参考。
Laravel 是一款简洁优雅的 PHP Web 开发框架,现已成功适配至 SAE 平台。
相较于 Laravel 4,版本 5.1 目录结构更清晰且功能增强。尽管官方未提供原生 SAE 支持,但通过定制移植版可实现本地与 SAE 环境的无缝切换。
鉴于 SAE 平台的特殊运行机制,需重点解决以下核心兼容性问题。
主要挑战包括:#1 putenv()函数被禁用;#2 模板编译路径限制;#3 缓存驱动适配;#4 日志处理机制变更;#5 Session 存储方式;#6 服务提供者缓存生成。
#1 putenv()函数禁用
Laravel 5.1 依赖 putenv() 动态加载环境变量以读取 .env 配置。因 SAE PHPRuntime 禁用了该函数,此处采取折中方案:注释掉 vendor/vlucas/phpdotenv/src/Dotenv.php 第 86 行左右的代码,并将所有配置项迁移至 config 文件夹下的对应文件中。虽解决了禁用问题,但实现略显粗糙,期待更优雅的替代方案。
#2 模板编译
由于 SAE 禁止本地文件写入,原生的模板编译机制失效。解决方案是利用 Wrapper 将编译后的视图文件写入 Storage 存储桶。此前尝试写入 KVDB 但因未知错误放弃。
需在 config\view.php 中进行如下配置调整:
$compiled = [
'paths' => [
realpath(base_path('resources/views')),
],
'compiled' => realpath(storage_path('framework/views')),
];
if(SAE){
$compiled['compiled'] = 'saestor://'.SAE_STORAGE.'/compiled';
}
return $compiled;
注意确保目标 Storage 中存在 compiled 文件夹。
#3 缓存类
Laravel 5.1 默认未包含 SAE 可用的 Memcache 驱动。解决方法是编写自定义服务提供者并在 app.php 中注册,同时在 config\cache.php 中关联,具体逻辑见项目源码。
#4 日志处理
这也是一个比较棘手的问题,由于Laravel 5.1的日志处理已经不是和4一样使用服务提供者,而且直接注入到启动器中,这就使得我们只能覆写原生ConfigureLogging启动类,而官方也没有给出如何覆写和在哪里覆写,所以我这边的解决方案是判断当前环境为SAE后直接重写Http内核中的一个启动器属性,核心代码:
namespace Illuminate\Cloud\SAE;
use App\Http\Kernel as DefaultKernel;
class Kernel extends DefaultKernel{
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Illuminate\Foundation\Bootstrap\LoadConfiguration',
'Illuminate\Cloud\SAE\Log\ConfigureLogging',
'Illuminate\Foundation\Bootstrap\HandleExceptions',
'Illuminate\Foundation\Bootstrap\RegisterFacades',
'Illuminate\Foundation\Bootstrap\RegisterProviders',
'Illuminate\Foundation\Bootstrap\BootProviders',
];
}
此外,还需对日志的部分底层实现进行重写。
class Writer extends IlluminateLogWriter {
protected function useSaeLog($level = 'debug'){
$level = $this->parseLevel($level);
$this->monolog->pushHandler($handler = new SaeLogHandler($level));
$handler->setFormatter($this->getDefaultFormatter());
}
public function useFiles($path, $level = 'debug'){
if (SAE) {
return $this->useSaeLog($level);
}
parent::useFiles($path, $level);
}
public function useDailyFiles($path, $days = 0, $level = 'debug'){
if (SAE) {
return $this->useSaeLog($level);
}
parent::useDailyFiles($path, $days, $level);
}
}
#5 Session类
Laravel 5.1 的 Session 同样面临本地写入限制。参考 Laravel 4 的移植经验,改用 Memcache 作为 Session 存储后端,具体实现可结合前述缓存部分的处理方式。
#6 服务提供者缓存
应用启动时,Laravel 会在 bootstrap/cache/services.json 生成服务提供者缓存以提升性能。为解决本地写入问题,同样采用 Storage 的 Wrapper 机制。上述问题解决后,基本完成适配。最后需修改 bootstrap\app.php 以实现本地与 SAE 环境的智能切换,即根据环境判断生成 SAE 专用应用实例并注入对应的 Http 内核。
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
define('SAE',true);
define('SAE_STORAGE', 'laravel');
if(SAE){
$app = new Illuminate\Cloud\SAE\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
Illuminate\Cloud\SAE\Kernel::class
);
}else{
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
}
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;
在 bootstrap\app.php 中定义 SAE 环境标识的原因在于:需要在此处注入特定的应用程序实例和 Http 实例,同时初始化 Storage 配置。
接着配置 config\app.php,根据环境判断结果动态注入相应的服务提供者。
if(SAE){
$removeProviders = [
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
];
for($i = 0; $i < count($app['providers']); $i++){
if (in_array($app['providers'][$i], $removeProviders)) {
unset($app['providers'][$i]);
}
}
$app['providers'] = array_merge($app['providers'],[
Illuminate\Cloud\SAE\Cache\SaeCacheServiceProvider::class,
Illuminate\Cloud\SAE\Session\SessionServiceProvider::class,
Illuminate\Cloud\SAE\Storage\StorageServiceProvider::class,
Illuminate\Cloud\SAE\Segment\SegmentServiceProvider::class,
]);
$app['aliases']['Storage'] = Illuminate\Cloud\SAE\Storage\Storage::class;
$app['aliases']['Segment'] = Illuminate\Cloud\SAE\Segment\Segment::class;
}
SAE 专有应用实例和 Http 实例与原生的主要区别在于处理本地写入限制。原生应用在启动时会生成路由、配置、服务提供者及模板编译文件以加速加载,而在 SAE 中需重写 Application 类中与路径相关的方法,将这些文件生成至 Storage。Http 专有内核则负责处理启动器中的日志类。具体代码详见项目仓库。
此外,提供一个适用于 SAE 的 rewrite 规则供参考。
handle:
- rewrite: if (path ~ "^/$") goto "public/index.php"
- rewrite: if(!is_dir() && !is_file() && path~"^(.*)$") goto "public/index.php/$1"
总结
得益于 Laravel 的高扩展性和 SAE 的便利性,整体移植过程较为顺利。但在 putenv() 禁用和日志处理方面,现有实现仍不够优雅,欢迎提出改进建议。其他 SAE 服务如分词、邮件、队列等可通过服务提供者自动加载,此处不再赘述。
项目 GitHub 地址: https://github.com/wh469012917/laravel5-on-sae
软件点击此处本站下载。
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。