diff --git a/composer.json b/composer.json index af3c9b4688..b8f8b8eebe 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,7 @@ "sort-packages": true }, "scripts": { - "php-cs-fixer": "php-cs-fixer fix src/ --rules=@PER-CS2.0 --dry-run --diff" + "php-cs-fixer": "php-cs-fixer fix src/ --rules=@PER-CS2.0 --dry-run --diff", + "test": "./vendor/bin/phpunit --colors=always" } } diff --git a/src/think/App.php b/src/think/App.php index f4dcc2f99b..a3e26f534a 100644 --- a/src/think/App.php +++ b/src/think/App.php @@ -45,79 +45,79 @@ class App extends Container * 应用调试模式 * @var bool */ - protected $appDebug = false; + protected bool $appDebug = false; /** * 公共环境变量标识 * @var string */ - protected $baseEnvName = ''; + protected string $baseEnvName = ''; /** * 环境变量标识 * @var string */ - protected $envName = ''; + protected string $envName = ''; /** * 应用开始时间 * @var float */ - protected $beginTime; + protected float $beginTime; /** * 应用内存初始占用 * @var integer */ - protected $beginMem; + protected int $beginMem; /** * 当前应用类库命名空间 * @var string */ - protected $namespace = 'app'; + protected string $namespace = 'app'; /** * 应用根目录 * @var string */ - protected $rootPath = ''; + protected string $rootPath = ''; /** * 框架目录 * @var string */ - protected $thinkPath = ''; + protected string $thinkPath = ''; /** * 应用目录 * @var string */ - protected $appPath = ''; + protected string $appPath = ''; /** * Runtime目录 * @var string */ - protected $runtimePath = ''; + protected string $runtimePath = ''; /** * 路由定义目录 * @var string */ - protected $routePath = ''; + protected string $routePath = ''; /** * 配置后缀 * @var string */ - protected $configExt = '.php'; + protected string $configExt = '.php'; /** * 应用初始化器 * @var array */ - protected $initializers = [ + protected array $initializers = [ Error::class, RegisterService::class, BootService::class, @@ -127,13 +127,13 @@ class App extends Container * 注册的系统服务 * @var array */ - protected $services = []; + protected array $services = []; /** * 初始化 * @var bool */ - protected $initialized = false; + protected bool $initialized = false; /** * 容器绑定标识 @@ -195,7 +195,7 @@ public function __construct(string $rootPath = '') * @param bool $force 强制重新注册 * @return Service|null */ - public function register(Service | string $service, bool $force = false) + public function register(Service | string $service, bool $force = false): ?Service { $registered = $this->getService($service); @@ -216,19 +216,22 @@ public function register(Service | string $service, bool $force = false) } $this->services[] = $service; + + return null; } /** * 执行服务 * @access public * @param Service $service 服务 - * @return mixed */ - public function bootService(Service $service) + public function bootService(Service $service): ?Service { if (method_exists($service, 'boot')) { return $this->invoke([$service, 'boot']); } + + return null; } /** @@ -250,7 +253,7 @@ public function getService(Service | string $service): ?Service * @param bool $debug 开启应用调试模式 * @return $this */ - public function debug(bool $debug = true) + public function debug(bool $debug = true): static { $this->appDebug = $debug; return $this; @@ -272,7 +275,7 @@ public function isDebug(): bool * @param string $namespace 应用命名空间 * @return $this */ - public function setNamespace(string $namespace) + public function setNamespace(string $namespace): static { $this->namespace = $namespace; return $this; @@ -294,24 +297,45 @@ public function getNamespace(): string * @param string $name 环境标识 * @return $this */ - public function setBaseEnvName(string $name) + public function setBaseEnvName(string $name): static { $this->baseEnvName = $name; return $this; } + /** + * 获取公共环境变量标识 + * @access public + * @return string + */ + public function getBaseEnvName(): string + { + return $this->baseEnvName; + } + /** * 设置环境变量标识 * @access public * @param string $name 环境标识 * @return $this */ - public function setEnvName(string $name) + public function setEnvName(string $name): static { $this->envName = $name; return $this; } + /** + * 获取环境变量标识 + * @access public + * @return string + */ + public function getEnvName(): string + { + $envName = $this->env->get('env_name', ''); + return $this->envName ?? (string) $envName; + } + /** * 获取框架版本 * @access public @@ -356,7 +380,7 @@ public function getAppPath(): string * 设置应用目录 * @param string $path 应用目录 */ - public function setAppPath(string $path) + public function setAppPath(string $path): void { $this->appPath = $path; } @@ -420,6 +444,18 @@ public function getBeginTime(): float return $this->beginTime; } + /** + * 设置应用开启时间 + * @access public + * @param float $beginTime + * @return $this + */ + public function setBeginTime(float $beginTime): static + { + $this->beginTime = $beginTime; + return $this; + } + /** * 获取应用初始内存占用 * @access public @@ -430,6 +466,17 @@ public function getBeginMem(): int return $this->beginMem; } + /** + * 设置应用初始内存占用 + * @access public + * @return $this + */ + public function setBeginMem(int $beginMem): static + { + $this->beginMem = $beginMem; + return $this; + } + /** * 加载环境变量定义 * @access public @@ -451,20 +498,22 @@ public function loadEnv(string $envName = ''): void * @access public * @return $this */ - public function initialize() + public function initialize(): static { $this->initialized = true; - $this->beginTime = microtime(true); - $this->beginMem = memory_get_usage(); + // 设置应用开启时间 + $this->setBeginTime(microtime(true)) + ->setBeginMem(memory_get_usage()); // 加载环境变量 - if ($this->baseEnvName) { - $this->loadEnv($this->baseEnvName); + $baseEnvName = $this->getBaseEnvName(); + if ($baseEnvName) { + $this->loadEnv($baseEnvName); } - $this->envName = $this->envName ?: (string) $this->env->get('env_name', ''); - $this->loadEnv($this->envName); + $envName = $this->getEnvName(); + $this->loadEnv($envName); $this->configExt = $this->env->get('config_ext', '.php'); @@ -493,7 +542,7 @@ public function initialize() * 是否初始化过 * @return bool */ - public function initialized() + public function initialized(): bool { return $this->initialized; } @@ -569,7 +618,7 @@ protected function debugModeInit(): void { // 应用调试模式 if (!$this->appDebug) { - $this->appDebug = $this->env->get('app_debug') ? true : false; + $this->appDebug = (bool) $this->env->get('app_debug'); } if (!$this->appDebug) { diff --git a/src/think/Cookie.php b/src/think/Cookie.php index 1d024a584f..69113f68e0 100644 --- a/src/think/Cookie.php +++ b/src/think/Cookie.php @@ -111,6 +111,7 @@ public function set(string $name, string $value, $option = null): void } $this->setCookie($name, $value, $expire, $config); + $this->request->setCookie($name, $value); } /** @@ -158,6 +159,7 @@ public function delete(string $name, array $options = []): void { $config = array_merge($this->config, array_change_key_case($options)); $this->setCookie($name, '', time() - 3600, $config); + $this->request->setCookie($name, null); } /** diff --git a/src/think/Request.php b/src/think/Request.php index 2dc4502905..ef585ffcb3 100644 --- a/src/think/Request.php +++ b/src/think/Request.php @@ -2018,6 +2018,18 @@ public function withCookie(array $cookie) return $this; } + /** + * 更新COOKIE数据 + * @access public + * @param string $name cookie名 + * @param mixed $value 数据 + * @return void + */ + public function setCookie(string $name, mixed $value) + { + $this->cookie[$name] = $value; + } + /** * 设置SESSION数据 * @access public diff --git a/src/think/Service.php b/src/think/Service.php index 9d0559f23a..26fff53c57 100644 --- a/src/think/Service.php +++ b/src/think/Service.php @@ -8,7 +8,7 @@ // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- -declare(strict_types=1); +declare (strict_types = 1); namespace think; @@ -31,7 +31,7 @@ public function __construct(protected App $app) * @access protected * @param string $path 路由路径 */ - protected function loadRoutesFrom(string $path) + protected function loadRoutesFrom(string $path): void { $this->registerRoutes(function () use ($path) { include $path; @@ -42,7 +42,7 @@ protected function loadRoutesFrom(string $path) * 注册路由 * @param Closure $closure */ - protected function registerRoutes(Closure $closure) + protected function registerRoutes(Closure $closure): void { $this->app->event->listen(RouteLoaded::class, $closure); } @@ -52,7 +52,7 @@ protected function registerRoutes(Closure $closure) * @access protected * @param array|string $commands 指令 */ - protected function commands($commands) + protected function commands(array | string $commands): void { $commands = is_array($commands) ? $commands : func_get_args(); diff --git a/src/think/initializer/RegisterService.php b/src/think/initializer/RegisterService.php index 77c62b8669..6fe0371f38 100644 --- a/src/think/initializer/RegisterService.php +++ b/src/think/initializer/RegisterService.php @@ -8,7 +8,7 @@ // +---------------------------------------------------------------------- // | Author: yunwuxin <448901948@qq.com> // +---------------------------------------------------------------------- -declare(strict_types=1); +declare (strict_types = 1); namespace think\initializer; @@ -23,13 +23,13 @@ class RegisterService { - protected $services = [ + protected array $services = [ PaginatorService::class, ValidateService::class, ModelService::class, ]; - public function init(App $app) + public function init(App $app): void { $file = $app->getRootPath() . 'vendor/services.php'; diff --git a/src/think/route/dispatch/Callback.php b/src/think/route/dispatch/Callback.php index 26614ccbff..987d0bdab0 100644 --- a/src/think/route/dispatch/Callback.php +++ b/src/think/route/dispatch/Callback.php @@ -22,7 +22,7 @@ class Callback extends Dispatch public function exec() { // 执行回调方法 - $vars = array_merge($this->request->param(), $this->param); + $vars = array_merge($this->request->get(), $this->param); return $this->app->invoke($this->dispatch, $vars); } diff --git a/src/think/route/dispatch/Controller.php b/src/think/route/dispatch/Controller.php index c36e521a87..9417a60916 100644 --- a/src/think/route/dispatch/Controller.php +++ b/src/think/route/dispatch/Controller.php @@ -87,7 +87,7 @@ public function exec() $action = $this->actionName . $suffix; if (is_callable([$instance, $action])) { - $vars = $this->request->param(); + $vars = array_merge($this->request->get(), $this->param); try { $reflect = new ReflectionMethod($instance, $action); // 严格获取当前操作方法名 diff --git a/tests/AppTest.php b/tests/AppTest.php index 2f7307581b..5112946e58 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -16,7 +16,7 @@ class SomeService extends Service { - public $bind = [ + public array $bind = [ 'some' => 'class', ]; @@ -126,10 +126,10 @@ public function testPath() /** * @param vfsStreamDirectory $root - * @param bool $debug + * @param bool $debug * @return App */ - protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true) + protected function prepareAppForInitialize(vfsStreamDirectory $root, bool $debug = true): App { $rootPath = $root->url() . DIRECTORY_SEPARATOR; @@ -204,4 +204,25 @@ public function testParseClass() $this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class')); } + public function testBaseEnvName() + { + $name = 'test'; + $this->app->setBaseEnvName($name); + $this->assertEquals($name, $this->app->getBaseEnvName()); + } + + public function testEnvName() + { + $this->assertEquals('', $this->app->getEnvName()); + $name = 'test'; + $this->app->setEnvName($name); + $this->assertEquals($name, $this->app->getEnvName()); + } + + public function testBeginTime() + { + $name = microtime(true); + $this->app->setBeginTime($name); + $this->assertEquals($name, $this->app->getBeginTime()); + } }