Merge branch 'develop' into release

This commit is contained in:
2023-06-16 00:54:10 +00:00
38 changed files with 848 additions and 0 deletions

7
PHP.Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM php:7.4-fpm
RUN docker-php-ext-install pdo pdo_mysql
#RUN pecl install xdebug-3.0.3 \
# && docker-php-ext-enable xdebug

View File

@ -0,0 +1,138 @@
<?php
namespace Aldarien\Common\Alias;
use Aldarien\Common\Definition\ConfigFile as ConfigFileInterface;
abstract class ConfigFile implements ConfigFileInterface {
protected $name;
public function setName(string $name) {
$this->name = $name;
}
public function getName(): string {
return $this->name;
}
protected $filename;
public function setFilename(string $filename) {
if (!file_exists($filename)) {
throw new \DomainException('File not found: ' . $filename . ' in ' . get_called_class() . '.');
}
$this->filename = $filename;
}
protected $data;
public function get(string $name, $default = null) {
$result = $default;
if ($this->has($name)) {
$result = $this->data[$name];
}
if (is_array($result)) {
foreach ($result as $k => $v) {
$result[$k] = $this->translate($v);
}
return $result;
}
if (is_object($result)) {
foreach ($result as $k => $v) {
$result->$k = $this->translate($v);
}
return $result;
}
$result = $this->translate($result);
return $result;
}
protected function translate($value) {
if (is_object($value)) {
foreach ($value as $k => $v) {
$value->$k = $this->translate($v);
}
return $value;
}
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->translate($v);
}
return $value;
}
if (strpos($value, '{') !== false) {
preg_match_all('/{(.*)}/', $value, $matches, \PREG_SET_ORDER);
foreach ($matches as $match) {
$value = str_replace($match[0], $this->get($match[1]), $value);
}
}
return $value;
}
public function set(string $name, $value): void {
if ($this->data === null) {
$this->data = [];
}
if (strpos($name, '.') === false) {
$name = implode('.', [$this->name, $name]);
}
$this->data[$name] = $value;
if (is_array($value)) {
$is_object = false;
foreach ($value as $k => $v) {
if (is_numeric($k)) {
continue;
}
$is_object = true;
$n = implode('.', [$name, $k]);
$this->set($n, $v);
}
if ($is_object) {
$this->data[$name] = (object) $this->parse($value);
}
}
if (is_object($value)) {
foreach ($value as $k => $v) {
$n = implode('.', [$name, $k]);
$this->set($n, $v);
}
}
}
protected function parse($value) {
if (is_array($value)) {
$is_object = false;
foreach ($value as $k => $v) {
if (!is_numeric($k)) {
$is_object = true;
}
$value[$k] = $this->parse($v);
}
$value = (object) $value;
}
return $value;
}
public function has(string $name): bool {
if (isset($this->data[$name])) {
return true;
}
return false;
}
protected $loaded;
public function isLoaded(): bool {
return ($this->loaded !== null and $this->loaded);
}
public function toArray() {
$arr = [];
foreach ($this->data as $key => $value) {
$arr[$key] = $this->translate($value);
}
return $arr;
}
protected $position;
public function current() {
return $this->data[array_keys($this->data)[$this->position]];
}
public function key(): scalar {
return array_keys($this->data)[$this->position];
}
public function rewind() {
$this->position = 0;
}
public function next() {
$this->position ++;
}
public function valid() {
return isset(array_keys($this->data)[$this->position]);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Aldarien\Common\Alias;
use Model as BaseModel;
class Model extends BaseModel {
}

View File

@ -0,0 +1,14 @@
<?php
namespace Aldarien\Common\Definition;
interface ConfigFile extends \Iterator {
public function setName(string $name);
public function getName(): string;
public function setFilename(string $filename);
public function get(string $name, $default = null);
public function set(string $name, $value): void;
public function has(string $name): bool;
public function load();
public function isLoaded(): bool;
public function save();
}

View File

@ -0,0 +1,86 @@
<?php
namespace Aldarien\Common\Service;
class Config implements \ArrayAccess {
protected $folder;
public function __construct(string $config_folder) {
if (!file_exists($config_folder)) {
throw new \InvalidArgumentException("\$config_folder for Config does not exist.");
}
$this->folder = $config_folder;
}
public function load() {
$files = new \DirectoryIterator($this->folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$this->loadFile($file);
}
return $this;
}
protected $files;
protected function loadFile(\SplFileInfo $file): void {
$ext = $file->getExtension();
$name = strtolower(str_replace(' ', '_', $file->getBasename('.' . $ext)));
$class = implode("\\", [
'Aldarien',
'Config',
str_replace('YML', 'YAML', strtoupper($ext))
]);
if (!class_exists($class)) {
return;
}
$obj = new $class();
$obj->setName($name);
$obj->setFilename($file->getRealPath());
if ($this->files === null) {
$this->files = [];
}
$this->files []= $obj;
}
public function get(string $name, $default = null) {
foreach ($this->files as $obj) {
if (!$obj->isLoaded()) {
$obj->load();
}
if ($obj->has($name)) {
return $obj->get($name);
}
}
return $default;
}
public function set(string $name, $value) {
if (strpos($name, '.') === false) {
return false;
}
$arr = explode('.', $name);
$file = array_shift($arr);
$name = implode('.', $arr);
$this->files[$file]->set($name, $value);
}
public function remove(string $name) {
}
public function toArray() {
$arr = [];
foreach ($this->files as $obj) {
if (!$obj->isLoaded()) {
$obj->load();
}
$arr = array_merge($arr, $obj->toArray());
}
return $arr;
}
public function offsetExists($offset): bool {
return $this->has($offset);
}
public function offsetGet($offset) {
return $this->get($offset);
}
public function offsetSet($offset, $value) {
$this->set($offset, $value);
}
public function offsetUnset ($offset) {
$this->remove($offset);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Aldarien\Config;
use Aldarien\Common\Alias\ConfigFile;
class JSON extends ConfigFile {
public function load() {
$data = json_decode(file_get_contents($this->filename));
foreach ($data as $k => $v) {
$this->set($k, $v);
}
$this->loaded = true;
}
public function save() {
file_put_contents($this->filename, json_encode($this->data, \JSON_PRETTY_PRINT || \JSON_NUMERIC_CHECK || \JSON_PRESERVE_ZERO_FRACTION));
}
}

View File

@ -0,0 +1,24 @@
<?php namespace Aldarien\Config;
use Aldarien\Common\Alias\ConfigFile;
class PHP extends ConfigFile {
public function load() {
$data = include($this->filename);
foreach ($data as $k => $v) {
$this->set($k, $v);
}
$this->loaded = true;
}
public function save() {
$str = [];
$str []= '<?php';
$str []= 'return [';
foreach ($this->data as $k => $v) {
$str []= "'" . $k . "' => " . $v;
}
$str []= '];';
$str []= '';
file_put_contents($this->filename, implode(PHP_EOL, $str));
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Aldarien\Config;
use Spyc;
use Aldarien\Common\Alias\ConfigFile;
class YAML extends ConfigFile {
public function load() {
$data = Spyc::YAMLLoad($this->filename);
foreach ($data as $k => $v) {
$this->set($k, $v);
}
$this->loaded = true;
}
public function save() {
file_put_contents($this->filename, Spyc::YAMLDump($this->data));
}
}

8
bootstrap/api/config.php Normal file
View File

@ -0,0 +1,8 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
'urls.base' => DI\decorate(function($prev, Container $c) {
return implode('/', [$prev, 'api']);
})
];

48
bootstrap/app.php Normal file
View File

@ -0,0 +1,48 @@
<?php
use DI\ContainerBuilder as Builder;
use DI\Bridge\Slim\Bridge;
include_once 'composer.php';
$builder = new Builder();
$folders = ['common'];
if (isset($__environment)) {
$folders []= $__environment;
}
$files = [
'config',
'setup'
];
foreach ($files as $file) {
foreach ($folders as $folder) {
$filename = implode(DIRECTORY_SEPARATOR, [
__DIR__,
$folder,
$file . '.php'
]);
if (file_exists($filename)) {
$builder->addDefinitions($filename);
}
}
}
$container = $builder->build();
$app = Bridge::create($container);
$app->setBasePath($container->get('urls.base'));
foreach ($folders as $folder) {
$filename = implode(DIRECTORY_SEPARATOR, [
__DIR__,
$folder,
'middleware.php'
]);
if (file_exists($filename)) {
include_once $filename;
}
}
$app->addErrorMiddleware(true, true, true);
include_once implode(DIRECTORY_SEPARATOR, [
$container->get('folders.routes'),
'router.php'
]);

View File

@ -0,0 +1,18 @@
<?php
return [
'folders.base' => dirname(__DIR__, 2),
'folders.config' => DI\string(implode(DIRECTORY_SEPARATOR, [
'{folders.base}',
'config'
])),
'folders.resources' => DI\string(implode(DIRECTORY_SEPARATOR, [
'{folders.base}',
'resources'
])),
'folders.routes' => DI\string(implode(DIRECTORY_SEPARATOR, [
'{folders.resources}',
'routes'
])),
//'urls.base' => '/optimus/money'
'urls.base' => ''
];

View File

@ -0,0 +1,14 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
'config' => function(Container $c) {
return (new Aldarien\Common\Service\Config($c->get('folders.config')))->load();
},
GuzzleHttp\ClientInterface::class => function(Container $c) {
return new GuzzleHttp\Client();
},
Aldarien\Money\UF\Handler::class => function(Container $c) {
return new Aldarien\Money\UF\Handler($c->get(GuzzleHttp\ClientInterface::class), 'https://mindicador.cl/api/uf');
}
];

6
bootstrap/composer.php Normal file
View File

@ -0,0 +1,6 @@
<?php
require_once implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__),
'vendor',
'autoload.php'
]);

