-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b10802b
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# LiteConfig | ||
|
||
A lightweight PHP static class with zero dependencies. | ||
|
||
## Requirements | ||
- PHP 7.0 + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "xy2z/lite-config", | ||
"description": "A lightweight PHP static class with zero dependencies.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "xy2z", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"xy2z\\LiteConfig\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace xy2z\LiteConfig; | ||
|
||
/** | ||
* Lite Config | ||
* | ||
*/ | ||
abstract class LiteConfig { | ||
|
||
/** | ||
* Config data | ||
* | ||
* @var array | ||
*/ | ||
protected static $data = []; | ||
|
||
/** | ||
* Argument used in file_parse_ini() | ||
* | ||
* @var boolean | ||
*/ | ||
public static $ini_process_sections = true; | ||
|
||
/** | ||
* Argument used in file_parse_ini() | ||
* | ||
* @var integer | ||
*/ | ||
public static $ini_scanner_mode = INI_SCANNER_TYPED; | ||
|
||
public static function loadDir(string $path, $prefix_filename = false, $custom_prefix = null) { | ||
$glob = glob($path . '/*.*'); | ||
|
||
foreach ($glob as $filepath) { | ||
static::loadFile($filepath, $prefix_filename, $custom_prefix); | ||
} | ||
} | ||
|
||
public static function loadFile(string $path, bool $prefix_filename = false, string $custom_prefix = null) { | ||
$pathinfo = pathinfo($path); | ||
|
||
// Add prefix | ||
$prefix = ($prefix_filename) ? $pathinfo['filename'] : null; | ||
|
||
if (!is_null($custom_prefix)) { | ||
$prefix = $custom_prefix . '.' . $prefix; | ||
} | ||
|
||
// Load file content | ||
$content = static::getFileContent($path, $pathinfo); | ||
static::loadArray($content, $prefix); | ||
} | ||
|
||
protected static function getFileContent(string $path, array $pathinfo) { | ||
switch ($pathinfo['extension']) { | ||
case 'php': | ||
return require($path); | ||
|
||
case 'ini': | ||
return parse_ini_file($path, static::$ini_process_sections, static::$ini_scanner_mode); | ||
|
||
case 'json': | ||
return json_decode(file_get_contents($path), true); | ||
} | ||
|
||
throw new \Exception('Unsupported filetype: ' . $pathinfo['extension']); | ||
} | ||
|
||
|
||
public static function loadArray(array $settings, string $prefix = null) { | ||
foreach ($settings as $key => $val) { | ||
if (!is_null($prefix)) { | ||
$key = $prefix . '.' . $key; | ||
} | ||
|
||
static::add($key, $val); | ||
} | ||
} | ||
|
||
protected static function add(string $key, $value) { | ||
if (is_array($value)) { | ||
foreach ($value as $k2 => $v2) { | ||
$key_path = $key . '.' . $k2; | ||
static::add($key_path, $v2); | ||
} | ||
return; | ||
} | ||
|
||
// Set. | ||
static::$data[$key] = $value; | ||
} | ||
|
||
public static function get(string $key) { | ||
return static::$data[$key] ?? null; | ||
} | ||
|
||
public static function all() : array { | ||
return static::$data; | ||
} | ||
|
||
public static function exists(string $key) : bool { | ||
return isset(self::$data[$key]); | ||
} | ||
} |