| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Core\Service;
- use SixShop\Core\Exception\ExceptionHandle;
- use SixShop\Core\Request;
- use think\event\HttpRun;
- use think\exception\Handle;
- use think\Service;
- class CoreService extends Service
- {
- public static string $extensionPath;
- public static array $extensionNameList = [];
- public function register(): void
- {
- $this->app->bind(Handle::class, ExceptionHandle::class);
- $this->app->bind('think\Request', Request::class);
- $this->app->bind('classLoader',
- require $this->app->getRootPath() . 'vendor/autoload.php');
- self::$extensionPath = $this->app->getRootPath() . 'extension' . DIRECTORY_SEPARATOR;
- $this->initExtensionList();
- }
- public function boot(): void
- {
- $this->app->make(AutoloadService::class)->init($this->app);
- $this->app->make(HookAttributeService::class)->init($this->app);
- $this->app->event->listen(HttpRun::class,function (){
- $this->registerRoutes($this->app->make(RegisterRouteService::class)->init($this->app));
- });
- $this->app->make(CommandService::class)->init($this->app, function ($commands) {
- $this->commands($commands);
- });
- }
- public function initExtensionList(): void
- {
- if (empty(self::$extensionNameList)) {
- $coreFile = $this->app->getRootPath() . 'runtime/module_name_list_core.php';
- if (file_exists($coreFile)) {
- self::$extensionNameList = require $coreFile;
- } else {
- $extensionInfoList = [];
- $extensionDirs = array_diff(scandir(self::extensionPath), ['.', '..']);
- foreach ($extensionDirs as $item) {
- if (!is_dir(self::extensionPath . $item)) {
- continue;
- }
- $infoFile = self::extensionPath . $item . '/info.php';
- if (is_file($infoFile)) {
- $info = require $infoFile;
- $info['weight'] = $info['weight'] ?? 10000;
- if ($info['is_core'] ?? false) {
- self::$extensionNameList[] = $info['id'];
- $extensionInfoList[] = $info;
- }
- }
- }
- usort($extensionInfoList, function ($a, $b) {
- return $a['weight'] <=> $b['weight'];
- });
- $header = '// This file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
- $content = '<?php ' . PHP_EOL . $header . "return " . var_export(array_column($extensionInfoList, 'id'), true) . ';';
- file_put_contents($coreFile, $content);
- }
- $normalFile = $this->app->getRootPath() . 'runtime/module_name_list_normal.php';
- if (file_exists($normalFile)) {
- self::$extensionNameList = array_merge(self::$extensionNameList, require $normalFile);
- }
- }
- }
- public function loadEnv(string $envName = ''): void
- {
- $home = getenv('HOME');
- $envFile = $envName ? $home . '/.env.' . $envName : $home . '/.env';
- if (is_file($envFile)) {
- $this->app->env->load($envFile);
- return;
- }
- $this->app->loadEnv($envName);
- }
- }
|