Переработка под iptvc

This commit is contained in:
2025-05-12 00:07:43 +08:00
parent f43843bb07
commit 252af50239
29 changed files with 1662 additions and 1268 deletions

View File

@@ -1,9 +1,14 @@
<?php
/*
* Copyright (c) 2025, Антон Аксенов
* This file is part of iptv.axenov.dev web interface
* MIT License: https://git.axenov.dev/IPTV/web/src/branch/master/LICENSE
*/
declare(strict_types=1);
use App\Core\Core;
use App\Core\IniFile;
use App\Core\Kernel;
use Slim\App;
/**
@@ -74,28 +79,13 @@ function env(string $key, mixed $default = null): mixed
}
/**
* Renders template
* Returns kernel object
*
* @param mixed $template
* @param array $data
* @return void
* @throws Exception
* @return Kernel
*/
function view(mixed $template, array $data = []): void
function kernel(): Kernel
{
$template = str_contains($template, '.twig') ? $template : "$template.twig";
/** @noinspection PhpVoidFunctionResultUsedInspection */
echo Flight::view()->render($template, $data);
}
/**
* Returns core object
*
* @return Core
*/
function core(): Core
{
return Core::get();
return Kernel::instance();
}
/**
@@ -105,25 +95,7 @@ function core(): Core
*/
function app(): App
{
return Core::get()->app();
}
/**
* Returns any value as boolean
*
* @param mixed $value
* @return bool
*/
function bool(mixed $value): bool
{
is_string($value) && $value = strtolower(trim($value));
if (in_array($value, [true, 1, '1', '+', 'y', 'yes', 'on', 'true', 'enable', 'enabled'], true)) {
return true;
}
if (in_array($value, [false, 0, '0', '-', 'n', 'no', 'off', 'false', 'disable', 'disabled'], true)) {
return false;
}
return (bool)$value;
return Kernel::instance()->app();
}
/**
@@ -135,7 +107,7 @@ function bool(mixed $value): bool
*/
function config(string $key, mixed $default = null): mixed
{
return Core::get()->config($key, $default);
return Kernel::instance()->config($key, $default);
}
/**
@@ -145,7 +117,100 @@ function config(string $key, mixed $default = null): mixed
*/
function redis(): Redis
{
return Core::get()->redis();
return Kernel::instance()->redis();
}
/**
* Returns any value as boolean
*
* @param mixed $value
* @return bool
*/
function bool(mixed $value): bool
{
is_string($value) && $value = strtolower(trim($value));
$positives = [true, 1, '1', '+', 'yes', 'on', 'true', 'enable', 'enabled'];
if (in_array($value, $positives, true)) {
return true;
}
$negatives = [false, 0, '0', '-', 'no', 'off', 'false', 'disable', 'disabled'];
if (in_array($value, $negatives, true)) {
return false;
}
return (bool)$value;
}
/**
* Проверяет значениен на пустоту
*
* @param $value
* @return bool
*/
function is_blank($value): bool
{
if (is_null($value)) {
return true;
}
if (is_string($value)) {
return trim($value) === '';
}
if (is_numeric($value) || is_bool($value)) {
return false;
}
if ($value instanceof Countable) {
return count($value) === 0;
}
return empty($value);
}
/**
* Возвращает натуральное представление значения переменной или null
*
* @param mixed $value
* @return int|null
*/
function int(mixed $value): ?int
{
if (is_blank($value)) {
return null;
}
$filtered = filter_var($value, FILTER_VALIDATE_INT);
return $filtered === false ? (int)$value : $filtered;
}
/**
* Возвращает первый элемент массива без перемотки указателя
*
* @param array $array Входной массив
* @param callable|null $callback Замыкание для предварительной фильтрации вх. массива
* @return mixed
*/
function array_first(array $array, ?callable $callback = null): mixed
{
is_null($callback) || $array = array_filter($array, $callback);
return $array[array_key_first($array)] ?? null;
}
/**
* Возвращает последний элемент массива без перемотки указателя
*
* @param array $array Входной массив
* @param callable|null $callback Замыкание для предварительной фильтрации вх. массива
* @return mixed
*/
function array_last(array $array, ?callable $callback = null): mixed
{
is_null($callback) || $array = array_filter($array, $callback);
return $array[array_key_last($array)] ?? null;
}
/**
@@ -155,5 +220,5 @@ function redis(): Redis
*/
function ini(): IniFile
{
return Core::get()->ini();
return Kernel::instance()->ini();
}