Skip to content

Commit

Permalink
PHPDocs and Readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2z committed Sep 21, 2018
1 parent b10802b commit 180aee9
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 4 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,64 @@

A lightweight PHP static class with zero dependencies.

Supports multiple configs and multidimensional arrays.

Built-in support for PHP, INI and JSON files.


## Requirements
- PHP 7.0 +


## Examples

```php
require 'path/to/vendor/autoload.php';
use xy2z\LiteConfig\LiteConfig as Config;
```

#### Array
```php
Config::loadArray([
'version' => '1.0',
'app' => [
'name' => 'Example'
]
]);

echo Config::get('version'); # 1.0
echo Config::get('app'); # Array('name' => 'Example')
echo Config::get('app.name'); # Example
```

#### Directory
```php
# config/settings.php
return [
'app_name' => 'Example',
];

# index.php
Config::loadDir('config/', true);
echo Config::get('settings.app_name'); # key is 'filename.key'
```

#### Single file
```php
# Without prefix
Config::loadFile('config/settings.php');
echo Config::get('key');

# With prefix
Config::loadFile('config/db.ini', 'prefix');
echo Config::get('prefix.key');
```


## Methods
- `get(string $key)` Get value of key.
- `all()` Returns a complete array.
- `exists(string $key)` Does key exist?
- `loadDir(string $path, bool $prefix_filename = false, string $prefix = null)` Loads all files in dir.
- `loadFile(string $path, bool $prefix_filename = false, string $custom_prefix = null)` Load a single file.
- `loadArray(array $array, string $prefix = null)` Loads a php array.
65 changes: 61 additions & 4 deletions src/LiteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ abstract class LiteConfig {
*/
public static $ini_scanner_mode = INI_SCANNER_TYPED;

/**
* Loads all config files in a directory.
*
* @param string $path Path to dir
* @param boolean $prefix_filename Prefix the key with the filename
* @param string $custom_prefix Prefix the key
*
* @return void
*/
public static function loadDir(string $path, $prefix_filename = false, $custom_prefix = null) {
$glob = glob($path . '/*.*');

Expand All @@ -37,6 +46,14 @@ public static function loadDir(string $path, $prefix_filename = false, $custom_p
}
}

/**
* Load config file
*
* @param string $path Path to file
* @param boolean $prefix_filename Prefix the key with the filename
* @param string $custom_prefix Prefix the key
*
*/
public static function loadFile(string $path, bool $prefix_filename = false, string $custom_prefix = null) {
$pathinfo = pathinfo($path);

Expand All @@ -52,6 +69,14 @@ public static function loadFile(string $path, bool $prefix_filename = false, str
static::loadArray($content, $prefix);
}

/**
* Get file content as php array
*
* @param string $path Path to file
* @param array $pathinfo pathinfo() array
*
* @return array
*/
protected static function getFileContent(string $path, array $pathinfo) {
switch ($pathinfo['extension']) {
case 'php':
Expand All @@ -68,8 +93,16 @@ protected static function getFileContent(string $path, array $pathinfo) {
}


public static function loadArray(array $settings, string $prefix = null) {
foreach ($settings as $key => $val) {
/**
* Load an array into the config
*
* @param array $data Data to load.
* @param string $prefix Prefix the keys (optional)
*
* @return void
*/
public static function loadArray(array $data, string $prefix = null) {
foreach ($data as $key => $val) {
if (!is_null($prefix)) {
$key = $prefix . '.' . $key;
}
Expand All @@ -78,27 +111,51 @@ public static function loadArray(array $settings, string $prefix = null) {
}
}

/**
* Add data to the config
*
* @param string $key Key name
* @param mixed $value Value
*/
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;
}

/**
* Get by key
*
* @param string $key Key
*
* @return mixed Value
*/
public static function get(string $key) {
return static::$data[$key] ?? null;
return static::$data[$key] ?? null;
}

/**
* Get complete config as array
*
* @return array
*/
public static function all() : array {
return static::$data;
}

/**
* Check if key exists
*
* @param string $key Key name
*
* @return bool
*/
public static function exists(string $key) : bool {
return isset(self::$data[$key]);
}
Expand Down
Empty file added test/config/.ignore-this
Empty file.
2 changes: 2 additions & 0 deletions test/config/conf.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[app]
name_ini = ok
6 changes: 6 additions & 0 deletions test/config/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app": {
"name": false
}

}
7 changes: 7 additions & 0 deletions test/config/conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'app_name' => 'phpFileLOL',

];
51 changes: 51 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

require '../src/LiteConfig.php';

// use xy2z\Liteconfig\Liteconfig as Config;
use xy2z\Liteconfig\LiteConfig as Config;

$start = microtime(true);


# PHP array
// Config::loadArray([
// 'app_name' => 'Example',
// ]);

# LoadFile
// Config::loadFile('config/conf.php', true);
// Config::loadFile('config/conf.ini', true);
// Config::loadFile('conf.json', true);

# loadDir
// Config::loadDir('config', true);

# JSON
// $json = file_get_contents('conf.json');
// Config::loadArray(json_decode($json, true));

# INI
// Config::loadArray(parse_ini_file('conf.ini', true));


Config::loadArray([
'version' => 1.5,
'app' => [
'name' => 'Example'
]
]);

var_dump(Config::get('version')); # 1.0
var_dump(Config::get('app')); # Array('name' => 'Example')
var_dump(Config::get('app.name')); # Example


# RESULT
echo PHP_EOL . '------------' . PHP_EOL;
var_dump(
Config::get('conf.app_name')
);

echo PHP_EOL;
var_dump(microtime(true) - $start);

0 comments on commit 180aee9

Please sign in to comment.