Laravel|Lumen项目配置信息config原理

ops/2024/10/19 7:27:22/

介绍

Laravel 框架的所有配置文件都保存在 config 目录中。每个选项都有说明,你可随时查看这些文件并熟悉都有哪些配置选项可供你使用。

使用

您可以在应用程序的任何位置使用全局 config 辅助函数轻松访问配置值。 可以使用“点”语法访问配置值,其中包括您希望访问的文件名和选项。 也可以指定默认值,如果配置选项不存在,将返回:

php">$value = config('app.timezone');// 如果配置值不存在,则检索默认值...
$value = config('app.timezone', 'Asia/Seoul');

使用分析

我们可以先看一下助手函数 config

php">if (! function_exists('config')) {/*** Get / set the specified configuration value.** If an array is passed as the key, we will assume you want to set an array of values.** @param  array|string|null  $key* @param  mixed  $default* @return mixed*/function config($key = null, $default = null){if (is_null($key)) {return app('config');}if (is_array($key)) {return app('config')->set($key);}return app('config')->get($key, $default);}
}

分析

app(‘config’) 是注册到服务容器的实例,这里我们追踪到config 的实例是

Illuminate\Contracts\Config\Repository

Repository 的 set & get 方法代码如下

php">    /*** Set a given configuration value.** @param  array|string  $key* @param  mixed  $value* @return void*/public function set($key, $value = null){$keys = is_array($key) ? $key : [$key => $value];foreach ($keys as $key => $value) {Arr::set($this->items, $key, $value);}}/*** Get the specified configuration value.** @param  array|string  $key* @param  mixed  $default* @return mixed*/public function get($key, $default = null){if (is_array($key)) {return $this->getMany($key);}return Arr::get($this->items, $key, $default);}

这时我们就可以清晰的知道,助手函数config($key = null, $default = null)的含义:

  • 如果$key 不传或者传null,返回的是注册到服务容器中Repository实例
  • 如果$key 为数组,那么修改Repository 的 items的值
  • 其他情况 返回 Repository 的 items[$key] ?? $default

  • 此时我们就可能有疑问 Repositiry 中 items 值是什么时候进行赋值的呢?赋的值又是那些呢?接下来我们一点点的分析

    绑定服务容器

    laravel运行中,并没有直接绑定 Repositiry 到容器中,而是第一次构建config实例的时候进行了绑定,app(‘config’) 或者 app()->make(‘config’) 时检测是否进行绑定,如果没有的话进行绑定,下面时make函数:

    php">    /*** Resolve the given type from the container.** @param  string  $abstract* @param  array  $parameters* @return mixed*/public function make($abstract, array $parameters = []){$abstract = $this->getAlias($abstract);if (! $this->bound($abstract) &&array_key_exists($abstract, $this->availableBindings) &&! array_key_exists($this->availableBindings[$abstract], $this->ranServiceBinders)) {$this->{$method = $this->availableBindings[$abstract]}();$this->ranServiceBinders[$method] = true;}return parent::make($abstract, $parameters);}
    

    根据代码可知

    php">$this->availableBindings['config'] = registerConfigBindings
    

    registerConfigBindings函数即为绑定到容器方法

    php">    /*** Register container bindings for the application.** @return void*/protected function registerConfigBindings(){$this->singleton('config', function () {return new ConfigRepository;});}
    

    因此在调用make(‘config’) 时若没有绑定,自动调用registerConfigBindings方法进行绑定

    配置文件赋值

    在bootstrap/app.php中,进行对配置文件进行了赋值,譬如:

    php">$app->configure('auth');
    $app->configure('permission');
    $app->configure('excel');
    $app->configure('database');
    

    configure函数为:

    php">    /*** Load a configuration file into the application.** @param  string  $name* @return void*/public function configure($name){if (isset($this->loadedConfigurations[$name])) {return;}$this->loadedConfigurations[$name] = true;$path = $this->getConfigurationPath($name);if ($path) {$this->make('config')->set($name, require $path);}}
    

    这里的操作就是将conf/$name.conf 的内容写到 Repositiry 中 items 值中


http://www.ppmy.cn/ops/126661.html

相关文章

【项目案例分享|易知微智慧工厂车间生产线数字孪生管理系统

一、项目背景 在现代工厂的车间产线中,普遍存在的问题包括效率瓶颈、质量控制不稳定、设备维护不足、物料管理不善以及人力资源配置不合理等。这些问题可能导致生产成本增加、交货时间延长、产品质量下降,从而影响企业的市场竞争力。数字化和智能化建设…

Web开发动画与性能优化

帧率说明 帧率(Frames Per Second, FPS)指的是每秒钟渲染的帧数。通常情况下,60FPS【每秒60帧】被认为是流畅动画的标准。这意味着每帧的渲染时间需要控制在16.67毫秒以内。帧率越高,动画越流畅;反之,帧率…

穿越沙漠问题

题目:一辆吉普车穿越1000km的沙漠。吉普车的总装油量为500L,耗油率为1L/km。由于沙漠中没有油库,必须先用这辆车在沙漠中建立临时油库。若吉普车用最少的耗油量穿越沙漠,应在哪些地方建立油库,以及各处存储的油量是多少…

第二章:信息建模:概念2

对象、变量和方法 # OPC UA 中最重要的节点类是对象、变量和方法。这些概念也是面向对象编程的产物。对象具有变量和方法,并且可以触发事件。 变量节点类的节点表示一个值。值的数据类型取决于变量。客户端可以读取值、订阅值的更改以及写入值。例如,变…

Spring Web MVC快速入门:掌握Java Web开发基础

White graces:个人主页 🙉专栏推荐:Java入门知识🙉 🐹今日诗词:桃李春风一杯酒,江湖夜雨十年灯🐹 ⛳️点赞 ☀️收藏⭐️关注💬卑微小博主🙏 ⛳️点赞 ☀️收藏⭐️关注&#x1f4…

深入理解Transformer的笔记记录(精简版本)---- ELMO->GPT->BERT

1、ELMO word embedding无法区分多义词的不同语义,其本质上是个静态的方式,所谓静态指的是训练好之后每个单词的表达就固定住了,以后使用的时候,不论新句子上下文单词是什么,这个单词的Word Embedding不会跟着上下文场…

抖音视频制作怎么暂停画面,抖音视频怎么让它有暂停的效果

千万别滥用视频特效,不然它能毁掉你的抖音作品。在创作过程中,应尽量使用类似暂停画面、隐形字幕这样的视觉特效,可以显著提高作品的视觉体验。增强视频表现力的同时,也不会让画面看起来过于夸张。有关抖音视频制作怎么暂停画面的…

Redis是单线程为何性能还高

背景 通常来讲,提到性能优化,我们都会说提高并行度。同样我们知道,Redis是单线程执行命令,那为何还能保持如此的高性能呢? 原因 基于内存访问 Redis 将所有数据存储在内存中,内存的读写速度远远高于磁盘&a…