30
bootstrap/web/config.php Normal file
View File

@ -0,0 +1,30 @@
<?php
return [
'folders.cache' => DI\string(implode(DIRECTORY_SEPARATOR, [
'{folders.resources}',
'cache'
])),
'folders.templates' => DI\string(implode(DIRECTORY_SEPARATOR, [
'{folders.resources}',
'views'
])),
'web.assets' => (object) [
'styles' => [
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/semantic.min.css'
],
'scripts' => [
'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/semantic.min.js'
],
'fonts' => [
'text/css' => [
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/brand-icons.woff',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/brand-icons.woff2',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/icons.woff',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/icons.woff2',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/outline-icons.woff',
'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.4/themes/default/assets/fonts/outline-icons.woff2'
]
]
]
];

17
bootstrap/web/setup.php Normal file
View File

@ -0,0 +1,17 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
Slim\Views\Blade::class => function(Container $c) {
return new Slim\Views\Blade(
$c->get('folders.templates'),
$c->get('folders.cache'),
null,
[
'base_url' => $c->get('urls.base'),
'page_language' => 'es',
'assets' => $c->get('web.assets')
]
);
}
];

View File

@ -0,0 +1,40 @@
<?php
namespace Aldarien\Money\Common\Controller\API\UF;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Carbon\Carbon;
use Aldarien\Money\UF\Handler as UFHandler;
class Value {
public function __invoke(Request $request, Response $response, UFHandler $handler): Response {
$fecha = Carbon::today();
$valor = $handler->get($fecha);
$output = [
'uf' => [
'date' => $fecha->format('Y-m-d'),
'value' => $valor
],
'total' => 1
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
public function fecha(Request $request, Response $response, UFHandler $handler, $fecha): Response {
$fecha = Carbon::parse($fecha);
$valor = $handler->get($fecha);
$output = [
'uf' => [
'date' => $fecha->format('Y-m-d'),
'value' => $valor
],
'total' => 1
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Aldarien\Money\Common\Controller\Web;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Carbon\Carbon;
use Slim\Views\Blade as View;
use Aldarien\Money\UF\Handler as UFHandler;
class Base {
public function __invoke(Request $request, Response $response, View $view, UFHandler $handler): Response {
$fecha = Carbon::today('America/Santiago');
$valor = $handler->get($fecha);
return $view->render($response, 'home', compact('fecha', 'valor'));
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Aldarien\Money\Common\Definition;
use GuzzleHttp\ClientInterface as Client;
interface Handler {
public function __construct(Client $client, string $api_url);
public function get(\DateTime $date);
}

View File

@ -0,0 +1,13 @@
<?php
namespace Aldarien\Money\Common\Implementation;
use GuzzleHttp\ClientInterface as Client;
use Aldarien\Money\Common\Definition\Handler as HandlerInterface;
abstract class Handler implements HandlerInterface {
protected $url;
public function __construct(Client $client, string $api_url) {
$this->client = $client;
$this->url = $api_url;
}
}

36
composer.json Normal file
View File

@ -0,0 +1,36 @@
{
"name": "aldarien/money",
"description": "Money handler",
"type": "project",
"require": {
"slim/slim": "^4.4",
"zeuxisoo/slim-whoops": "^0.7.2",
"rubellum/slim-blade-view": "^0.1.1",
"nyholm/psr7": "^1.2",
"nyholm/psr7-server": "^0.4.1",
"php-di/slim-bridge": "^3.0",
"nesbot/carbon": "^2.32",
"j4mie/paris": "^1.5",
"rmoiseev/spyc": "^0.5.1",
"guzzlehttp/guzzle": "^6.5"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"kint-php/kint": "^3.3"
},
"license": "MIT",
"authors": [
{
"name": "Aldarien",
"email": "aldarien85@gmail.com"
}
],
"autoload": {
"psr-4": {
"Aldarien\\Money\\Common\\": "common",
"Aldarien\\Money\\": "src",
"Aldarien\\Common\\": "aldarien/common",
"Aldarien\\": "aldarien/src"
}
}
}

65
docker-compose.yml Normal file
View File

@ -0,0 +1,65 @@
version: '3'
services:
money-web:
profiles:
- proxy
container_name: money_server
image: nginx:alpine
restart: unless-stopped
ports:
- 8008:80
volumes:
- .:/code
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- money-php
money-php:
profiles:
- app
container_name: money_php
build:
context: .
dockerfile: PHP.Dockerfile
restart: unless-stopped
volumes:
- .:/code
# ports:
# - 9124:9000
money-db:
profiles:
- db
container_name: money_db
image: mariadb:latest
restart: unless-stopped
# ports:
# - 3307:3306
environment:
MYSQL_ROOT_PASSWORD: 'money'
MYSQL_DATABASE: 'money'
MYSQL_USER: 'money'
MYSQL_PASSWORD: 'money_pass'
volumes:
- dbdata:/var/lib/mysql
adminer:
profiles:
- testing
container_name: money_adminer
image: adminer:latest
restart: unless-stopped
ports:
- 8009:8080
environment:
ADMINER_DESIGN: 'rmsoft_blue'
ADMINER_PLUGINS: 'dump-json'
networks:
default:
external: true
name: incoviba_network
volumes:
dbdata:

28
nginx.conf Normal file
View File

@ -0,0 +1,28 @@
server {
listen 80;
server_name money;
index index.php;
# error_log /code/logs/error.log;
# access_log /code/logs/access.log;
root /code/public;
location / {
try_files $uri /index.php$is_args$args;
}
location /api {
try_files $uri /api/index.php$is_args$args;
# add_header "Content-Type" "application/json";
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_index index.php;
fastcgi_pass money-php:9000;
}
}

4
public/.htaccess Normal file
View File

@ -0,0 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

4
public/api/.htaccess Normal file
View File

@ -0,0 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

8
public/api/index.php Normal file
View File

@ -0,0 +1,8 @@
<?php
$__environment = 'api';
include_once implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'bootstrap',
'app.php'
]);
$app->run();

8
public/index.php Normal file
View File

@ -0,0 +1,8 @@
<?php
$__environment = 'web';
include_once implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__),
'bootstrap',
'app.php'
]);
$app->run();

14
resources/routes/api.php Normal file
View File

@ -0,0 +1,14 @@
<?php
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'api'
]);
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
}

