Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*):完善App类单测,规范化App类编写 #3048

Open
wants to merge 3 commits into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"sort-packages": true
},
"scripts": {
"php-cs-fixer": "php-cs-fixer fix src/ [email protected] --dry-run --diff"
"php-cs-fixer": "php-cs-fixer fix src/ [email protected] --dry-run --diff",
"test": "./vendor/bin/phpunit --colors=always"
}
}
113 changes: 81 additions & 32 deletions src/think/App.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/think/App.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: visibility_required

Check warning on line 1 in src/think/App.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: blank_line_after_opening_tag

Check warning on line 1 in src/think/App.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: declare_equal_normalize
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
Expand Down Expand Up @@ -45,79 +45,79 @@
* 应用调试模式
* @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,
Expand All @@ -127,13 +127,13 @@
* 注册的系统服务
* @var array
*/
protected $services = [];
protected array $services = [];

/**
* 初始化
* @var bool
*/
protected $initialized = false;
protected bool $initialized = false;

/**
* 容器绑定标识
Expand Down Expand Up @@ -195,7 +195,7 @@
* @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);

Expand All @@ -216,19 +216,22 @@
}

$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;
}

/**
Expand All @@ -250,7 +253,7 @@
* @param bool $debug 开启应用调试模式
* @return $this
*/
public function debug(bool $debug = true)
public function debug(bool $debug = true): static
{
$this->appDebug = $debug;
return $this;
Expand All @@ -272,7 +275,7 @@
* @param string $namespace 应用命名空间
* @return $this
*/
public function setNamespace(string $namespace)
public function setNamespace(string $namespace): static
{
$this->namespace = $namespace;
return $this;
Expand All @@ -294,24 +297,45 @@
* @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
Expand Down Expand Up @@ -356,7 +380,7 @@
* 设置应用目录
* @param string $path 应用目录
*/
public function setAppPath(string $path)
public function setAppPath(string $path): void
{
$this->appPath = $path;
}
Expand Down Expand Up @@ -420,6 +444,18 @@
return $this->beginTime;
}

/**
* 设置应用开启时间
* @access public
* @param float $beginTime
* @return $this
*/
public function setBeginTime(float $beginTime): static
{
$this->beginTime = $beginTime;
return $this;
}

/**
* 获取应用初始内存占用
* @access public
Expand All @@ -430,6 +466,17 @@
return $this->beginMem;
}

/**
* 设置应用初始内存占用
* @access public
* @return $this
*/
public function setBeginMem(int $beginMem): static
{
$this->beginMem = $beginMem;
return $this;
}

/**
* 加载环境变量定义
* @access public
Expand All @@ -451,20 +498,22 @@
* @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');

Expand Down Expand Up @@ -493,7 +542,7 @@
* 是否初始化过
* @return bool
*/
public function initialized()
public function initialized(): bool
{
return $this->initialized;
}
Expand Down Expand Up @@ -569,7 +618,7 @@
{
// 应用调试模式
if (!$this->appDebug) {
$this->appDebug = $this->env->get('app_debug') ? true : false;
$this->appDebug = (bool) $this->env->get('app_debug');
}

if (!$this->appDebug) {
Expand Down
2 changes: 2 additions & 0 deletions src/think/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/think/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/think/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// +----------------------------------------------------------------------
// | Author: yunwuxin <[email protected]>
// +----------------------------------------------------------------------
declare(strict_types=1);
declare (strict_types = 1);

namespace think;

Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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();

Expand Down
Loading
Loading