Dependencies

This commit is contained in:
2021-03-25 21:23:29 -03:00
parent 15e1eecc76
commit 5e0e7d1d55
76 changed files with 1687 additions and 0 deletions

13
aldarien/config/.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
composer.phar
/vendor/
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
config
# Eclipse IDE
.settings
.buildpath
.project

View File

@ -0,0 +1,3 @@
language: php
php: '7.1'
install: composer update

21
aldarien/config/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Aldarien
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,4 @@
# config
Config module that recovers configuration files
[![Build Status](https://travis-ci.org/Aldarien/config.svg?branch=1.0.1)](https://travis-ci.org/Aldarien/config)

View File

@ -0,0 +1,31 @@
<?php
namespace App\Contract;
use App\Definition\Contract;
use App\Service\Config AS ConfigService;
class Config
{
use Contract;
protected static function newInstance()
{
return new ConfigService();
}
public static function get($name = null)
{
$instance = self::getInstance();
return $instance->get($name);
}
public static function set($name, $value)
{
$instance = self::getInstance();
return $instance->set($name, $value);
}
public static function addFile($filename)
{
$instance = self::getInstance();
return $instance->loadFile($filename);
}
}
?>

View File

@ -0,0 +1,21 @@
<?php
namespace App\Contract;
use Symfony\Component\Yaml\Yaml;
class YamlWrapper
{
public static function load($filename, $flags = 0)
{
return self::parse(file_get_contents($filename), $flags);
}
public static function parse($input, $flags = 0)
{
return Yaml::parse($input, $flags);
}
public static function dump($array, $inline = 2, $indent = 4, $flags = 0)
{
return Yaml::dump($array, $inline, $indent, $flags);
}
}
?>

View File

@ -0,0 +1,9 @@
<?php
function config($name = null, $value = null) {
if ($value == null) {
return App\Contract\Config::get($name);
} else {
return App\Contract\Config::set($name, $value);
}
}
?>

View File

@ -0,0 +1,146 @@
<?php
namespace App\Service;
use App\Contract\YamlWrapper;
class Config
{
protected $dir;
protected $data;
public function __construct($dir = null)
{
if ($dir == null) {
$dir = realpath(root() . '/config');
}
$this->dir = $dir;
$this->load();
}
protected function load()
{
$files = glob($this->dir . '/*.{php,json,yml}', GLOB_BRACE);
foreach ($files as $file) {
$info = pathinfo($file);
$name = $info['filename'];
$d = $this->getData($file);
$data[$name] = $d;
$data = array_merge($data, $this->translateArray($d, $name));
foreach ($data as $key => $value) {
$this->add($key, $value);
}
}
}
public function loadFile(string $filename)
{
if (!file_exists(realpath($filename))) {
return false;
}
$info = pathinfo($filename);
$name = $info['filename'];
$d = $this->getData($filename);
$data[$name] = $d;
$data = array_merge($data, $this->translateArray($d, $name));
foreach ($data as $key => $value) {
$this->add($key, $value);
}
return true;
}
protected function getData($filename)
{
$info = pathinfo($filename);
switch ($info['extension']) {
case 'php':
return include_once $filename;
case 'json':
return json_decode(file_get_contents($filename), true);
case 'yml':
return YamlWrapper::load($filename);
default:
throw new \DomainException('Invalid file extension for ' . $filename);
}
}
protected function translateArray($array, $level)
{
$output = [];
foreach ($array as $k1 => $l1) {
$key = $level . '.' . $k1;
if (is_array($l1)) {
$output[$key] = $l1;
$output = array_merge($output, $this->translateArray($l1, $key));
} else {
$output[$key] = $l1;
}
}
return $output;
}
protected function add($field, $value)
{
if (isset($this->data[$field])) {
if ($this->data[$field] == $value) {
return;
}
if (is_array($this->data[$field])) {
$this->data[$field] = $this->merge($this->data[$field], $this->replace($value));
} else {
$this->data[$field] = $this->replace($value);
}
} else {
$this->data[$field] = $this->replace($value);
}
}
protected function merge($arr1, $arr2)
{
$output = $arr1;
foreach ($arr2 as $k => $value) {
if (isset($arr1[$k])) {
if ($arr1[$k] == $value) {
continue;
}
if (is_array($arr1[$k])) {
$output[$k] = $this->merge($arr1[$k], $value);
} else {
$output[$k] = array_merge([$arr1[$k]], $value);
}
} else {
$output[$k] = $value;
}
}
return $output;
}
protected function replace($value)
{
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->replace($v);
}
return $value;
}
if (strpos($value, '{') !== false) {
while(strpos($value, '{') !== false) {
$ini = strpos($value, '{') + 1;
$end = strpos($value, '}', $ini);
$rep = substr($value, $ini, $end - $ini);
$value = str_replace('{' . $rep . '}', $this->get($rep), $value);
}
}
return $value;
}
public function get($name = null)
{
if ($name == null) {
return $this->data;
}
if (isset($this->data[$name])) {
return $this->data[$name];
}
return null;
}
public function set($name, $value)
{
$this->add($name, $value);
}
}
?>

