时间:2023-05-01 19:48:01 | 来源:网站运营
时间:2023-05-01 19:48:01 来源:网站运营
ThinkPHP6源码分析之应用初始化:ThinkPHP6 源码分析之应用初始化public function __construct(string $rootPath = ''){ $this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR; $this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath(); $this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR; $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; if (is_file($this->appPath . 'provider.php')) { $this->bind(include $this->appPath . 'provider.php'); } static::setInstance($this); $this->instance('app', $this); $this->instance('think/Container', $this);}
● 从魔术的方法的参数 rootPath 来看,是支持自定义根目录路径的。public function initialize(){ $this->initialized = true; $this->beginTime = microtime(true); $this->beginMem = memory_get_usage(); // 加载环境变量 if (is_file($this->rootPath . '.env')) { $this->env->load($this->rootPath . '.env'); } $this->configExt = $this->env->get('config_ext', '.php'); $this->debugModeInit(); // 加载全局初始化文件 $this->load(); // 加载框架默认语言包 $langSet = $this->lang->defaultLangSet(); $this->lang->load($this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $langSet . '.php'); // 加载应用默认语言包 $this->loadLangPack($langSet); // 监听AppInit $this->event->trigger('AppInit'); date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai')); // 初始化 foreach ($this->initializers as $initializer) { $this->make($initializer)->init($this); } return $this;}
● 加载 .env 环境变量文件public function register($service, bool $force = false){ $registered = $this->getService($service); if ($registered && !$force) { return $registered; } if (is_string($service)) { $service = new $service($this); } if (method_exists($service, 'register')) { $service->register(); } if (property_exists($service, 'bind')) { $this->bind($service->bind); } $this->services[] = $service;}
● 服务是否注册过,如果需要强制重新注册foreach ($this->initializers as $initializer) { $this->make($initializer)->init($this);}
这三个服务分别是:think/initializer/BootServicethink/initializer/Errorthink/initializer/RegisterService
● Error 服务是用来处理框架异常和错误的public function init(App $app){ $file = $app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'services.php'; $services = $this->services; if (is_file($file)) { $services = array_merge($services, include $file); } foreach ($services as $service) { if (class_exists($service)) { $app->register($service); } }}
该方法就很奇怪了,和我想象的有点不一样。服务是直接从 runtime 目录下面获取的,而非在 config 目录下的 service.php 中。为什么会这样呢?由于 composer 的发展,TP 框架也可以提供包的自动发现的功能,这也证明了开发组在不断向社区靠拢。下面来看一下是如何实现的。"scripts": { "post-autoload-dump": [ "@php think service:discover", "@php think vendor:publish" ]}
从配置来看,框架一共提供了两个指令,service:discover 和 vendor:publish。具体实现这里就不说了,你只需要知道包的发现是由 service:discover 实现的。PaginatorService::class,ValidateService::class,ModelService::class,
最后再来看看 BootService,这个就很简单了。从命名来讲就不难看出,下面就是代码,正常的启动服务,但是这里要说明的是,服务类中必须实现了 boot 方法才会启动。public function init(App $app){ $app->boot();}
以上就是ThinkPHP6源码分析之应用初始化的详细内容关键词:分析