Swoft\Processor\EnvProcessor
是利用执行run后第一个调用的处理器.
handle办法代码:
public function handle(): bool
{
if (!$this->application->beforeEnv()) {
CLog::warning('Stop env processor by beforeEnv return false');
return false;
}
// 依据env文件别名获取env文件实在门路
$envFile = Swoft::getAlias($this->application->getEnvFile());
// Fix: In phar package, remove phar:// prefix
if (IN_PHAR) {
$envFile = Str::rmPharPrefix($envFile);
}
// 如果env文件不存在 则控制台打印正告信息
if (!file_exists($envFile)) {
CLog::warning('Env file(%s) is not exist! skip load it', $envFile);
return $this->application->afterEnv();
}
// 加载env信息
// 此处应用的是vlucas包
// vlucas官网镜像对此包的阐明如下:
// Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
// 翻译过去就是主动将.env文件中的内容加载到php的
// $_ENV和$_SERVER,应用getenv()函数能获取其中的内容
// Load env info
$factory = new DotenvFactory([
new EnvConstAdapter,
new PutenvAdapter,
new ServerConstAdapter
]);
$path = dirname($envFile);
$name = basename($envFile);
Dotenv::create($path, $name, $factory)->overload();
// 打印加载实现信息
CLog::info('Env file(%s) is loaded', $envFile);
return $this->application->afterEnv();
}
总结:
这个处理器干的事件很简略:
调用一个第三方包将.env文件中的配置加载到php中,使后续程序能通过getenv()办法获取配置信息.
发表回复