Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0 #1

Merged
merged 6 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# PHP Config
# PHP Container PSR-11

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/config/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/config/?branch=master)
[![Build Status](https://travis-ci.org/byjg/config.svg?branch=master)](https://travis-ci.org/byjg/config)

A very basic and minimalist component for config management and dependency injection.
A very basic and minimalist PSR-11 implementation for config management and dependency injection.

## How it Works?

The container is created based on your current environment (dev, homolog, test, live, ...) defined in array files;

See below how to setup:

### Setup files:

Expand Down Expand Up @@ -63,63 +66,67 @@ return [
'property1' => 'string',
'property2' => true,
'property3' => function () {
return xxxxxx;
return 'xxxxxx';
},
'propertyWithArgs' => function ($p1, $p2) {
return xxxxxx;
return 'xxxxxx';
},
];
```

You can inherit properties from another environment:

**config-live.php**
```php
<?php

$config = \ByJG\Util\Config::inherit('homolog');

$config['property2'] = false;

return $config;
return [
'property2' => false
];
```

### Use in your PHP Code

The code below will get a property from the defined environment:
Create the Definition:

```php
<?php
$property = \ByJG\Util\Config::get('property1');
$definition = (new \ByJG\Config\Definition())
->addEnvironment('homolog') // This will setup the HOMOLOG environment
->addEnvironment('live') // This will setup the LIVE environenment inherited HOMOLOG
->inheritFrom('hormolog')
->setCache($somePsr16Implementation); // This will cache the result;
```

By default if the property does not exists an error will be throwed.
If you want to get null instead throw an error use:
The code below will get a property from the defined environment:

```php
<?php
$property = \ByJG\Util\Config::get('property-not-exists', false);
$container = $definition->build();
$property = $container->get('property2');
```

Or you can pass parameters to the closure function:
If the property does not exists an error will be throwed.


If the property is a closure, you can get and run passing parameters (this is not a PSR-11 implementation):

```php
<?php
$property = \ByJG\Util\Config::getArgs('propertyWithArgs', 'value1', 'value2');
$property = \ByJG\Util\Config::getArgs('propertyWithArgs', ['value1', 'value2']);
$container = $definition->build();
$property = $container->getClosure('propertyWithArgs', 'value1', 'value2');
$property = $container->getClosure('propertyWithArgs', ['value1', 'value2']);
```

### Checking current environment

```php
<?php
\ByJG\Util\Config::getCurrentEnv();
$defintion->getCurrentEnv();
```

## Install

```
composer require "byjg/config=1.0.*"
composer require "byjg/config=2.0.*"
```

## Tests
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "byjg/config",
"description": "A very basic and minimalist PSR-11 implementation for config management and dependency injection.",
"license": "MIT",
"require": {
"psr/container": "1.0.*",
"psr/simple-cache": "1.0.*",
"php": ">=5.6"
},
"require-dev": {
"byjg/cache-engine": "4.0.*"
},
"prefer-stable": true,
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"ByJG\\Util\\": "src/"
"ByJG\\Config\\": "src/"
}
}
}
10 changes: 4 additions & 6 deletions config/config-test2.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

$config = \ByJG\Util\Config::inherit('test');

$config['property2'] = false;
$config['property4'] = 'new';

return $config;
return [
'property2' => false,
'property4' => 'new',
];
80 changes: 0 additions & 80 deletions src/Config.php

This file was deleted.

65 changes: 65 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ByJG\Config;

use ByJG\Config\Exception\NotFoundException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

class Container implements ContainerInterface
{
private $config = [];

public function __construct($config)
{
$this->config = $config;
}

/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
* @return mixed Entry.
*/
public function get($id)
{
if (!$this->has($id)) {
throw new NotFoundException("The key '$id'' does not exists");
}

return $this->config[$id];
}

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
* @return bool
*/
public function has($id)
{
return isset($this->config[$id]);
}

/**
* @param $id
* @param $args
* @return mixed
*/
public function getClosure($id, $args = null)
{
$closure = $this->get($id);

if (is_array($args)) {
return call_user_func_array($closure, $args);
}

return call_user_func_array($closure, array_slice(func_get_args(), 1));
}
}
Loading