View File

@ -0,0 +1,16 @@
<?php
$app->group('/uf', function($app) {
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'uf'
]);
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
}
});

View File

@ -0,0 +1,7 @@
<?php
use Aldarien\Money\Common\Controller\API\UF\Value;
$app->group('/value', function($app) {
$app->get('/{fecha}', [Value::class, 'fecha']);
$app->get('[/]', Value::class);
});

View File

@ -0,0 +1,10 @@
<?php
if (isset($__environment)) {
$filename = implode(DIRECTORY_SEPARATOR, [
__DIR__,
$__environment . '.php'
]);
if (file_exists($filename)) {
include_once $filename;
}
}

18
resources/routes/web.php Normal file
View File

@ -0,0 +1,18 @@
<?php
use Aldarien\Money\Common\Controller\Web\Base;
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'web'
]);
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
}
$app->get('/', Base::class);

View File

@ -0,0 +1,14 @@
@extends('layout.base')
@section('page_content')
<div class="ui center aligned container">
<div class="ui statistic">
<div class="value">
$ {{number_format($valor, 2, ',', '.')}}
</div>
<div class="label">
{{$fecha->format('d/m/Y')}}
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,5 @@
<!DOCTYPE html>
<html lang="{{$page_language}}">
@include('layout.head')
@include('layout.body')
</html>

