从bin/swoft开始,阅读Swoft框架源码(三)--EnvProcessor

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()方法获取配置信息.

你可能感兴趣的:(php,swoole,swoft)