Fix LESS import error and refactor project structure
This commit is contained in:
@@ -2,66 +2,68 @@
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* App - главный класс приложения
|
||||
*/
|
||||
class App
|
||||
{
|
||||
private Router $router;
|
||||
private static ?App $instance = null;
|
||||
private array $config = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
self::$instance = $this;
|
||||
|
||||
// Регистрируем автозагрузчик сразу
|
||||
$this->registerAutoloader();
|
||||
|
||||
$this->router = new Router();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить экземпляр приложения
|
||||
*/
|
||||
public static function getInstance(): ?self
|
||||
{
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить роутер
|
||||
*/
|
||||
public function getRouter(): Router
|
||||
{
|
||||
return $this->router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Инициализация приложения
|
||||
*/
|
||||
public function getConfig(string $key = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
return $this->config;
|
||||
}
|
||||
return $this->config[$key] ?? null;
|
||||
}
|
||||
|
||||
public function init(): self
|
||||
{
|
||||
// Запускаем сессию
|
||||
$this->loadConfig();
|
||||
date_default_timezone_set($this->config['timezone'] ?? 'Europe/Moscow');
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Настраиваем обработку ошибок
|
||||
$this->setupErrorHandling();
|
||||
|
||||
// Загружаем маршруты
|
||||
$this->loadRoutes();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Регистрация автозагрузчика классов
|
||||
*/
|
||||
private function loadConfig(): void
|
||||
{
|
||||
$configPath = $this->getBasePath() . '/config/app.php';
|
||||
if (file_exists($configPath)) {
|
||||
$this->config = require $configPath;
|
||||
}
|
||||
}
|
||||
|
||||
public function getBasePath(): string
|
||||
{
|
||||
return defined('ROOT_PATH') ? ROOT_PATH : dirname(__DIR__, 2);
|
||||
}
|
||||
|
||||
private function registerAutoloader(): void
|
||||
{
|
||||
spl_autoload_register(function ($class) {
|
||||
// Преобразуем namespace в путь к файлу
|
||||
$prefix = 'App\\';
|
||||
$baseDir = dirname(__DIR__) . '/';
|
||||
|
||||
@@ -79,14 +81,9 @@ class App
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Настройка обработки ошибок
|
||||
*/
|
||||
private function setupErrorHandling(): void
|
||||
{
|
||||
$config = require dirname(__DIR__, 2) . '/config/app.php';
|
||||
|
||||
if ($config['debug'] ?? false) {
|
||||
if ($this->config['debug'] ?? false) {
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
} else {
|
||||
@@ -103,16 +100,11 @@ class App
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработка исключений
|
||||
*/
|
||||
private function handleException(\Throwable $e): void
|
||||
{
|
||||
$config = require dirname(__DIR__, 2) . '/config/app.php';
|
||||
|
||||
http_response_code(500);
|
||||
|
||||
if ($config['debug'] ?? false) {
|
||||
if ($this->config['debug'] ?? false) {
|
||||
echo "<h1>Ошибка приложения</h1>";
|
||||
echo "<p><strong>Сообщение:</strong> " . htmlspecialchars($e->getMessage()) . "</p>";
|
||||
echo "<p><strong>Файл:</strong> " . htmlspecialchars($e->getFile()) . ":" . $e->getLine() . "</p>";
|
||||
@@ -122,12 +114,9 @@ class App
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Загрузка маршрутов
|
||||
*/
|
||||
private function loadRoutes(): void
|
||||
{
|
||||
$routesFile = dirname(__DIR__, 2) . '/config/routes.php';
|
||||
$routesFile = $this->getBasePath() . '/config/routes.php';
|
||||
|
||||
if (file_exists($routesFile)) {
|
||||
$router = $this->router;
|
||||
@@ -135,21 +124,17 @@ class App
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запуск приложения
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
|
||||
// Удаляем базовый путь, если он есть
|
||||
$basePath = '/cite_practica';
|
||||
if (strpos($uri, $basePath) === 0) {
|
||||
$basePath = $this->config['base_path'] ?? '';
|
||||
|
||||
if (!empty($basePath) && strpos($uri, $basePath) === 0) {
|
||||
$uri = substr($uri, strlen($basePath));
|
||||
}
|
||||
|
||||
// Если URI пустой, делаем его корневым
|
||||
if (empty($uri) || $uri === false) {
|
||||
$uri = '/';
|
||||
}
|
||||
@@ -161,4 +146,3 @@ class App
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user