jaxwilko/simple-config

用于创建PHP配置文件的简单流畅接口

v1.0.0 2019-11-15 23:22 UTC

This package is auto-updated.

Last update: 2024-09-24 03:48:41 UTC


README

存档:请使用winter/laravel-config-writer代替。

SimpleConfig

本软件包旨在提供创建PHP配置文件的简单接口。

安装

通过以下方式将软件包添加到您的项目中:

composer require jaxwilko/simple-config

使用方法

<?php

use SimpleConfig\Compiler;
use SimpleConfig\Section;

$compiler = new Compiler();
$section = new Section();
$section->title = 'Database';
$section->key = 'database';
$section->comment = 'This is where your database config goes.';
$section->value = ['mysql' => ['host' => 'localhost']];
$compiler->addSection($section);
echo $compiler->render();

将返回

<?php

return [
    /*
    |----------------------------------------
    | Database
    |----------------------------------------
    | This is where your database config goes.
    |
    */

    'database' => [
        'mysql' => [
            'host' => 'localhost',
        ],
    ],
];

达到相同效果的一种更简单的方式是

<?php

$compiler = new Compiler();
$compiler->addSection(new Section([
    'title'     => 'Database',
    'key'       => 'database',
    'comment'   => 'This is where your database config goes.',
    'value'     => ['mysql' => ['host' => 'localhost']],
]));
echo $compiler->render();

可以省略所有内容,例如只保留注释和标题

$compiler = new Compiler();
$compiler->addSection(new Section([
    'title' => 'Database',
    'comment' => 'This is where your database config goes.',
]));
echo $compiler->render();

将返回

<?php

return [
    /*
    |----------------------------------------
    | Database
    |----------------------------------------
    | This is where your database config goes.
    |
    */

];

或者,没有注释和标题

<?php

$compiler = new Compiler();
$compiler->addSection(new Section([
    'key' => 'database',
    'value' => ['mysql' => ['host' => 'localhost']],
]));
echo $compiler->render();

将返回

<?php

return [
    'database' => [
        'mysql' => [
            'host' => 'localhost',
        ],
    ],
];

可以通过链式调用添加多个部分

<?php

$compiler->addSection(new Section([
        'title' => 'Section one',
        'key' => 'section_one',
        'comment' => 'This is a section',
        'value' => [
            'foo' => 'bar',
            'bar' => [
                'foo'
            ]
        ]
    ]))
    ->addSection(new Section([
        'title' => 'Section two',
        'key' => 'section_two',
        'comment' => 'This is another section',
        'value' => [
            'bar' => 'foo',
            'foo' => [
                'bar'
            ]
        ]
    ]));

可以通过在方法前加@@来传递函数。例如:

<?php

$compiler->addSection(new Section([
    'title' => 'Section',
    'key' => 'section',
    'comment' => 'This is a section',
    'value' => [
        'foo' => '@@env(\'VALUE\')',
    ]
]));

将得到以下结果:

<?php

return [
    /*
    |----------------------------------------
    | Section
    |----------------------------------------
    | This is a section
    |
    */

    'section' => [
        'foo' => env('VALUE'),
    ],
];

可以通过以下方式设置编译器选项:

<?php

// define the comment length
$compiler->option('length', 80);
// use spaces over tabs
$compiler->option('tabs', false);
// define the indentation length
$compiler->option('indent', 4);