Skip to content

Commit

Permalink
Implemented call to 'custom_handler' on unsupported file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2z committed Oct 3, 2021
1 parent 4886254 commit d61cb90
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ echo Config::get('db.key');
```

#### YAML
To load a single file as YAML see below. If you need to use `loadDir()` with yaml (or other) files, then read about "Custom Handler" below.

```bash
composer require symfony/yaml
```
Expand All @@ -76,8 +78,35 @@ Config::loadArray(Yaml::parseFile(__DIR__ . '/config/file.yml'));
echo Config::get('key');
```

#### Custom Handler
A custom handler can be used for file extensions other than the built in (.php, .json and .ini). This will automatically work when using the `loadFile()` and `loadDir()` functions.

Here's how you use the static `custom_handler()` function if you want YAML support.

```php
use xy2z\LiteConfig\LiteConfig;
use Symfony\Component\Yaml\Yaml;

class CustomLiteConfig extends LiteConfig {

protected static function custom_handler(string $extension, string $path) {
if (($extension === 'yml') || ($extension === 'yaml')) {
return Yaml::parseFile($path);
}

// Handle other extensions here...
}

}

CustomLiteConfig::loadDir(__DIR__ . '/config', true);
var_dump(CustomLiteConfig::all());
```

If you want to modify the existing handling of all files, you can overwrite the complete `getFileContent()` function in your child class, which is used by `loadDir()` and `loadFile()`.


## Methods
## Public Methods
- `get(string $key, $default = null)` Get value of key.
- `all()` Returns a complete array.
- `exists(string $key)` Does key exist?
Expand Down
23 changes: 23 additions & 0 deletions src/LiteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ protected static function getFileContent(string $path, array $pathinfo) {

case 'json':
return json_decode(file_get_contents($path), true);

default:
// Allow custom handler for other filename extensions.
$custom_handler = call_user_func_array(array(get_called_class(), 'custom_handler'), array(
'extension' => $pathinfo['extension'],
'path' => $path,
));

// Only return if success, otherwise it will throw exception at end of this method.
if (($custom_handler !== null) && ($custom_handler !== false)) {
return $custom_handler;
}
}

throw new \Exception('Unsupported filetype: ' . $pathinfo['extension']);
Expand Down Expand Up @@ -160,4 +172,15 @@ public static function all(): array
public static function exists(string $key): bool {
return isset(self::$data[$key]);
}

/**
* This is a placeholder for child classes.
* To make sure they follow the "interface" of this function (protected, arguments, etc.)
*
* @param string $extension File extension (eg. "php")
* @param string $path Full file path
*/
protected static function custom_handler(string $extension, string $path) {
return false;
}
}
3 changes: 3 additions & 0 deletions tests/LiteConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

// This should be called using "phpunit" command.


namespace xy2z\LiteConfigTests;

use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit d61cb90

Please sign in to comment.