一个基于hassankhan/config的精简中间件,用于从不同的文件中读取配置

v0.1.2 2022-03-26 15:11 UTC

This package is auto-updated.

Last update: 2024-08-26 23:44:57 UTC


README

Latest version Build Status Coverage Status Quality Score Total Downloads PSR2 Conformance

一个用于Slim框架的文件配置加载器,支持PHP、INI、XML、JSON和YML文件。它内部使用hassankhan/config

安装

通过Composer

$ composer require davidepastore/slim-config

需要Slim 3.0.0或更高版本。

用法

在大多数情况下,您希望为单个路由注册DavidePastore\Slim\Config,但是,作为中间件,您也可以为所有路由注册它。

按路由注册

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['config'] = function () {
  //Create the configuration
  return new \DavidePastore\Slim\Config\Config('config.json');
};

$app->get('/api/myEndPoint',function ($req, $res, $args) {
    //Here you have your configuration
    $config = $this->config->getConfig();
    $secret = $config->get('security.secret');
})->add($container->get('config'));

$app->run();

为所有路由注册

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register provider
$container['config'] = function () {
  //Create the configuration
  return new \DavidePastore\Slim\Config\Config('config.json');
};

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add($container->get('config'));

$app->get('/foo', function ($req, $res, $args) {
  //Here you have your configuration
  $config = $this->config->getConfig();
  $secret = $config->get('security.secret');
});

$app->post('/bar', function ($req, $res, $args) {
  //Here you have your configuration
  $config = $this->config->getConfig();
  $ttl = $config->get('app.timeout', 3000);
});

$app->run();

有哪些好处?

按路由使用的情况下,只有在调用给定的路由时才会从文件系统加载配置。在其他情况下(所有路由),配置应该是通用的,并在整个路由中使用,因为它在每次请求中都会被读取。

这只是冰山一角!

有关更多信息,您可以阅读hassankhan/config的文档这里

测试

$ phpunit

贡献

请参阅CONTRIBUTING以获取详细信息。

鸣谢