alius/config 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alius/config

Composer 安装命令:

composer require alius/config

包简介

Include php array files

README 文档

README

Build Status

Alius Config

Alius Config is just a simple composer package, all it can do is include php array files.

Never commit any sensitive information into your version control!

Basic usage:

<?php

return [
    'test' => 'this is a test',
];
$config = new \Alius\Config\Config('path_to_file.php');
print $config->get('test'); // => this is a test

Multiple files, directories

You can include multiple files:

$config = new \Alius\Config\Config([
    'path_to_file1.php',
    'path_to_file2.php',
]);

One or more directories (includes the subdirectories and files recursively):

$config = new \Alius\Config\Config([
    'path_to_directory',
    'path_to_directory2',
]);

All of the files must use php as extension and they must return array.

Last file wins

If you include two files with the same variable, the last file always wins:

<?php
// file1.php
return [
    'test' => 'one',
];
<?php
// file2.php
return [
    'test' => 'two',
];
$config = new \Alius\Config\Config(['file1.php', 'file2.php']);
print $config->get('test'); // => two

It's a good idea to name your files starting with numbers:

10-something.php
20-something_more.php

Get the source file of a variable

Including lots of files could be confusing so you can get the source file of a variable:

print $config->getOrigin('test'); // => file2.php

Required variables

You can mark a variables required, if none of the included files has this variable then an exception will be thrown:

$config = new \Alius\Config\Config('path_to_file.php', ['test']);
var_dump($config->isRequired('test')); // => true
var_dump($config->isRequired('test2')); // => false

Get the variables in one array

You can get an array with all the variables:

$config->compile();

Error thrown, if:

  • any of the files doesn't exist or not readable or has extension other than php
  • if you try to get a non-existing variable ($config->get('not_there'); or $config->getOrigin('not_there');)
  • when a required variable is not found in any of the included files

The point is that it should fail fast so you can fix everything before you commit your changes or deploy.

Recommended usage

Never commit any sensitive information into your version control!

You should create a directory for all of your non-sensitive data, and another one for sensitive data (and exclude that one from version control).

For example:

config/non-sensitive-data1.php
config/non-sensitive-data2.php
exclude_this_dir_from_vc/database.php

Or you can still use dotenv or similar for sensitive data, for example Laravel’s env function with defaults:

return [
    'db_host' => env('db_host', 'localhost'),
    'db_user' => env('db_user', 'homestead'),
    'db_pass' => env('db_pass', 'secret'),
    'db_name' => env('db_name', 'homestead'),
];

What’s wrong with dotenv?

Nothing, but sometimes it’s just not enough.

With this package you can include files conditionally, override variables, you can use bool, int, float, arrays, constants, Closures, even objects or resources.

Or you can extend it, create your DatabaseConfig class so you can inject it as a dependency.

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2015-12-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固