abdeslam/configuration-manager

一个快速、强大且灵活的PHP、JSON和XML配置加载器

1.0.0 2021-06-27 12:45 UTC

This package is auto-updated.

Last update: 2024-09-05 02:03:26 UTC


README

ConfigurationManager 是一个简单、强大且灵活的 PHPJSONXML 配置文件加载器。

目录

要求

  • php: ^8.0

安装

1. 使用Composer

您可以通过 Composer 安装此库。

php composer.phar require abdeslam/configuration-manager

composer require abdeslam/configuration-manager

2. 手动

如果您不使用Composer,您还可以将 Abdeslam/ConfigurationManager 仓库克隆到您的目录中

git clone https://github.com/Abdeslam-Gacemi/ConfigurationManager.git

然而,推荐使用Composer,因为您可以轻松保持库的更新。

使用方法

1. 使用 ConfigurationManager

假设这是您的PHP配置文件

<?php

// config.php

return [
    'database' => [
        'driver' => 'mysql',
        'username' => 'user',
        'password' => '1234',
    ],
    'debug' => true
];
<?php

use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

require '/path/to/autoload.php';

$loader = new PHPConfigurationLoader();
$manager = new ConfigurationManager();
$manager->addLoader($loader, '/path/to/config.php')->load();

echo $manager->get('database.driver'); // output: 'mysql'

或加载多个配置文件:要加载多个配置文件,请在第一个参数(加载器)之后添加尽可能多的文件作为参数到 ConfigurationManager::addLoader() 方法中。

<?php

use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

require '/path/to/autoload.php';

$loader = new PHPConfigurationLoader();
$manager = new ConfigurationManager();
// loading multiple configuration files
$manager->addLoader($loader, '/path/to/config.php', '/path/to/config2.php')->load();

// rest of the code

通过 ConfigurationManager 可用的方法

<?php

use Abdeslam\ConfigurationManager\ConfigurationManager;
use Abdeslam\ConfigurationManager\Loaders\PHPConfigurationLoader;

require '/path/to/autoload.php';

$loader = new PHPConfigurationLoader();
$manager = new ConfigurationManager();
$manager->addLoader($loader, '/path/to/config.php')->load();

echo $manager->get('database.driver'); // output: 'mysql'
$manager->get('debug'); // returns: (bool) true
$manager->get('non_existing_key'); // throws an exception
$manager->has('debug')); // returns: (bool) true
$manager->has('non_existing_key'); // returns: (bool) false
$manager->remove('debug'); // removes 'debug' item from the configuration items array

$manager->all(); // returns: the array of all the configuration items
$manager->getLoaders(); // returns: the array of all the registered loaders
$manager->getLoader(PHPConfigurationLoader::class); // returns: the loader instance
$manager->hasLoader(PHPConfigurationLoader::class); // returns: true
$manager->getLoadedFiles(); // returns: an array of all the loaded files
$manager->set('api_key' => 'some_key'); // adds an item with the key 'api_key' and the value 'some_key' to the configuration items
$manager->merge(['verbose' => true]); // merges the configuration items array with the array given as an argument
$manager->merge($anotherManagerInstance); // merges the array of items of the original manager with the array of items of the managers supplies as an argument
$manager->reset(); // resets the object to the initial state

2. 使用静态工厂

要设置要使用的特定加载器,将以下字符串之一作为第一个参数传递给 ConfigurationManagerFactory::create() 静态工厂方法

  • 'php':使用 PHPConfigurationLoader::class 作为默认加载器
  • 'json':使用 JSONConfigurationLoader::class 作为默认加载器
  • 'xml':使用 XMLConfigurationLoader::class 作为默认加载器
<?php

use Abdeslam\ConfigurationManager\ConfigurationManagerFactory;

require '/path/to/autoload.php';

$manager = ConfigurationManagerFactory::create(
  'php',
  '/path/to/config.php',
  '/path/to/config2.php'
);
$manager->load();

// rest of the code

访问值

ConfigurationManager::get()ConfigurationManager::has() 具有默认通过 '.' 分隔的 复合键解析 功能,并且可以更改键分隔符,这意味着

<?php

// config.php
return [
  'address' => [
    'country' => 'Algeria',
    'city' => [
      'name' => 'Algiers',
      'postal_code' => '16000'
    ]
  ]
];

// client code
$manager->get('address.city.name'); // returns: 'Algiers'
$manager->setKeySeparator('_'); // changes the key separator to '_'
$manager->get('address_country'); // returns: 'Algeria'
$manager->getKeySeparator(); // returns: '_'

自定义

您可以通过创建一个实现了 ConfigurationLoaderInterface::class 的类来创建自己的配置加载器

<?php

// CustomConfigurationLoader.php

use Abdeslam\ConfigurationManager\Contracts\ConfigurationLoaderInterface;

class CustomConfigurationLoader implements ConfigurationLoaderInterface
{
  /**
   * @inheritDoc
   */
  public function (string ...$filepaths): array
  {
    return ['hello' => 'world'];
  }
}

// client code
$manager->addLoader(new CustomLoader(), '/path/to/configuration/file');
$manager->load();
$manager->get('hello'); // returns: 'world'

// using the factory
ConfigurationManagerFactory::addConfigurationLoader('custom_loader', CustomConfigurationLoader::class);
$manager = ConfigurationManagerFactory::create('custom_loader', '/path/to/configuration/file');
$manager->load();
$manager->get('hello'); // returns: 'world'

// overriding Factory loaders
ConfigurationManagerFactory::addConfigurationLoader('php', CustomConfigurationLoader::class);
$manager = ConfigurationManagerFactory::create('php', '/path/to/config.php');
$manager->load();
$manager->get('hello'); // returns: 'world'

用爱制作 ❤️