取消vendor文件夹的排除
This commit is contained in:
1 parent
55d4d7b9c8
commit
1eafa8f5c9
1279 files changed
+277858
-1
No files matched your search
+610
@@ -0,0 +1,610 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think;
|
||||
|
||||
use think\event\AppInit;
|
||||
use think\helper\Str;
|
||||
use think\initializer\BootService;
|
||||
use think\initializer\Error;
|
||||
use think\initializer\RegisterService;
|
||||
|
||||
/**
|
||||
* App 基础类
|
||||
* @property Route $route
|
||||
* @property Config $config
|
||||
* @property Cache $cache
|
||||
* @property Request $request
|
||||
* @property Http $http
|
||||
* @property Console $console
|
||||
* @property Env $env
|
||||
* @property Event $event
|
||||
* @property Middleware $middleware
|
||||
* @property Log $log
|
||||
* @property Lang $lang
|
||||
* @property Db $db
|
||||
* @property Cookie $cookie
|
||||
* @property Session $session
|
||||
* @property Validate $validate
|
||||
* @property Filesystem $filesystem
|
||||
*/
|
||||
class App extends Container
|
||||
{
|
||||
const VERSION = '6.0.0';
|
||||
|
||||
/**
|
||||
* 应用调试模式
|
||||
* @var bool
|
||||
*/
|
||||
protected $appDebug = false;
|
||||
|
||||
/**
|
||||
* 应用开始时间
|
||||
* @var float
|
||||
*/
|
||||
protected $beginTime;
|
||||
|
||||
/**
|
||||
* 应用内存初始占用
|
||||
* @var integer
|
||||
*/
|
||||
protected $beginMem;
|
||||
|
||||
/**
|
||||
* 当前应用类库命名空间
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'app';
|
||||
|
||||
/**
|
||||
* 应用根目录
|
||||
* @var string
|
||||
*/
|
||||
protected $rootPath = '';
|
||||
|
||||
/**
|
||||
* 框架目录
|
||||
* @var string
|
||||
*/
|
||||
protected $thinkPath = '';
|
||||
|
||||
/**
|
||||
* 应用目录
|
||||
* @var string
|
||||
*/
|
||||
protected $appPath = '';
|
||||
|
||||
/**
|
||||
* Runtime目录
|
||||
* @var string
|
||||
*/
|
||||
protected $runtimePath = '';
|
||||
|
||||
/**
|
||||
* 路由定义目录
|
||||
* @var string
|
||||
*/
|
||||
protected $routePath = '';
|
||||
|
||||
/**
|
||||
* 配置后缀
|
||||
* @var string
|
||||
*/
|
||||
protected $configExt = '.php';
|
||||
|
||||
/**
|
||||
* 应用初始化器
|
||||
* @var array
|
||||
*/
|
||||
protected $initializers = [
|
||||
Error::class,
|
||||
RegisterService::class,
|
||||
BootService::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 注册的系统服务
|
||||
* @var array
|
||||
*/
|
||||
protected $services = [];
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @var bool
|
||||
*/
|
||||
protected $initialized = false;
|
||||
|
||||
/**
|
||||
* 容器绑定标识
|
||||
* @var array
|
||||
*/
|
||||
protected $bind = [
|
||||
'app' => App::class,
|
||||
'cache' => Cache::class,
|
||||
'config' => Config::class,
|
||||
'console' => Console::class,
|
||||
'cookie' => Cookie::class,
|
||||
'db' => Db::class,
|
||||
'env' => Env::class,
|
||||
'event' => Event::class,
|
||||
'http' => Http::class,
|
||||
'lang' => Lang::class,
|
||||
'log' => Log::class,
|
||||
'middleware' => Middleware::class,
|
||||
'request' => Request::class,
|
||||
'response' => Response::class,
|
||||
'route' => Route::class,
|
||||
'session' => Session::class,
|
||||
'validate' => Validate::class,
|
||||
'view' => View::class,
|
||||
'filesystem' => Filesystem::class,
|
||||
'think\DbManager' => Db::class,
|
||||
'think\LogManager' => Log::class,
|
||||
'think\CacheManager' => Cache::class,
|
||||
|
||||
// 接口依赖注入
|
||||
'Psr\Log\LoggerInterface' => Log::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构方法
|
||||
* @access public
|
||||
* @param string $rootPath 应用根目录
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册服务
|
||||
* @access public
|
||||
* @param Service|string $service 服务
|
||||
* @param bool $force 强制重新注册
|
||||
* @return Service|null
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行服务
|
||||
* @access public
|
||||
* @param Service $service 服务
|
||||
* @return mixed
|
||||
*/
|
||||
public function bootService($service)
|
||||
{
|
||||
if (method_exists($service, 'boot')) {
|
||||
return $this->invoke([$service, 'boot']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务
|
||||
* @param string|Service $service
|
||||
* @return Service|null
|
||||
*/
|
||||
public function getService($service)
|
||||
{
|
||||
$name = is_string($service) ? $service : get_class($service);
|
||||
return array_values(array_filter($this->services, function ($value) use ($name) {
|
||||
return $value instanceof $name;
|
||||
}, ARRAY_FILTER_USE_BOTH))[0] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启应用调试模式
|
||||
* @access public
|
||||
* @param bool $debug 开启应用调试模式
|
||||
* @return $this
|
||||
*/
|
||||
public function debug(bool $debug = true)
|
||||
{
|
||||
$this->appDebug = $debug;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为调试模式
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function isDebug(): bool
|
||||
{
|
||||
return $this->appDebug;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置应用命名空间
|
||||
* @access public
|
||||
* @param string $namespace 应用命名空间
|
||||
* @return $this
|
||||
*/
|
||||
public function setNamespace(string $namespace)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用类库命名空间
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getNamespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取框架版本
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function version(): string
|
||||
{
|
||||
return static::VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用根目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getRootPath(): string
|
||||
{
|
||||
return $this->rootPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用基础目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getBasePath(): string
|
||||
{
|
||||
return $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前应用目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getAppPath(): string
|
||||
{
|
||||
return $this->appPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置应用目录
|
||||
* @param string $path 应用目录
|
||||
*/
|
||||
public function setAppPath(string $path)
|
||||
{
|
||||
$this->appPath = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用运行时目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getRuntimePath(): string
|
||||
{
|
||||
return $this->runtimePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置runtime目录
|
||||
* @param string $path 定义目录
|
||||
*/
|
||||
public function setRuntimePath(string $path): void
|
||||
{
|
||||
$this->runtimePath = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取核心框架目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getThinkPath(): string
|
||||
{
|
||||
return $this->thinkPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用配置目录
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigPath(): string
|
||||
{
|
||||
return $this->rootPath . 'config' . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置后缀
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigExt(): string
|
||||
{
|
||||
return $this->configExt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用开启时间
|
||||
* @access public
|
||||
* @return float
|
||||
*/
|
||||
public function getBeginTime(): float
|
||||
{
|
||||
return $this->beginTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用初始内存占用
|
||||
* @access public
|
||||
* @return integer
|
||||
*/
|
||||
public function getBeginMem(): int
|
||||
{
|
||||
return $this->beginMem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化应用
|
||||
* @access public
|
||||
* @return $this
|
||||
*/
|
||||
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::class);
|
||||
|
||||
date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai'));
|
||||
|
||||
// 初始化
|
||||
foreach ($this->initializers as $initializer) {
|
||||
$this->make($initializer)->init($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否初始化过
|
||||
* @return bool
|
||||
*/
|
||||
public function initialized()
|
||||
{
|
||||
return $this->initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载语言包
|
||||
* @param string $langset 语言
|
||||
* @return void
|
||||
*/
|
||||
public function loadLangPack($langset)
|
||||
{
|
||||
if (empty($langset)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载系统语言包
|
||||
$files = glob($this->appPath . 'lang' . DIRECTORY_SEPARATOR . $langset . '.*');
|
||||
$this->lang->load($files);
|
||||
|
||||
// 加载扩展(自定义)语言包
|
||||
$list = $this->config->get('lang.extend_list', []);
|
||||
|
||||
if (isset($list[$langset])) {
|
||||
$this->lang->load($list[$langset]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 引导应用
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
array_walk($this->services, function ($service) {
|
||||
$this->bootService($service);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载应用文件和配置
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
protected function load(): void
|
||||
{
|
||||
$appPath = $this->getAppPath();
|
||||
|
||||
if (is_file($appPath . 'common.php')) {
|
||||
include_once $appPath . 'common.php';
|
||||
}
|
||||
|
||||
include_once $this->thinkPath . 'helper.php';
|
||||
|
||||
$configPath = $this->getConfigPath();
|
||||
|
||||
$files = [];
|
||||
|
||||
if (is_dir($configPath)) {
|
||||
$files = glob($configPath . '*' . $this->configExt);
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
$this->config->load($file, pathinfo($file, PATHINFO_FILENAME));
|
||||
}
|
||||
|
||||
if (is_file($appPath . 'event.php')) {
|
||||
$this->loadEvent(include $appPath . 'event.php');
|
||||
}
|
||||
|
||||
if (is_file($appPath . 'service.php')) {
|
||||
$services = include $appPath . 'service.php';
|
||||
foreach ($services as $service) {
|
||||
$this->register($service);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调试模式设置
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
protected function debugModeInit(): void
|
||||
{
|
||||
// 应用调试模式
|
||||
if (!$this->appDebug) {
|
||||
$this->appDebug = $this->env->get('app_debug') ? true : false;
|
||||
ini_set('display_errors', 'Off');
|
||||
}
|
||||
|
||||
if (!$this->runningInConsole()) {
|
||||
//重新申请一块比较大的buffer
|
||||
if (ob_get_level() > 0) {
|
||||
$output = ob_get_clean();
|
||||
}
|
||||
ob_start();
|
||||
if (!empty($output)) {
|
||||
echo $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册应用事件
|
||||
* @access protected
|
||||
* @param array $event 事件数据
|
||||
* @return void
|
||||
*/
|
||||
public function loadEvent(array $event): void
|
||||
{
|
||||
if (isset($event['bind'])) {
|
||||
$this->event->bind($event['bind']);
|
||||
}
|
||||
|
||||
if (isset($event['listen'])) {
|
||||
$this->event->listenEvents($event['listen']);
|
||||
}
|
||||
|
||||
if (isset($event['subscribe'])) {
|
||||
$this->event->subscribe($event['subscribe']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析应用类的类名
|
||||
* @access public
|
||||
* @param string $layer 层名 controller model ...
|
||||
* @param string $name 类名
|
||||
* @return string
|
||||
*/
|
||||
public function parseClass(string $layer, string $name): string
|
||||
{
|
||||
$name = str_replace(['/', '.'], '\\', $name);
|
||||
$array = explode('\\', $name);
|
||||
$class = Str::studly(array_pop($array));
|
||||
$path = $array ? implode('\\', $array) . '\\' : '';
|
||||
|
||||
return $this->namespace . '\\' . $layer . '\\' . $path . $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否运行在命令行下
|
||||
* @return bool
|
||||
*/
|
||||
public function runningInConsole()
|
||||
{
|
||||
return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用根目录
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
protected function getDefaultRootPath(): string
|
||||
{
|
||||
$path = dirname(dirname(dirname(dirname($this->thinkPath))));
|
||||
|
||||
return $path . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think;
|
||||
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use think\cache\Driver;
|
||||
use think\cache\TagSet;
|
||||
use think\exception\InvalidArgumentException;
|
||||
use think\helper\Arr;
|
||||
|
||||
/**
|
||||
* 缓存管理类
|
||||
* @mixin Driver
|
||||
* @mixin \think\cache\driver\File
|
||||
*/
|
||||
class Cache extends Manager implements CacheInterface
|
||||
{
|
||||
|
||||
protected $namespace = '\\think\\cache\\driver\\';
|
||||
|
||||
/**
|
||||
* 默认驱动
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDefaultDriver()
|
||||
{
|
||||
return $this->getConfig('default');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存配置
|
||||
* @access public
|
||||
* @param null|string $name 名称
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(string $name = null, $default = null)
|
||||
{
|
||||
if (!is_null($name)) {
|
||||
return $this->app->config->get('cache.' . $name, $default);
|
||||
}
|
||||
|
||||
return $this->app->config->get('cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取驱动配置
|
||||
* @param string $store
|
||||
* @param string $name
|
||||
* @param null $default
|
||||
* @return array
|
||||
*/
|
||||
public function getStoreConfig(string $store, string $name = null, $default = null)
|
||||
{
|
||||
if ($config = $this->getConfig("stores.{$store}")) {
|
||||
return Arr::get($config, $name, $default);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException("Store [$store] not found.");
|
||||
}
|
||||
|
||||
protected function resolveType(string $name)
|
||||
{
|
||||
return $this->getStoreConfig($name, 'type', 'file');
|
||||
}
|
||||
|
||||
protected function resolveConfig(string $name)
|
||||
{
|
||||
return $this->getStoreConfig($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接或者切换缓存
|
||||
* @access public
|
||||
* @param string $name 连接配置名
|
||||
* @return Driver
|
||||
*/
|
||||
public function store(string $name = null)
|
||||
{
|
||||
return $this->driver($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓冲池
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
return $this->store()->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $key 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
return $this->store()->get($key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $key 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int|\DateTime $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function set($key, $value, $ttl = null): bool
|
||||
{
|
||||
return $this->store()->set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $key 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($key): bool
|
||||
{
|
||||
return $this->store()->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return iterable
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getMultiple($keys, $default = null): iterable
|
||||
{
|
||||
return $this->store()->getMultiple($keys, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param iterable $values 缓存数据
|
||||
* @param null|int|\DateInterval $ttl 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function setMultiple($values, $ttl = null): bool
|
||||
{
|
||||
return $this->store()->setMultiple($values, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param iterable $keys 缓存变量名
|
||||
* @return bool
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function deleteMultiple($keys): bool
|
||||
{
|
||||
return $this->store()->deleteMultiple($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $key 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key): bool
|
||||
{
|
||||
return $this->store()->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存标签
|
||||
* @access public
|
||||
* @param string|array $name 标签名
|
||||
* @return TagSet
|
||||
*/
|
||||
public function tag($name): TagSet
|
||||
{
|
||||
return $this->store()->tag($name);
|
||||
}
|
||||
}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace think;
|
||||
|
||||
/**
|
||||
* 配置管理类
|
||||
* @package think
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* 配置文件目录
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* 配置文件后缀
|
||||
* @var string
|
||||
*/
|
||||
protected $ext;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(string $path = null, string $ext = '.php')
|
||||
{
|
||||
$this->path = $path ?: '';
|
||||
$this->ext = $ext;
|
||||
}
|
||||
|
||||
public static function __make(App $app)
|
||||
{
|
||||
$path = $app->getConfigPath();
|
||||
$ext = $app->getConfigExt();
|
||||
|
||||
return new static($path, $ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载配置文件(多种格式)
|
||||
* @access public
|
||||
* @param string $file 配置文件名
|
||||
* @param string $name 一级配置名
|
||||
* @return array
|
||||
*/
|
||||
public function load(string $file, string $name = ''): array
|
||||
{
|
||||
if (is_file($file)) {
|
||||
$filename = $file;
|
||||
} elseif (is_file($this->path . $file . $this->ext)) {
|
||||
$filename = $this->path . $file . $this->ext;
|
||||
}
|
||||
|
||||
if (isset($filename)) {
|
||||
return $this->parse($filename, $name);
|
||||
}
|
||||
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析配置文件
|
||||
* @access public
|
||||
* @param string $file 配置文件名
|
||||
* @param string $name 一级配置名
|
||||
* @return array
|
||||
*/
|
||||
protected function parse(string $file, string $name): array
|
||||
{
|
||||
$type = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$config = [];
|
||||
switch ($type) {
|
||||
case 'php':
|
||||
$config = include $file;
|
||||
break;
|
||||
case 'yml':
|
||||
case 'yaml':
|
||||
if (function_exists('yaml_parse_file')) {
|
||||
$config = yaml_parse_file($file);
|
||||
}
|
||||
break;
|
||||
case 'ini':
|
||||
$config = parse_ini_file($file, true, INI_SCANNER_TYPED) ?: [];
|
||||
break;
|
||||
case 'json':
|
||||
$config = json_decode(file_get_contents($file), true);
|
||||
break;
|
||||
}
|
||||
|
||||
return is_array($config) ? $this->set($config, strtolower($name)) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测配置是否存在
|
||||
* @access public
|
||||
* @param string $name 配置参数名(支持多级配置 .号分割)
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name): bool
|
||||
{
|
||||
return !is_null($this->get($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一级配置
|
||||
* @access protected
|
||||
* @param string $name 一级配置名
|
||||
* @return array
|
||||
*/
|
||||
protected function pull(string $name): array
|
||||
{
|
||||
$name = strtolower($name);
|
||||
|
||||
return $this->config[$name] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置参数 为空则获取所有配置
|
||||
* @access public
|
||||
* @param string $name 配置参数名(支持多级配置 .号分割)
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name = null, $default = null)
|
||||
{
|
||||
// 无参数时获取所有
|
||||
if (empty($name)) {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
if (false === strpos($name, '.')) {
|
||||
return $this->pull($name);
|
||||
}
|
||||
|
||||
$name = explode('.', $name);
|
||||
$name[0] = strtolower($name[0]);
|
||||
$config = $this->config;
|
||||
|
||||
// 按.拆分成多维数组进行判断
|
||||
foreach ($name as $val) {
|
||||
if (isset($config[$val])) {
|
||||
$config = $config[$val];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置参数 name为数组则为批量设置
|
||||
* @access public
|
||||
* @param array $config 配置参数
|
||||
* @param string $name 配置名
|
||||
* @return array
|
||||
*/
|
||||
public function set(array $config, string $name = null): array
|
||||
{
|
||||
if (!empty($name)) {
|
||||
if (isset($this->config[$name])) {
|
||||
$result = array_merge($this->config[$name], $config);
|
||||
} else {
|
||||
$result = $config;
|
||||
}
|
||||
|
||||
$this->config[$name] = $result;
|
||||
} else {
|
||||
$result = $this->config = array_merge($this->config, array_change_key_case($config));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
Loaded 3 of 1279 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user