illuminate/database 使用 二

上一篇文章写怎么单独使用illuminate/database,这回讲下怎么整合到项目里使用。为此特意看了下laravel对其使用。本篇文章,参照laravel的使用,简单实现。

一 原理

laravel 里使用illuminate/config。

 illuminate/config composer 地址:illuminate/config - Packagist

 illuminate/config github 地址:GitHub - illuminate/config: [READ ONLY] Subtree split of the Illuminate Config component (see laravel/framework)

 根据  illuminate/config 源码显示 illuminate/config/Repository.php  仅继承Illuminate\Contracts\Config\Repository,没有其他操作。

所以编写Myconfig.php相同代码,没有加入 illuminate/config。也可以直接执行命令添加。

composer require illuminate/config:版本

通过查看laravel源码,发现先加入容器,再重写容器中绑定的对象。

之所以不能直接绑定操作后的对象,因为bind()、bindIf()等相关方法中,被绑定的数据不能传对象,而instance()没有此限制,所以能传设置参数后的对象。

数据库管理对象构造传入容器对象,会自动获取config对应参数。之后可以直接查询。

例子里对于设置容器后,处理配置文件的流程与laravel不同。laravel中更复杂也更完善。

二 代码

目录结构

- test.php

- config/database.php

- Myconfig.php

