kmucms / config
项目配置。配置变量按类名分组。每个类使用它们自己的配置变量。
dev-main
2021-03-02 16:36 UTC
Requires
- php: >=8.0.0
This package is auto-updated.
Last update: 2024-09-29 05:43:06 UTC
README
Configure a php project.
Every php-class gets their own configuration variables.
Configuration syntax is an nested php-array.
1. install
If you use composer, add this line to require block of composer.json .
┌─ composer.json ────────────────────────────────────────────────────────┐
{
...
"require": {
...
"kmucms/config": "@dev"
}
}
└────────────────────────────────────────────────────────────────────────┘
2. Init configuration class. Preffered on the same spot where you import composer's autoload.
┌─ index.php ────────────────────────────────────────────────────────────┐
require_once __DIR__ . '/vendor/autoload.php';
\kmucms\config\Config::init(__DIR__ . '/_config.php');
└────────────────────────────────────────────────────────────────────────┘
3. Create the configuration file on given position (in example above: __DIR__ . '/_config.php').
┌─ _config.php ──────────────────────────────────────────────────────────┐
<?php
return [
'global' => [
],
'class' => [
\my\namespace\my\ClassName::class => [
'value1' => 'v1',
'value2' => 'v2',
'name_n' => 'nxyz',
],
],
];
└────────────────────────────────────────────────────────────────────────┘
4. use it in classes
┌─ ClassName.php ────────────────────────────────────────────────────────┐
<?php
namespace my\namespace\my;
class ClassName{
public function myFunction(){
$conf = \kmucms\config\Config::getInstanceByClass(self::class);
$a = $conf->getConf('value1'); // = 'v1'
// ...
}
└────────────────────────────────────────────────────────────────────────┘