Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2z committed Sep 21, 2018
0 parents commit b10802b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
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 +
17 changes: 17 additions & 0 deletions composer.json
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/"
}
}
}
105 changes: 105 additions & 0 deletions src/LiteConfig.php
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]);
}
}

0 comments on commit b10802b

Please sign in to comment.