bauhaus/config

简单的配置容器

v0.1.0 2018-02-11 11:30 UTC

This package is auto-updated.

Last update: 2024-09-23 11:05:11 UTC


README

Build Status Coverage Status Codacy Badge

Latest Stable Version Latest Unstable Version Total Downloads composer.lock

警告! 该软件包不会关注 v0.* 的向后兼容性。

Bauhaus Config

Bauhaus Config 旨在通过 PSR-11 的容器实现提供一个简单的方式来加载和验证配置。

待办事项

这个软件包还不完整。缺少的功能包括

  • 验证配置架构(使用 JSON 架构)
  • yaml 加载(将在新的仓库 bauhaus/config-yaml 中完成)

动机

当应用内部某个配置项缺失时,应用程序中的某些功能出现故障,这非常令人烦恼。

为了防止这种情况,Bauhaus Config 在加载配置时进行验证(正在实施)。

使用方法

<?php

use Psr\Container\NotFoundExceptionInterface as PsrNotFoundException;
use Psr\Container\ContainerInterface as PsrContainer;
use Bauhaus\Config;
use Bauhaus\Config\NotFoundException;

$config = new Config([
    'instrument' => 'bass',
    'pokemons' => ['charmander', 'pikachu'],
    'books' => [
        'study' => ['Clean Code', 'GOOS', 'Design Patterns'],
    ],
]);

$config instanceof PsrContainer; // true

$config->has('instrument'); // true
$config->has('pokemons'); // true
$config->has('books.study'); // true
$config->has('cars'); // false
$config->has('travels.asia'); // false

$config->get('instrument'); // 'bass'
$config->get('pokemons'); // ['charmander', 'pikachu']
$config->get('books'); // Config(['study' => ['Clean Code', 'GOOS', 'Design Patterns']])
$config->get('cars'); // throw NotFoundException implementing PsrNotFoundException
$config->get('travels.asia'); // throw NotFoundException implementing PsrNotFoundException

$config->get('books')->has('study'); // true

安装

使用 Composer 进行安装

$ composer require bauhaus/config

编码标准和测试

此代码有两级测试

  1. 编码标准(使用 PHPCS 和 PSR-2 标准)

    $ composer run test:cs
  2. 单元测试(使用 PHPUnit)

    $ composer run test:unit

要一次性运行所有测试

$ composer run tests