phlib/console-configuration

控制台配置助手实现。

2.0.1 2023-10-15 07:25 UTC

This package is auto-updated.

Last update: 2024-08-31 00:33:34 UTC


README

Code Checks Codecov Latest Stable Version Total Downloads Licence

控制台配置助手实现。

安装

通过 Composer

$ composer require phlib/console-configuration

配置助手

-c path/to/config.php 参数添加到控制台应用程序中,使其对所有命令都易于访问。这对于依赖于从选项中指定配置的第三方库非常有用。

基本用法

// your usual cli setup script

use Phlib\ConsoleConfiguration\Helper\ConfigurationHelper;

$app = new Application('my-cli');
$app->setCommands(['...']);
$helper = ConfigurationHelper::initHelper(
    $app,
    $default = ['host' => 'localhost'],
    $options = []
);
$app->run();
class MyCommand extends Command
{
    '...'

    protected function createMyObjectInstance()
    {
        $config = $this->getHelper('configuration')->fetch();
        return new MyObjectInstance($config);
    }
}

选项

您可以通过 initHelper 静态方法指定一些选项来设置助手。

ConfigurationHelper::initHelper($app, null, [
    'name' => 'config-option',
    'filename' => 'my-cli-config.php',
]);

默认值

如果没有指定默认值(null),则获取方法返回 false。您可以使用 setDefault 方法或通过 initHelper 静态方法指定默认配置。

$helper->setDefault(['host' => 'localhost']);

或者

ConfigurationHelper::initHelper(
    $app,
    ['host' => 'localhost'],
    ['filename' => 'service-name.php']
);

用例:无命令行选项的自动检测

$app = new Application('my-cli');
$app->setCommands(['...']);
$app->getHelperSet()->set(new ConfigurationHelper('config', 'my-config.php'));
$app->run();

许可证

此软件包是免费软件:您可以在自由软件基金会发布的GNU Lesser General Public License的条款下重新分发和/或修改它,许可证版本为3,或(根据您的选择)任何更高版本。

本程序分发时附带希望它将是有用的,但没有任何保证;甚至没有关于其可销售性或适用于特定目的的暗示性保证。有关详细信息,请参阅GNU Lesser General Public License。

您应该已随本程序收到GNU Lesser General Public License的副本。如果没有,请参阅https://gnu.ac.cn/licenses/