am/config

1.2.2 2019-06-12 14:20 UTC

This package is auto-updated.

Last update: 2024-09-13 01:53:14 UTC


README

关于

PHP配置加载类帮助通过数组或Yaml配置文件管理和加载应用程序配置。

安装

通过Composer

composer require am/config

或者简单地下载代码并将其添加到您的项目中。

示例用法

config.yaml

root_path: C:/xampp/htdocs
base_url: /
directories:
  controllers: controllers
  models: models
  views: views
  core: core
test_number: 10

config-2.yaml

this name: config 2
this number: 10
test_number: 10
<?php
require dirname(__DIR__) . '/vendor/autoload.php';

$config_mixed_src = array(
	dirname(__DIR__) . '/examples/configs/config.yaml',
	dirname(__DIR__) . '/examples/configs/config-2.yaml',
	array(
		'hotel'=>'5 star',
		'rooms'=> 10
	)
);
$config = new \Am\Config\Config($config_mixed_src,1,true);

var_dump($config);

结果转储

<?php
object(Am\Config\Config)[2]
  public 'config' => 
    array (size=3)
      0 => string 'C:\xampp\htdocs\projects\composer\config/examples/configs/config.yaml' (length=69)
      1 => string 'C:\xampp\htdocs\projects\composer\config/examples/configs/config-2.yaml' (length=71)
      2 => 
        array (size=2)
          'hotel' => string '5 star' (length=6)
          'rooms' => int 10
  public 'src' => int 1
  public 'debug' => boolean true
  public 'root_path' => string 'C:/xampp/htdocs' (length=15)
  public 'base_url' => string '/' (length=1)
  public 'directories' => 
    array (size=4)
      'controllers' => string 'controllers' (length=11)
      'models' => string 'models' (length=6)
      'views' => string 'views' (length=5)
      'core' => string 'core' (length=4)
  public 'test_number' => int 10
  public 'this_name' => string 'config 2' (length=8)
  public 'this_number' => int 10
  public 'hotel' => string '5 star' (length=6)
  public 'rooms' => int 10

现在您可以在文件中作为公共变量检索应用程序配置,如下所示

echo $config->hotel; //returns "5 star"

foreach($config->directories as $dir){
	echo $dir;
}

附加资源

如何安装LibYAML

要允许PHP解析Yaml文件,您将需要php libyaml可用。

https://github.com/LegendOfMCPE/LoM-CMS/wiki/How-to-Install-LibYAML

https://stackoverflow.com/questions/27122701/installing-php-yaml-extension-with-xampp-on-windows

注意:对于Windows Xampp用户,将yaml.dll复制到您的Xampp php文件夹中也是有用的,以便在CLI中运行(这在运行测试时尤为重要)。