//test.php
require_once './vendor/autoload.php';
require_once './Myconfig.php';use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DbManager;
use Illuminate\Events\Dispatcher;class Loader
{private $container;public function __construct(Container $container){$this->container = $container;$this->makes();}private function get_makes_list(){$list = ['config' => Myconfig::class,];return $list;}private function makes(){$list = $this->get_makes_list();foreach ($list as $key => $value) {$this->container->bindIf($key, $value);}}public function get_path_list($name){$list = ['config' => "./config",];return $list[$name] ? $list[$name] : false;}
}class App
{private $container;private $loader;public function __construct(){$this->container = new Container();$this->loader = new Loader($this->container);$this->init();}public function init(){$this->init_config();$this->init_db();}private function init_config(){$config_list = [];$config_path = $this->loader->get_path_list('config');if ($files_list = opendir($config_path)) {while (($file = readdir($files_list)) != false) {if (!preg_match('/(.*).php/', $file, $matches)) {continue;}$data = include $config_path . "/" . $file;if (!is_array($data)) {continue;}$file_name = $matches[1];foreach ($data as $key => $value) {$list_key = $file_name . "." . $key;$config_list[$list_key] = $value;}}}if (!empty($config_list)) {$myconfig = new Myconfig($config_list);$this->container->instance('config', $myconfig);}}private function init_db(){$dbm = new DbManager($this->container);$dbm->setEventDispatcher(new Dispatcher(new Container));$dbm->setAsGlobal(); //设置静态全局可用$dbm->bootEloquent();}
}function test()
{new App();$info = DbManager::table('userinfo')->where('id', '=', 2)->get();var_dump($info);
}test();
//./config/database.php
return ['migrations' => '', //数据迁移//PDO文档 https://www.php.net/manual/zh/pdo.constants.php'fetch' => PDO::FETCH_OBJ,'default' => 'master','connections' => ['master' => ['driver' => 'mysql','host' => 'localhost','database' => 'test','username' => 'root','password' => 'qwe110110','charset' => 'utf8','collation' => 'utf8_general_ci','prefix' => '',],'test' => ["driver" => "mysql","host" => "127.0.0.1","database" => "brook3_master","username" => "root","password" => "root",'charset' => 'utf8mb4','collation' => 'utf8mb4_unicode_ci','prefix' => '',],],
];
//./Myconfig.php
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;class Myconfig implements ConfigContract, ArrayAccess
{/*** All of the configuration items.** @var array*/protected $items = [];/*** Create a new configuration repository.** @param  array  $items* @return void*/public function __construct(array $items = []){$this->items = $items;}/*** Determine if the given configuration value exists.** @param  string  $key* @return bool*/public function has($key){return Arr::has($this->items, $key);}/*** 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);}/*** Get many configuration values.** @param  array  $keys* @return array*/public function getMany($keys){$config = [];foreach ($keys as $key => $default) {if (is_numeric($key)) {[$key, $default] = [$default, null];}$config[$key] = Arr::get($this->items, $key, $default);}return $config;}/*** 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);}}/*** Prepend a value onto an array configuration value.** @param  string  $key* @param  mixed  $value* @return void*/public function prepend($key, $value){$array = $this->get($key, []);array_unshift($array, $value);$this->set($key, $array);}/*** Push a value onto an array configuration value.** @param  string  $key* @param  mixed  $value* @return void*/public function push($key, $value){$array = $this->get($key, []);$array[] = $value;$this->set($key, $array);}/*** Get all of the configuration items for the application.** @return array*/public function all(){return $this->items;}/*** Determine if the given configuration option exists.** @param  string  $key* @return bool*/#[\ReturnTypeWillChange]public function offsetExists($key){return $this->has($key);}/*** Get a configuration option.** @param  string  $key* @return mixed*/#[\ReturnTypeWillChange]public function offsetGet($key){return $this->get($key);}/*** Set a configuration option.** @param  string  $key* @param  mixed  $value* @return void*/#[\ReturnTypeWillChange]public function offsetSet($key, $value){$this->set($key, $value);}/*** Unset a configuration option.** @param  string  $key* @return void*/#[\ReturnTypeWillChange]public function offsetUnset($key){$this->set($key, null);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/119719.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot整合OAuth2实现GitHub第三方登录

Spring Boot整合OAuth2,实现GitHub第三方登录 1、第三方登录原理 第三方登录的原理是借助OAuth授权来实现,首先用户先向客户端提供第三方网站的数据证明自己的身份获取授权码,然后客户端拿着授权码与授权服务器建立连接获得一个Access Token…

vuejs实现点击导出按钮把数据加密后传到json/txt格式文件中并下载,以及上传json文件解密获得json内容

vuejs实现点击导出按钮把数据加密后传到json/txt格式文件中并下载,以及上传json文件解密获得json内容 (1)在Vue.js中使用crypto-js进行加密和解密,首先安装crypto-js库 npm install crypto-js(2)在需要使…

研发效能认证学员作品:快速进行持续集成应用实践丨IDCF

作者:赖嘉明 研发效能(DevOps)工程师认证学员 随着数字化转型的推进及市场竞争的加剧,越来越多的企业也意识到持续集成的重要性。 而持续集成作为一种先进的软件开发实践和工具链,可以帮助企业实现自动化构建、集成和…

systemctl 自启软件闪屏桌面

一、问题分析 systemctl 服务启动在桌面系统之前,启动界面加载到 100% 时桌面系统开始加载,会强制隐藏我们的界面并显示桌面,待桌面彻底加载完毕,才能显示我们的软件界面。这期间就是闪屏并显示桌面的原因。 不过正常情况桌面系…

STM32CubeMX和STM32F4

目录 嵌入式开发的硬件相关STM32CubeMXSTM32F4Cortex-M4-FSFPU 嵌入式开发的软件相关μC/OS-II 嵌入式开发的硬件相关 STM32CubeMX STM32CubeMX是一个基于图形界面的工具(是软件),用于配置和生成STM32微控制器的初始化代码和库文件。它适用于基于ARM C…

文件上传漏洞(1), 文件上传绕过原理

文件上传漏洞 一, 前端校验上传文件 添加 Javascript 代码&#xff0c;然后在 form 表单中 添加 onsubmit"returb checkFile()" <script>function checkFile() {// var file document.getElementsByName(photo)[0].value;var file document.getElementByI…

基于YOLOv8模型的烟雾目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的烟雾目标检测系统可用于日常生活中检测与定位烟雾目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

elasticsearch-7.9.3 单节点启动配置

一、elasticsearch-7.9.3 单节点启动配置 node.name: node-1 network.host: 192.168.227.128 http.port: 9200 discovery.seed_hosts: ["192.168.227.128"] node.max_local_storage_nodes: 1 discovery.type: single-node二、kibana-7.9.3-linux-x86_64 单节点启动配…

Ubuntu22.04安装,SSH无法连接

Ubuntu初始化安装后&#xff0c;系统默认不允许root通过ssh连接&#xff0c;因此需要完成三个设置 1.修改ssh配置文件 vim /etc/ssh/sshd_config 将PermitRootLogin注释打开&#xff0c;并将值改为yes 保存修改并退出 :wq 2.重启ssh服务 sudo service ssh restart 3.重新打…

spark3.3.x处理excel数据

环境: spark3.3.x scala2.12.x 引用: spark-shell --jars spark-excel_2.12-3.3.1_0.18.5.jar 或项目里配置pom.xml <!-- https://mvnrepository.com/artifact/com.crealytics/spark-excel --> <dependency><groupId>com.crealytics</groupId><art…

八大排序算法(C语言版)之插入排序

八大排序详解 目录&#xff1a;一、排序的概念1.1 排序的概念1.2 排序的应用 二、直接插入排序三、希尔排序四、排序算法复杂度及稳定性分析 目录&#xff1a; 八大排序算法&#xff1a; #mermaid-svg-7qCaGEYz0Jyj9dYw {font-family:"trebuchet ms",verdana,arial,…

[极客大挑战 2019]Havefun

1.打开链接 2.检查一下源代码 发现一段代码。 3.分析代码 <!-- $cat$_GET[cat]; echo $cat; if($catdog){ echo Syc{cat_cat_cat_cat}; } --> 询问ChatGPT&#xff1a; 从您提供的代码片段来看&#xff0c;这是…

C++中低级内存操作

C中低级内存操作 C相较于C有一个巨大的优势&#xff0c;那就是你不需要过多地担心内存管理。如果你使用面向对象的编程方式&#xff0c;你只需要确保每个独立的类都能妥善地管理自己的内存。通过构造和析构&#xff0c;编译器会帮助你管理内存&#xff0c;告诉你什么时候需要进…

GB/T 29734.2-2013 铝塑复合门窗检测

铝合金门窗是指采用铝塑复合型材制作框、扇杆件结构的门、窗的总称&#xff0c;根据门窗开启形式的不同&#xff0c;可分为固定窗、平开窗、推拉窗&#xff0c;悬窗等。 GB/T 29734.2-2013 铝塑复合门窗检测项目 测试项目 测试标准 外观质量 GB/T 29734.2 尺寸 GB/T 2973…

面试经验——面试项目准备工作

摘要 本博文主要是分享个人在面试中对于项目思考&#xff0c;希望帮助大家能够面试中能够很好的介绍和分享自己的项目。在面试官心中留下一个好印象&#xff0c;希望你能拿到自己满意的offer。 一、面试项目常见问题 1.1 工作经历中&#xff0c;最优技术挑战/亮点的事情是什…

Elasticsearch聚合----aggregations的简单使用

文章目录 Getting started1、搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄&#xff0c;但不显示这些人的详情2、size0不展示命中记录&#xff0c;只展示聚合结果3、按照年龄聚合&#xff0c;并且请求这些年龄段的这些人的平均薪资4、查出所有年龄分布&#xff0c;…

【Android Studio】工程中文件Annotate with Git Blame 不能点击

问题描述 工程文件中想要查看代码提交信息但是相关按钮不可点击 解决方法 Android Studio -> Preferences -> Version Control-> 在Unregistered roots里找到你想要的工程文件 点击左上角➕号 然后右下角Apply即可

Python分享之多进程初步 (multiprocessing包)

我们已经见过了使用subprocess包来创建子进程&#xff0c;但这个包有两个很大的局限性&#xff1a;1) 我们总是让subprocess运行外部的程序&#xff0c;而不是运行一个Python脚本内部编写的函数。2) 进程间只通过管道进行文本交流。以上限制了我们将subprocess包应用到更广泛的…

【异常】理解Java中的异常处理机制

标题&#xff1a;理解Java中的异常处理机制 摘要&#xff1a; 异常处理是Java编程中的重要概念之一&#xff0c;它可以帮助开发者识别和处理程序运行过程中的错误和异常情况。本文将深入探讨Java中的异常处理机制&#xff0c;包括异常的分类、异常处理的语法和最佳实践。通过示…

Filter过滤器和Listener监听器

2023.10.26 Filter过滤器 过滤器&#xff0c;顾名思义就是对事物进行过滤的。Web中的过滤器&#xff0c;就是对请求进行过滤&#xff0c;我们使用过滤器&#xff0c;就可以对请求进行拦截&#xff0c;然后做相应的处理&#xff0c;实现许多特殊功能。如登录控制&#xff0c;权…