View File

@ -0,0 +1,3 @@
<body>
@yield('page_content')
</body>

View File

@ -0,0 +1,5 @@
<head>
<meta charset="utf-8" />
<title>Money</title>
@include('layout.styles')
</head>

View File

@ -0,0 +1,14 @@
@if (isset($assets->styles))
@foreach ($assets->styles as $style)
<link rel="stylesheet" type="text/css" href="{{$style}}" />
@endforeach
@endif
@if (isset($assets->fonts))
@foreach ($assets->fonts as $type => $fonts)
@foreach ($fonts as $font)
<link type="{{$type}}" href="{{$font}}" />
@endforeach
@endforeach
@endif
@stack('styles')

37
src/UF/Handler.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace Aldarien\Money\UF;
use Aldarien\Money\Common\Implementation\Handler as BaseHandler;
class Handler extends BaseHandler {
protected $history;
public function get(\DateTime $date) {
if ($this->history === null or !isset($this->history[$date->format('Y-m-d')])) {
$response = $this->client->request('GET', implode('/', [
$this->url,
$date->format('d-m-Y')
]), ['verify' => false]);
/*
{
"version": "1.6.0",
"autor": "mindicador.cl",
"codigo": "uf",
"nombre": "Unidad de fomento (UF)",
"unidad_medida": "Pesos",
"serie": [
{
"fecha": "2020-04-08T04:00:00.000Z",
"valor": 28626.94
}
]
}
*/
if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) {
$this->history[$date->format('Y-m-d')] = false;
return false;
}
$this->history[$date->format('Y-m-d')] = json_decode($response->getBody())->serie[0]->valor;
}
return $this->history[$date->format('Y-m-d')];
}
}

21
src/UF/Model.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Aldarien\Money\UF;
use Carbon\Carbon;
use Aldarien\Common\Alias\Model as ModelAlias;
/**
* @property int $id
* @property \DateTime $date
* @property double $value
*/
class Model extends ModelAlias {
public static $_table = 'ufs';
public function date(\DateTime $date = null) {
if ($date === null) {
return Carbon::parse($this->date);
}
$this->date = $date->format('Y-m-d');
}
}