Skip to content

Commit

Permalink
make it so!
Browse files Browse the repository at this point in the history
  • Loading branch information
saltandvinegarcrisps committed Aug 22, 2020
0 parents commit 5319bef
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vendor
bin
coverage
*.log
*.phar
*.cache
*.lock
48 changes: 48 additions & 0 deletions composer.json
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"
]
}
}
6 changes: 6 additions & 0 deletions phpspec.yml
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
8 changes: 8 additions & 0 deletions phpstan.neon
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
19 changes: 19 additions & 0 deletions readme.md
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']
```
25 changes: 25 additions & 0 deletions spec/Codin/Config/ConfigSpec.php
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');
}
}
24 changes: 24 additions & 0 deletions spec/Codin/Config/Loaders/ChainedLoaderSpec.php
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');
}
}
37 changes: 37 additions & 0 deletions spec/Codin/Config/Loaders/JsonFileLoaderSpec.php
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);
}
}
37 changes: 37 additions & 0 deletions spec/Codin/Config/Loaders/PhpFileLoaderSpec.php
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);
}
}
36 changes: 36 additions & 0 deletions src/Config.php
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);
}
}
22 changes: 22 additions & 0 deletions src/ConfigAccessInterface.php
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);
}
9 changes: 9 additions & 0 deletions src/ConfigException.php
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
{
}
Loading

0 comments on commit 5319bef

Please sign in to comment.