• 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

    1. //test.php
    2. require_once './vendor/autoload.php';
    3. require_once './Myconfig.php';
    4. use Illuminate\Container\Container;
    5. use Illuminate\Database\Capsule\Manager as DbManager;
    6. use Illuminate\Events\Dispatcher;
    7. class Loader
    8. {
    9. private $container;
    10. public function __construct(Container $container)
    11. {
    12. $this->container = $container;
    13. $this->makes();
    14. }
    15. private function get_makes_list()
    16. {
    17. $list = [
    18. 'config' => Myconfig::class,
    19. ];
    20. return $list;
    21. }
    22. private function makes()
    23. {
    24. $list = $this->get_makes_list();
    25. foreach ($list as $key => $value) {
    26. $this->container->bindIf($key, $value);
    27. }
    28. }
    29. public function get_path_list($name)
    30. {
    31. $list = [
    32. 'config' => "./config",
    33. ];
    34. return $list[$name] ? $list[$name] : false;
    35. }
    36. }
    37. class App
    38. {
    39. private $container;
    40. private $loader;
    41. public function __construct()
    42. {
    43. $this->container = new Container();
    44. $this->loader = new Loader($this->container);
    45. $this->init();
    46. }
    47. public function init()
    48. {
    49. $this->init_config();
    50. $this->init_db();
    51. }
    52. private function init_config()
    53. {
    54. $config_list = [];
    55. $config_path = $this->loader->get_path_list('config');
    56. if ($files_list = opendir($config_path)) {
    57. while (($file = readdir($files_list)) != false) {
    58. if (!preg_match('/(.*).php/', $file, $matches)) {
    59. continue;
    60. }
    61. $data = include $config_path . "/" . $file;
    62. if (!is_array($data)) {
    63. continue;
    64. }
    65. $file_name = $matches[1];
    66. foreach ($data as $key => $value) {
    67. $list_key = $file_name . "." . $key;
    68. $config_list[$list_key] = $value;
    69. }
    70. }
    71. }
    72. if (!empty($config_list)) {
    73. $myconfig = new Myconfig($config_list);
    74. $this->container->instance('config', $myconfig);
    75. }
    76. }
    77. private function init_db()
    78. {
    79. $dbm = new DbManager($this->container);
    80. $dbm->setEventDispatcher(new Dispatcher(new Container));
    81. $dbm->setAsGlobal(); //设置静态全局可用
    82. $dbm->bootEloquent();
    83. }
    84. }
    85. function test()
    86. {
    87. new App();
    88. $info = DbManager::table('userinfo')->where('id', '=', 2)->get();
    89. var_dump($info);
    90. }
    91. test();
    1. //./config/database.php
    2. return [
    3. 'migrations' => '', //数据迁移
    4. //PDO文档 https://www.php.net/manual/zh/pdo.constants.php
    5. 'fetch' => PDO::FETCH_OBJ,
    6. 'default' => 'master',
    7. 'connections' => [
    8. 'master' => [
    9. 'driver' => 'mysql',
    10. 'host' => 'localhost',
    11. 'database' => 'test',
    12. 'username' => 'root',
    13. 'password' => 'qwe110110',
    14. 'charset' => 'utf8',
    15. 'collation' => 'utf8_general_ci',
    16. 'prefix' => '',
    17. ],
    18. 'test' => [
    19. "driver" => "mysql",
    20. "host" => "127.0.0.1",
    21. "database" => "brook3_master",
    22. "username" => "root",
    23. "password" => "root",
    24. 'charset' => 'utf8mb4',
    25. 'collation' => 'utf8mb4_unicode_ci',
    26. 'prefix' => '',
    27. ],
    28. ],
    29. ];
    1. //./Myconfig.php
    2. use Illuminate\Contracts\Config\Repository as ConfigContract;
    3. use Illuminate\Support\Arr;
    4. class Myconfig implements ConfigContract, ArrayAccess
    5. {
    6. /**
    7. * All of the configuration items.
    8. *
    9. * @var array
    10. */
    11. protected $items = [];
    12. /**
    13. * Create a new configuration repository.
    14. *
    15. * @param array $items
    16. * @return void
    17. */
    18. public function __construct(array $items = [])
    19. {
    20. $this->items = $items;
    21. }
    22. /**
    23. * Determine if the given configuration value exists.
    24. *
    25. * @param string $key
    26. * @return bool
    27. */
    28. public function has($key)
    29. {
    30. return Arr::has($this->items, $key);
    31. }
    32. /**
    33. * Get the specified configuration value.
    34. *
    35. * @param array|string $key
    36. * @param mixed $default
    37. * @return mixed
    38. */
    39. public function get($key, $default = null)
    40. {
    41. if (is_array($key)) {
    42. return $this->getMany($key);
    43. }
    44. return Arr::get($this->items, $key, $default);
    45. }
    46. /**
    47. * Get many configuration values.
    48. *
    49. * @param array $keys
    50. * @return array
    51. */
    52. public function getMany($keys)
    53. {
    54. $config = [];
    55. foreach ($keys as $key => $default) {
    56. if (is_numeric($key)) {
    57. [$key, $default] = [$default, null];
    58. }
    59. $config[$key] = Arr::get($this->items, $key, $default);
    60. }
    61. return $config;
    62. }
    63. /**
    64. * Set a given configuration value.
    65. *
    66. * @param array|string $key
    67. * @param mixed $value
    68. * @return void
    69. */
    70. public function set($key, $value = null)
    71. {
    72. $keys = is_array($key) ? $key : [$key => $value];
    73. foreach ($keys as $key => $value) {
    74. Arr::set($this->items, $key, $value);
    75. }
    76. }
    77. /**
    78. * Prepend a value onto an array configuration value.
    79. *
    80. * @param string $key
    81. * @param mixed $value
    82. * @return void
    83. */
    84. public function prepend($key, $value)
    85. {
    86. $array = $this->get($key, []);
    87. array_unshift($array, $value);
    88. $this->set($key, $array);
    89. }
    90. /**
    91. * Push a value onto an array configuration value.
    92. *
    93. * @param string $key
    94. * @param mixed $value
    95. * @return void
    96. */
    97. public function push($key, $value)
    98. {
    99. $array = $this->get($key, []);
    100. $array[] = $value;
    101. $this->set($key, $array);
    102. }
    103. /**
    104. * Get all of the configuration items for the application.
    105. *
    106. * @return array
    107. */
    108. public function all()
    109. {
    110. return $this->items;
    111. }
    112. /**
    113. * Determine if the given configuration option exists.
    114. *
    115. * @param string $key
    116. * @return bool
    117. */
    118. #[\ReturnTypeWillChange]
    119. public function offsetExists($key)
    120. {
    121. return $this->has($key);
    122. }
    123. /**
    124. * Get a configuration option.
    125. *
    126. * @param string $key
    127. * @return mixed
    128. */
    129. #[\ReturnTypeWillChange]
    130. public function offsetGet($key)
    131. {
    132. return $this->get($key);
    133. }
    134. /**
    135. * Set a configuration option.
    136. *
    137. * @param string $key
    138. * @param mixed $value
    139. * @return void
    140. */
    141. #[\ReturnTypeWillChange]
    142. public function offsetSet($key, $value)
    143. {
    144. $this->set($key, $value);
    145. }
    146. /**
    147. * Unset a configuration option.
    148. *
    149. * @param string $key
    150. * @return void
    151. */
    152. #[\ReturnTypeWillChange]
    153. public function offsetUnset($key)
    154. {
    155. $this->set($key, null);
    156. }
    157. }

  • 相关阅读:
    300PLC转以太网与MatrikonOPC以太网通讯
    09.Tornado_获取请求参数
    Golang综合项目实战(一)
    Android gradle dependency tree change(依赖树变化)监控实现
    盘点:数字人直播系统源码部署哪家好?
    Python手写语音识别文字
    selenium反反爬虫,隐藏selenium特征
    [XCUITest] 处理iOS权限点击授权 有哪些权限?
    布朗大学发现GPT-4存在新问题,可通过非常见语言绕过限制
    ChatGPT3 Transformer 的多模态全能语言模型
  • 原文地址:https://blog.csdn.net/lsswear/article/details/134036432