loops/config

简单配置设置管理的基础类。

1.0.2-RC 2016-04-06 12:04 UTC

This package is auto-updated.

Last update: 2024-08-29 02:36:44 UTC


README

简单配置设置管理的基础类。

要求

  • 至少PHP 5.3。
  • Loops\Autoloader包。

如何使用

自动加载

如果没有composer,请调用bootstrap

:::php
  require $path_to_package.'/bootstrap.inc.php';

此库使用基于包的自动加载,具有包命名空间目录(PSR-4)和下划线作为类名目录分隔符(PSR-0)。

扩展

\Loops\Config\Base类已被设计为可扩展,以便隔离包/捆绑/项目/组件配置。

:::php
  class MyConfig extends \Loops\Config\Base {}

魔术getter/setter/isset

然后,您应该能够分配/检索任何设置

:::php
  // set "my_custom_setting"
  MyConfig::getInstance()->my_custom_setting = 'foo';
  MyConfig::getInstance()->setMyCustomSetting( 'foo' );
  MyConfig::setMyCustomSetting( 'foo' );

  // get "my_custom_setting"
  $s = MyConfig::getInstance()->my_custom_setting;
  $s = MyConfig::getInstance()->getMyCustomSetting();
  $s = MyConfig::getMyCustomSetting();

  // "my_custom_setting" is set?
  isset( MyConfig::getInstance()->my_custom_setting );
  MyConfig::getInstance()->hasMyCustomSetting();
  MyConfig::hasMyCustomSetting();

  // set "my_custom_setting", if not exists
  MyConfig::getInstance()->defineMyCustomSetting( 'foo' );
  MyConfig::defineMyCustomSetting( 'foo' );

预置/附加

根据您的设置类型,您可以在某些值之前/之后添加/附加

:::php
  //// array case

  // prepend
  MyConfig::setMyCustomSetting( array( 0 => 'foo0' , 1 => 'bar1' ) );
  MyConfig::prependMyCustomSetting( array( 0 => 'bar0' , 2 => 'foo2' ) );
  $s = MyConfig::getMyCustomSetting(); // result to array( 'bar0' , 'bar1' , 'foo2' )

  // append
  MyConfig::setMyCustomSetting( array( 0 => 'foo0' , 1 => 'bar1' ) );
  MyConfig::appendMyCustomSetting( array( 0 => 'bar0' , 2 => 'foo2' ) );
  $s = MyConfig::getMyCustomSetting(); // result to array( 'foo0' , 'bar1' , 'foo2' )
  //// string case

  // prepend
  MyConfig::setMyCustomSetting( 'foo' );
  MyConfig::prependMyCustomSetting( 'bar' );
  $s = MyConfig::getMyCustomSetting(); // result to 'bar'.PATH_SEPARATOR.'foo'

  // append
  MyConfig::setMyCustomSetting( 'foo' );
  MyConfig::appendMyCustomSetting( 'bar' );
  $s = MyConfig::getMyCustomSetting(); // result to 'foo'.PATH_SEPARATOR.'bar'

  // prepend with custom separator
  MyConfig::setMyCustomSetting( 'foo' );
  MyConfig::prependMyCustomSetting( 'bar' , '|' );
  $s = MyConfig::getMyCustomSetting(); // result to 'bar|foo'

  // append with custom separator
  MyConfig::setMyCustomSetting( 'foo' );
  MyConfig::appendMyCustomSetting( 'bar' , '|' );
  $s = MyConfig::getMyCustomSetting(); // result to 'foo|bar'
  //// integer case

  // prepend
  MyConfig::setMyCustomSetting( 4 );
  MyConfig::prependMyCustomSetting( 2 );
  $s = MyConfig::getMyCustomSetting(); // result to 6 (4|2)

  // append
  MyConfig::setMyCustomSetting( 4 );
  MyConfig::appendMyCustomSetting( 2 );
  $s = MyConfig::getMyCustomSetting(); // result to 6 (4|2)

加载/转储

用于转储/加载当前配置的有用方法。

:::php
  // dump
  $s = MyConfig::->getInstance()->dump();

  // load
  MyConfig::->getInstance()->load( $s );

限制

属性名中的数字

您可能会遇到驼峰/下划线转换的问题,特别是在这些情况下

:::php
  // 'my_setting1' => 'MySetting1' => 'my_setting_1'
  $s = MyConfig::underscore( MyConfig::camelize( 'my_setting1' ) ); // result to 'my_setting_1'

  // 'my2_setting' => 'My2Setting' => 'my_2_setting'
  $s = MyConfig::underscore( MyConfig::camelize( 'my2_setting' ) ); // result to 'my_2_setting'

无法逻辑地确定是否在数字之前保留下划线更好,因此请留意此行为。

附加/预置空字符串

目前,append/prepend方法附加/预置空字符串(开发者可能有一个很好的理由这么做)。

:::php
  MyConfig::setMySetting1( 'a' );
  MyConfig::appendMySetting1( '' );
  $s = MyConfig::getMySetting1(); // result to 'a'.PATH_SEPARATOR

  MyConfig::setMySetting1( 'a' );
  MyConfig::prependMySetting1( '' );
  $s = MyConfig::getMySetting1(); // result to PATH_SEPARATOR.'a'

贡献者

想贡献吗?

只有一个规则要遵循: 挑战自己