web6/config

简单的PHP配置读取器

0.0.1 2019-05-19 21:26 UTC

This package is auto-updated.

Last update: 2024-09-28 11:27:36 UTC


README

此类读取存储在PHP文件中的配置值,并将它们作为属性提供。

安装

通过Composer安装

$ composer require web6/config

使用方法

创建配置文件

创建一个包含配置的PHP文件。此文件必须由服务器可读。

<?php
// The configuration is stored in a simple PHP array
$config = array();
$config['title'] = 'Lorem Ipsum sit dolor amet';
// IMPORTANT : return the configuration!
return $config;

使用PHP数组作为配置提供了很大的灵活性和良好的可读性。一些示例位于此文件的末尾。

配置自动加载

通过包含Composer生成的文件来配置自动加载。

include_once('vendor/autoload.php');

读取配置

要读取配置值,请使用配置文件路径实例化W6\Config\Reader类。

var $config = new \W6\Config\Reader( '/path/to/config.php' );

print_r($config->environment);
print_r($config->debug);
print_r($config->debug['level']);

高级配置

示例演示了PHP配置提供的可能性。

<?php
$conf = array();

/**
 * SECTION : ENVIRONMENT 
 * Defines the environment
 * Possible choices :
 * - development
 * - production
 */
$conf['environment'] = 'development';

/**
 * SECTION : DEBUG 
 * Debugging options
 */
$conf['debug'] = array();

    /**
     * Debug level
     * 0 - No messages
     * 1 - Show messages
     * 2 - Show messages + backtrace
     */
    $conf['debug']['level'] = $conf['environment'] == 'development' ? 2 : 0;

/**
 * SECTION : USER 
 * Include other configuration files
 */
$conf['user'] = include( './config/user.php' );

// Important, return the configuration array
return $conf;