mikeyevstropov/configuration

该软件包已被废弃且不再维护。没有建议替代包。

读取和写入YAML文件中的配置参数。

1.0.4 2017-07-14 10:06 UTC

This package is not auto-updated.

Last update: 2021-06-26 10:37:18 UTC


README

读取和写入YAML文件中的配置参数。

安装

使用Composer添加依赖项

$ composer require mikeevstropov/configuration

示例用法

例如,我们有一个以下配置文件。

# app/config/config.yml
  
my_parameter: my_value

我们能用它做什么?

<?php
  
namespace Mikeevstropov\Configuration\Configuration;
  
// origin configuration
$originFile = 'app/config/config.yml';
  
// modified configuration
$modifiedFile = 'var/temp/config.yml';
  
// create instance
$configuration = new Configuration(
    $originFile,
    $modifiedFile
);
  
// get parameter
$value = $configuration->get('my_parameter'); // my_value
  
// set parameter
// and save it to the file of modified configuration
$configuration->set('my_parameter', 'new_value');
  
// check it
$configuration->get('my_parameter'); // new_value
  
// now we can remove instance or exit form runtime
unset($configuration);
  
// create instance again
$configuration = new Configuration(
    $originFile,
    $modifiedFile
);
  
// and check saved value
$value = $configuration->get('my_parameter'); // new_value
  
// it has
$configuration->has('my_parameter'); // true
  
// remove
$configuration->remove('my_parameter');
  
// not defined
$configuration->has('my_parameter'); // false
  
// also you can use "strict getter" to get existed value
// or thrown InvalidArgumentException if not
$configuration->getStrict('my_parameter'); // thrown InvalidArgumentException

// "strict getter" provide type hinting by second argument
// and thrown InvalidArgumentException if does not match
$configuration->getStrict('my_parameter', 'array'); // thrown InvalidArgumentException

// method getAssert provide assertion behaviour by method
// from Webmozart\Assert by second argument
$configuration->getAssert('my_parameter', 'nullOrStringNotEmpty'); // new_value

// arguments following the second will be passed to
// the assertion method from Webmozart\Assert
$configuration->getAssert('my_parameter', 'range', 10, 20); // thrown InvalidArgumentException

如你所见,当我们使用setter时,值将保存到修改后的配置文件中,并在下次加载时一并加载。

开发

克隆

$ git clone https://github.com/mikeevstropov/configuration.git

进入项目

$ cd configuration

安装依赖项

$ composer install

设置权限

$ sudo chmod 777 ./var -v -R

尝试测试(需要全局范围内的PHPUnit)

$ composer test