-
Notifications
You must be signed in to change notification settings - Fork 0
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 5319bef
Showing
17 changed files
with
529 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,12 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.{js,yml}] | ||
indent_size = 2 |
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,7 @@ | ||
vendor | ||
bin | ||
coverage | ||
*.log | ||
*.phar | ||
*.cache | ||
*.lock |
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,48 @@ | ||
{ | ||
"name": "codin/config", | ||
"description": "Basic config file load supports PHP ans Json files", | ||
"license": "Apache-2.0", | ||
"type": "library", | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"authors": [ | ||
{ | ||
"name": "Kieron", | ||
"email": "hello@madebykieron.co.uk", | ||
"homepage": "http://madebykieron.co.uk", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.3" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "@stable", | ||
"phpspec/phpspec": "@stable", | ||
"phpstan/phpstan": "@stable" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Codin\\Config\\": "src/" | ||
} | ||
}, | ||
"config": { | ||
"preferred-install": "dist", | ||
"sort-packages": true, | ||
"bin-dir": "bin" | ||
}, | ||
"scripts": { | ||
"psr": [ | ||
"./bin/php-cs-fixer fix . --allow-risky=yes --rules=@PSR2,no_unused_imports,ordered_imports,ordered_interfaces,single_quote,trailing_comma_in_multiline_array" | ||
], | ||
"test": [ | ||
"phpstan analyse", | ||
"phpspec run" | ||
], | ||
"uninstall": [ | ||
"rm -rf ./bin", | ||
"rm -rf ./vendor", | ||
"rm ./composer.lock" | ||
] | ||
} | ||
} |
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 @@ | ||
formatter.name: pretty | ||
stop_on_failure: true | ||
code_generation: false | ||
suites: | ||
default: | ||
src_path: ./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,8 @@ | ||
parameters: | ||
level: max | ||
paths: | ||
- %currentWorkingDirectory%/src | ||
bootstrapFiles: | ||
- %currentWorkingDirectory%/vendor/autoload.php | ||
inferPrivatePropertyTypeFromConstructor: true | ||
checkMissingIterableValueType: false |
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,19 @@ | ||
# Config | ||
|
||
Lightweight configuration file loader that supports PHP and JSON files | ||
|
||
```shell | ||
$ cat path/to/config/foos.php | ||
<?php return ['foo' => 'bar']; | ||
|
||
$ cat path/to/config/bars.json | ||
{"bar":"baz"} | ||
``` | ||
|
||
```php | ||
$config = Codin\Config\Config::create('path/to/config'); | ||
$config->has('foos.foo'); // true | ||
$config->get('foos.foo'); // "bar" | ||
$config->has('bars'); // true | ||
$config->get('bars'); // ['bar' => 'baz'] | ||
``` |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Codin\Config; | ||
|
||
use Codin\Config\ConfigAccessInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ConfigSpec extends ObjectBehavior | ||
{ | ||
public function it_should_check_items(ConfigAccessInterface $loader) | ||
{ | ||
$this->beConstructedWith($loader); | ||
$loader->has('foo')->shouldBeCalled()->willReturn(true); | ||
$this->has('foo')->shouldReturn(true); | ||
} | ||
|
||
public function it_should_get_items(ConfigAccessInterface $loader) | ||
{ | ||
$this->beConstructedWith($loader); | ||
$loader->get('foo', null)->shouldBeCalled()->willReturn('bar'); | ||
$this->get('foo')->shouldReturn('bar'); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace spec\Codin\Config\Loaders; | ||
|
||
use Codin\Config\ConfigAccessInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ChainedLoaderSpec extends ObjectBehavior | ||
{ | ||
public function it_should_check_items(ConfigAccessInterface $loader) | ||
{ | ||
$this->beConstructedWith([$loader]); | ||
$loader->has('foo')->shouldBeCalled()->willReturn(true); | ||
$this->has('foo')->shouldReturn(true); | ||
} | ||
|
||
public function it_should_get_items(ConfigAccessInterface $loader) | ||
{ | ||
$this->beConstructedWith([$loader]); | ||
$loader->has('foo')->shouldBeCalled()->willReturn(true); | ||
$loader->get('foo', null)->shouldBeCalled()->willReturn('bar'); | ||
$this->get('foo')->shouldReturn('bar'); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace spec\Codin\Config\Loaders; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
class JsonFileLoaderSpec extends ObjectBehavior | ||
{ | ||
public function it_should_check_items() | ||
{ | ||
$path = sys_get_temp_dir(); | ||
$name = sprintf('phpspec-test-%u', random_int(1, PHP_INT_MAX)); | ||
$filepath = sprintf('%s/%s.json', $path, $name); | ||
|
||
file_put_contents($filepath, '{"foo":"bar"}'); | ||
|
||
$this->beConstructedWith($path); | ||
$this->has($name)->shouldReturn(true); | ||
$this->has(sprintf('%s.%s', $name, 'foo'))->shouldReturn(true); | ||
|
||
unlink($filepath); | ||
} | ||
|
||
public function it_should_get_items() | ||
{ | ||
$path = sys_get_temp_dir(); | ||
$name = sprintf('phpspec-test-%u', random_int(1, PHP_INT_MAX)); | ||
$filepath = sprintf('%s/%s.json', $path, $name); | ||
|
||
file_put_contents($filepath, '{"foo":"bar"}'); | ||
|
||
$this->beConstructedWith($path); | ||
$this->get($name)->shouldReturn(['foo' => 'bar']); | ||
|
||
unlink($filepath); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace spec\Codin\Config\Loaders; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
class PhpFileLoaderSpec extends ObjectBehavior | ||
{ | ||
public function it_should_check_items() | ||
{ | ||
$path = sys_get_temp_dir(); | ||
$name = sprintf('phpspec-test-%u', random_int(1, PHP_INT_MAX)); | ||
$filepath = sprintf('%s/%s.php', $path, $name); | ||
|
||
file_put_contents($filepath, "<?php return ['foo' => 'bar'];"); | ||
|
||
$this->beConstructedWith($path); | ||
$this->has($name)->shouldReturn(true); | ||
$this->has(sprintf('%s.%s', $name, 'foo'))->shouldReturn(true); | ||
|
||
unlink($filepath); | ||
} | ||
|
||
public function it_should_get_items() | ||
{ | ||
$path = sys_get_temp_dir(); | ||
$name = sprintf('phpspec-test-%u', random_int(1, PHP_INT_MAX)); | ||
$filepath = sprintf('%s/%s.php', $path, $name); | ||
|
||
file_put_contents($filepath, "<?php return ['foo' => 'bar'];"); | ||
|
||
$this->beConstructedWith($path); | ||
$this->get($name)->shouldReturn(['foo' => 'bar']); | ||
|
||
unlink($filepath); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codin\Config; | ||
|
||
class Config implements ConfigAccessInterface | ||
{ | ||
/** | ||
* @var ConfigAccessInterface | ||
*/ | ||
protected $loader; | ||
|
||
final public function __construct(ConfigAccessInterface $loader) | ||
{ | ||
$this->loader = $loader; | ||
} | ||
|
||
public static function create(string $path): ConfigAccessInterface | ||
{ | ||
return new static(new Loaders\ChainedLoader([ | ||
new Loaders\JsonFileLoader($path), | ||
new Loaders\PhpFileLoader($path), | ||
])); | ||
} | ||
|
||
public function has(string $key): bool | ||
{ | ||
return $this->loader->has($key); | ||
} | ||
|
||
public function get(string $key, $default = null) | ||
{ | ||
return $this->loader->get($key, $default); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codin\Config; | ||
|
||
interface ConfigAccessInterface | ||
{ | ||
/** | ||
* Key exists in config file | ||
*/ | ||
public function has(string $key): bool; | ||
|
||
/** | ||
* Find key from config file | ||
* | ||
* @param string $key | ||
* @param mixed $default | ||
* @return mixed | ||
*/ | ||
public function get(string $key, $default = null); | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codin\Config; | ||
|
||
class ConfigException extends \ErrorException | ||
{ | ||
} |
Oops, something went wrong.