iptv/src/app/Core/Bootstrapper.php

105 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
declare(strict_types=1);
namespace App\Core;
use App\Controllers\AjaxController;
use App\Controllers\HomeController;
use App\Controllers\PlaylistController;
use App\Extensions\TwigFunctions;
use Flight;
use Illuminate\Support\Arr;
use Symfony\Component\Dotenv\Dotenv;
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
/**
* Сборщик приложения
*/
final class Bootstrapper
{
/**
* Загружает env-переменные
*
* @return void
*/
public static function bootEnv(): void
{
(new Dotenv())->loadEnv(root_path() . '/.env');
}
/**
* Загружает конфигурацию приложения в контейнер
*
* @return void
*/
public static function bootSettings(): void
{
$settings = Arr::dot(require_once config_path('app.php'));
Arr::map($settings, function ($value, $key) {
Flight::set("flight.$key", $value);
});
Flight::set('config', $settings);
}
/**
* Загружает шаблонизатор и его расщирения
*
* @return void
*/
public static function bootTwig(): void
{
$filesystemLoader = new FilesystemLoader(config('views.path'));
Flight::register(
'view',
Environment::class,
[$filesystemLoader, config('twig')],
function ($twig) {
/** @var Environment $twig */
Flight::set('twig', $twig);
$twig->addExtension(new TwigFunctions());
$twig->addExtension(new DebugExtension());
}
);
}
/**
* Загружает маршруты
*
* @return void
*/
public static function bootRoutes(): void
{
Flight::route(
'GET /',
fn() => (new HomeController())->index()
);
Flight::route(
'GET /page/@page:[0-9]+',
fn($page) => (new HomeController())->index((int)$page)
);
Flight::route(
'GET /faq',
fn() => (new HomeController())->faq()
);
Flight::route(
'GET /@id:[a-zA-Z0-9_-]+',
fn($id) => (new PlaylistController())->download($id)
);
Flight::route(
'GET /?[a-zA-Z0-9_-]+',
fn($id) => (new PlaylistController())->download($id)
);
Flight::route(
'GET /@id:[a-zA-Z0-9_-]+/details',
fn($id) => (new PlaylistController())->details($id)
);
Flight::route(
'GET /@id:[a-zA-Z0-9_-]+/json',
fn($id) => (new PlaylistController())->json($id)
);
}
}