From 4e8ea1514e9a5a961b03d61dc48d9218b66f1892 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Tue, 21 Jun 2022 23:18:17 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=20=D1=82=D0=B2=D0=B8=D0=B3=20=D0=B8=20?= =?UTF-8?q?=D1=80=D0=BE=D1=83=D1=82=D0=B8=D0=BD=D0=B3=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 4 +- composer.lock | 246 +++++++++++++++++- core/bootstrap.php | 17 +- core/config.php | 11 + core/lib/Core/App.class.php | 32 +++ core/lib/Core/Helpers/Cache.class.php | 2 +- .../Core/Helpers/SystemFunctions.class.php | 12 + core/lib/Core/Models/Router.class.php | 1 + core/routes.php | 15 +- templates/index.twig | 9 + templates/layout.twig | 28 ++ templates/parts/footer.twig | 10 + templates/parts/navbar/navbar.twig | 18 ++ templates/test.twig | 8 + 14 files changed, 395 insertions(+), 18 deletions(-) create mode 100644 core/lib/Core/App.class.php create mode 100644 templates/index.twig create mode 100644 templates/layout.twig create mode 100644 templates/parts/footer.twig create mode 100644 templates/parts/navbar/navbar.twig create mode 100644 templates/test.twig diff --git a/composer.json b/composer.json index 0b82aa5..1350a83 100644 --- a/composer.json +++ b/composer.json @@ -14,5 +14,7 @@ "email": "rsgrinko@gmail.com" } ], - "require": {} + "require": { + "twig/twig": "^3.0" + } } diff --git a/composer.lock b/composer.lock index 2f9bddb..a591cd2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,250 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "76495c532399aad23bb670f0202d18a2", - "packages": [], + "content-hash": "686f24e92c5bf479bf32f07bc4124026", + "packages": [ + { + "name": "symfony/polyfill-ctype", + "version": "v1.26.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "reference": "6fd1b9a79f6e3cf65f9e679b23af304cd9e010d4", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.26-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.26.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-05-24T11:49:31+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.26.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", + "reference": "9344f9cb97f3b19424af1a21a3b0e75b0a7d8d7e", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.26-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.26.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-05-24T11:49:31+00:00" + }, + { + "name": "twig/twig", + "version": "v3.4.1", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/e939eae92386b69b49cfa4599dd9bead6bf4a342", + "reference": "e939eae92386b69b49cfa4599dd9bead6bf4a342", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-mbstring": "^1.3" + }, + "require-dev": { + "psr/container": "^1.0", + "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.4-dev" + } + }, + "autoload": { + "psr-4": { + "Twig\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Twig Team", + "role": "Contributors" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "https://twig.symfony.com", + "keywords": [ + "templating" + ], + "support": { + "issues": "https://github.com/twigphp/Twig/issues", + "source": "https://github.com/twigphp/Twig/tree/v3.4.1" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2022-05-17T05:48:52+00:00" + } + ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", diff --git a/core/bootstrap.php b/core/bootstrap.php index 300a233..882f13c 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -11,6 +11,7 @@ } require_once __DIR__ . '/../vendor/autoload.php'; + ob_start(function($buffer) { try { Template::set('CLEAR_CACHE_LINK_NAME', 'Сброс файлового кэша'); @@ -28,6 +29,8 @@ require_once __DIR__ . '/config.php'; + + /** * Реализация механизма автозагрузки классов */ @@ -43,7 +46,15 @@ require_once __DIR__ . '/routes.php'; - Cache::init(ROOT_PATH . '/core/cache/', USE_CACHE); + /** + * Инициализация шаблонизатора + */ + $loader = new \Twig\Loader\FilesystemLoader(PATH_TO_TEMPLATES); + $twig = new \Twig\Environment($loader, [ + //'cache' => CACHE_DIR, + ]); + + Cache::init(CACHE_DIR, USE_CACHE); // очистка кэша if(isset($_REQUEST['clear_cache']) && $_REQUEST['clear_cache'] === CODE_VALUE_Y) { @@ -70,6 +81,8 @@ $USER = null; } - // запускаем маршрутизатор, передавая ему запрошенный адрес + /** + * Запускаем маршрутизатор + */ Router::execute(); die(); \ No newline at end of file diff --git a/core/config.php b/core/config.php index 62e4eda..f153d9f 100644 --- a/core/config.php +++ b/core/config.php @@ -24,6 +24,11 @@ const DB_PASSWORD = '2670135'; const DB_NAME = 'rsgrinko_delta'; + /** + * Путь до шаблонов + */ + const PATH_TO_TEMPLATES = ROOT_PATH . '/templates'; + /** * Токен телеграм бота */ @@ -43,6 +48,12 @@ * Использование кэша */ const USE_CACHE = false; + + /** + * Папка кэша + */ + const CACHE_DIR = ROOT_PATH . '/core/cache'; + /** * Время жизни кэша */ diff --git a/core/lib/Core/App.class.php b/core/lib/Core/App.class.php new file mode 100644 index 0000000..ceec341 --- /dev/null +++ b/core/lib/Core/App.class.php @@ -0,0 +1,32 @@ + 'Главная', 'url' => '/', 'active' => true], + ['name' => 'Админка', 'url' => '/admin', 'active' => false], + ['name' => 'Командная PHP строка', 'url' => '/phpcmd.php', 'active' => false], + ['name' => 'Тест', 'url' => '/test', 'active' => false], + ]; + echo $twig->render('index.twig', ['navbarItems' => $navbarItems, 'name' => 'Roman Grinko']); + } + + public static function test(int $id = 1) + { + global $twig; + $navbarItems = [ + ['name' => 'Главная', 'url' => '/', 'active' => true], + ['name' => 'Админка', 'url' => '/admin', 'active' => false], + ['name' => 'Командная PHP строка', 'url' => '/phpcmd.php', 'active' => false], + ['name' => 'Тест', 'url' => '/test', 'active' => false], + ]; + + $res = (new \Core\Models\User($id))->getAllUserData(); + echo $twig->render('test.twig', ['navbarItems' => $navbarItems, 'userId' => $id, 'res' => print_r($res, true)]); + } + } \ No newline at end of file diff --git a/core/lib/Core/Helpers/Cache.class.php b/core/lib/Core/Helpers/Cache.class.php index 93cd380..834a001 100644 --- a/core/lib/Core/Helpers/Cache.class.php +++ b/core/lib/Core/Helpers/Cache.class.php @@ -40,7 +40,7 @@ */ public static function init(string $dir, $enabled = true): void { - self::$cacheDir = $dir; + self::$cacheDir = $dir . '/'; self::$cacheEnabled = $enabled; } diff --git a/core/lib/Core/Helpers/SystemFunctions.class.php b/core/lib/Core/Helpers/SystemFunctions.class.php index 1604623..62ab211 100644 --- a/core/lib/Core/Helpers/SystemFunctions.class.php +++ b/core/lib/Core/Helpers/SystemFunctions.class.php @@ -359,4 +359,16 @@ } return $table; } + + + public static function showPage() + { + $loader = new \Twig\Loader\FilesystemLoader(PATH_TO_TEMPLATES); + $twig = new \Twig\Environment($loader, [ + 'cache' => CACHE_DIR, + ]); + + echo $twig->render('index.tpl', ['name' => 'Roman']); + + } } \ No newline at end of file diff --git a/core/lib/Core/Models/Router.class.php b/core/lib/Core/Models/Router.class.php index d2518c0..10a633b 100644 --- a/core/lib/Core/Models/Router.class.php +++ b/core/lib/Core/Models/Router.class.php @@ -2,6 +2,7 @@ namespace Core\Models; + class Router { diff --git a/core/routes.php b/core/routes.php index d262215..4780a58 100644 --- a/core/routes.php +++ b/core/routes.php @@ -1,11 +1,8 @@ + + + + + + {% block title %}Тестовый заголовок{% endblock %} + + + +
+ {{ include('parts/navbar/navbar.twig', {'navbarItems': navbarItems}) }} +
+
+
+ {% block content %}{% endblock %} +
+
+
+
+{{ include('parts/footer.twig') }} + + + \ No newline at end of file diff --git a/templates/parts/footer.twig b/templates/parts/footer.twig new file mode 100644 index 0000000..47357a6 --- /dev/null +++ b/templates/parts/footer.twig @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/templates/parts/navbar/navbar.twig b/templates/parts/navbar/navbar.twig new file mode 100644 index 0000000..f35e938 --- /dev/null +++ b/templates/parts/navbar/navbar.twig @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/templates/test.twig b/templates/test.twig new file mode 100644 index 0000000..26f25fc --- /dev/null +++ b/templates/test.twig @@ -0,0 +1,8 @@ +{% extends 'layout.twig' %} + +{% block title %}тестинг{% endblock %} + +{% block content %} + User id: {{ userId }}
+
{{ res }}
+{% endblock %} \ No newline at end of file