[新轮子] Laravel 扩展,Configuration 组件化,支持覆盖

config.svg?sanitize=true

Larvel组件化配置项灵活覆盖


English


特色

  • 支持组件配置,配置文件可以在任何地方。
  • 覆盖配置,灵活。

环境

php >= 7.1

安装

    composer require ofcold/configuration

说明

我们可能用这样一个场景,在开发Laravel组件时,需要一些配置,或多个配置项目。原有Laravel可能需要你去合并配置并且发布到根目录。
随着组件的增多config文件也会随之增多。

使用


use Ofcold\Configuration\LoaderConfiguration;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Config\Repository;

$loader = new LoaderConfiguration(
    $config = new Repository,
    new Filesystem
);

$loader->addNamespace('test', __DIR__ . '/tests/config');

print_r(json_encode($config->all()));
// print_r($config->get('test::test.foo') . "\r\n");

$loader->addNamespaceOverrides('test', __DIR__ . '/tests/overrides');

print_r(json_encode($config->all()));
// print_r($config->get('test::test.foo')  . "\r\n");

Results:

{
    "test::test":{
        "foo":"example"
    }
}

{
    "test::test":{
        "foo":"overrides"
    }
}

Larvel


use Ofcold\Configuration\LoaderConfiguration;

class Foo
{
    /**
     * Create an a new Foo instance.
     *
     * @param LoaderConfiguration $loader
     */
    public function __construct(LoaderConfiguration $loader)
    {
        $loader->addNamespace('test', '/config');
    }
}

OR test file.

    php test

Api

  • addNamespace(?string $namespace = null, string $directory) : void
  • addNamespaceOverrides($namespace, $directory) : void

如果您觉得有用,请点个赞!后面的日子我会大力支持开源。

Mujin
Mumujin
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!