Линтовка
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) 2025, Антон Аксенов
|
||||
* This file is part of m3u.su project
|
||||
@@ -30,11 +31,6 @@ final class Kernel
|
||||
*/
|
||||
public const string VERSION = '1.0.0';
|
||||
|
||||
/**
|
||||
* @var Kernel
|
||||
*/
|
||||
private static Kernel $instance;
|
||||
|
||||
/**
|
||||
* @var App
|
||||
*/
|
||||
@@ -55,6 +51,11 @@ final class Kernel
|
||||
*/
|
||||
protected ?Redis $cache = null;
|
||||
|
||||
/**
|
||||
* @var Kernel
|
||||
*/
|
||||
private static Kernel $instance;
|
||||
|
||||
/**
|
||||
* Закрытый конструктор
|
||||
*
|
||||
@@ -75,11 +76,73 @@ final class Kernel
|
||||
*
|
||||
* @return Kernel
|
||||
*/
|
||||
public static function instance(): Kernel
|
||||
public static function instance(): self
|
||||
{
|
||||
return self::$instance ??= new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект подключения к Redis
|
||||
*
|
||||
* @return Redis
|
||||
* @see https://github.com/phpredis/phpredis/?tab=readme-ov-file
|
||||
*/
|
||||
public function redis(): Redis
|
||||
{
|
||||
if (!empty($this->cache)) {
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
$options = [
|
||||
'host' => $this->config['cache']['host'],
|
||||
'port' => (int) $this->config['cache']['port'],
|
||||
];
|
||||
|
||||
if (!empty($this->config['cache']['password'])) {
|
||||
$options['auth'] = $this->config['cache']['password'];
|
||||
}
|
||||
|
||||
$this->cache = new Redis($options);
|
||||
$this->cache->select((int) $this->config['cache']['db']);
|
||||
$this->cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает значение из конфига
|
||||
*
|
||||
* @param string $key Ключ в формате "config.key"
|
||||
* @param mixed|null $default Значение по умолчанию
|
||||
* @return mixed
|
||||
*/
|
||||
public function config(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$parts = explode('.', $key);
|
||||
|
||||
return $this->config[$parts[0]][$parts[1]] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект приложения
|
||||
*
|
||||
* @return App
|
||||
*/
|
||||
public function app(): App
|
||||
{
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект ini-файла
|
||||
*
|
||||
* @return IniFile
|
||||
*/
|
||||
public function ini(): IniFile
|
||||
{
|
||||
return $this->iniFile ??= new IniFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Загружает файл .env или .env.$env
|
||||
*
|
||||
@@ -88,12 +151,13 @@ final class Kernel
|
||||
*/
|
||||
protected function loadDotEnvFile(string $env = ''): array
|
||||
{
|
||||
$filename = empty($env) ? '.env' : ".env.$env";
|
||||
$filename = empty($env) ? '.env' : ".env.{$env}";
|
||||
if (!file_exists(root_path($filename))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$dotenv = Dotenv::createMutable(root_path(), $filename);
|
||||
|
||||
return $dotenv->safeLoad();
|
||||
}
|
||||
|
||||
@@ -138,7 +202,7 @@ final class Kernel
|
||||
default => throw new InvalidArgumentException(sprintf('Неверный HTTP метод %s', $method))
|
||||
};
|
||||
|
||||
$definition = $this->app->$func($route['path'], $route['handler']);
|
||||
$definition = $this->app->{$func}($route['path'], $route['handler']);
|
||||
}
|
||||
|
||||
if (!empty($route['name'])) {
|
||||
@@ -163,65 +227,4 @@ final class Kernel
|
||||
$twig->addExtension(new DebugExtension());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект подключения к Redis
|
||||
*
|
||||
* @return Redis
|
||||
* @see https://github.com/phpredis/phpredis/?tab=readme-ov-file
|
||||
*/
|
||||
public function redis(): Redis
|
||||
{
|
||||
if (!empty($this->cache)) {
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
$options = [
|
||||
'host' => $this->config['cache']['host'],
|
||||
'port' => (int)$this->config['cache']['port'],
|
||||
];
|
||||
|
||||
if (!empty($this->config['cache']['password'])) {
|
||||
$options['auth'] = $this->config['cache']['password'];
|
||||
}
|
||||
|
||||
$this->cache = new Redis($options);
|
||||
$this->cache->select((int)$this->config['cache']['db']);
|
||||
$this->cache->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
|
||||
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает значение из конфига
|
||||
*
|
||||
* @param string $key Ключ в формате "config.key"
|
||||
* @param mixed|null $default Значение по умолчанию
|
||||
* @return mixed
|
||||
*/
|
||||
public function config(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$parts = explode('.', $key);
|
||||
return $this->config[$parts[0]][$parts[1]] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект приложения
|
||||
*
|
||||
* @return App
|
||||
*/
|
||||
public function app(): App
|
||||
{
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает объект ini-файла
|
||||
*
|
||||
* @return IniFile
|
||||
*/
|
||||
public function ini(): IniFile
|
||||
{
|
||||
return $this->iniFile ??= new IniFile();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user