View File

@ -0,0 +1,3 @@
<?php
include_once dirname(__DIR__) . '/vendor/autoload.php';
?>

View File

@ -0,0 +1,28 @@
{
"name": "aldarien/config",
"description": "Config module for my apps",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Aldarien",
"email": "aldarien85@gmail.com"
}
],
"require": {
"aldarien/contract": "dev-master",
"aldarien/root": "dev-master",
"symfony/yaml": "^3.3"
},
"autoload": {
"psr-4": {
"App\\": "app"
},
"files": [
"app/Helper/functions.php"
]
},
"require-dev": {
"phpunit/phpunit": "^6.3"
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
verbose="true"
bootstrap="./bootstrap/autoload.php"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./app</directory>
</whitelist>
</filter>
</phpunit>

View File

@ -0,0 +1,90 @@
<?php
use PHPUnit\Framework\TestCase;
use App\Contract\YamlWrapper;
class ConfigTest extends TestCase
{
public function setUp()
{
mkdir(dirname(__DIR__) . '/config');
$str = "<?php return ['name' => 'Config', 'test_array' => ['data1' => 1, 'data2' => 2]]; ?>";
file_put_contents(dirname(__DIR__) . '/config/app.php', $str);
$data = ['name' => 'Config', 'test_array' => ['data1' => 1, 'data2' => 2]];
file_put_contents(dirname(__DIR__) . '/config/json.json', json_encode($data));
file_put_contents(dirname(__DIR__) . '/config/yaml.yml', YamlWrapper::dump($data));
$data = ['last_name' => 'Config'];
file_put_contents(dirname(__DIR__) . '/config/yaml.json', json_encode($data));
}
public function testGetNamePhp()
{
$name = 'Config';
$this->assertEquals($name, config('app.name'));
}
public function testGetNameJson()
{
$name = 'Config';
$this->assertEquals($name, config('json.name'));
}
public function testGetNameYaml()
{
$name = 'Config';
$this->assertEquals($name, config('yaml.name'));
}
public function testSetNamehp()
{
$new_name = 'Config_Test';
config('app.name', $new_name);
$this->assertEquals($new_name, config('app.name'));
}
public function testSetNameJson()
{
$new_name = 'Config_Test';
config('json.name', $new_name);
$this->assertEquals($new_name, config('json.name'));
}
public function testSetNameYaml()
{
$new_name = 'Config_Test';
config('yaml.name', $new_name);
$this->assertEquals($new_name, config('yaml.name'));
}
public function testArrayGetPhp()
{
$this->assertArrayHasKey('data1', config('app.test_array'));
}
public function testArrayGetJson()
{
$this->assertArrayHasKey('data1', config('json.test_array'));
}
public function testArrayGetYaml()
{
$this->assertArrayHasKey('data1', config('yaml.test_array'));
}
public function testSameSectionName()
{
$this->assertEquals('Config', config('yaml.last_name'));
}
public function testDuplicateValue()
{
config('json.name', 'Config2');
$this->assertEquals('Config2', config('json.name'));
}
public function testAddFile()
{
$filename = dirname(__DIR__) . '/composer.json';
App\Contract\Config::addFile($filename);
$this->assertEquals('aldarien/config', config('composer.name'));
}
public function tearDown()
{
unlink(dirname(__DIR__) . '/config/app.php');
unlink(dirname(__DIR__) . '/config/json.json');
unlink(dirname(__DIR__) . '/config/yaml.yml');
unlink(dirname(__DIR__) . '/config/yaml.json');
rmdir(dirname(__DIR__) . '/config');
}
}
?>