[+] добавлен примитивный шаблонизатор (в зачаточном состоянии) и кэширование пользовательских методов
This commit is contained in:
1 parent
2119348888
commit
3b6db0f7a2
5 files changed
+58
-4
No files matched your search
@@ -2,10 +2,17 @@
|
|||||||
|
|
||||||
use Core\Models\User;
|
use Core\Models\User;
|
||||||
use Core\Helpers\Cache;
|
use Core\Helpers\Cache;
|
||||||
|
use Core\Template;
|
||||||
|
|
||||||
if (session_status() == PHP_SESSION_NONE) {
|
if (session_status() == PHP_SESSION_NONE) {
|
||||||
session_start();
|
session_start();
|
||||||
}
|
}
|
||||||
|
ob_start(function($buffer) {
|
||||||
|
Template::set('CLEAR_CACHE_LINK_NAME', 'Сброс файлового кэша');
|
||||||
|
Template::set('ADMIN_PANEL_LINK_NAME', 'Панель администратора');
|
||||||
|
Template::set('REFRESH_PAGE_LINK_NAME', 'Перезагрузить страницу');
|
||||||
|
return Template::render($buffer);
|
||||||
|
});
|
||||||
|
|
||||||
define('START_TIME', microtime(true)); // засекаем время старта скрипта
|
define('START_TIME', microtime(true)); // засекаем время старта скрипта
|
||||||
define('CORE_LOADED', true); // флаг корректного запуска
|
define('CORE_LOADED', true); // флаг корректного запуска
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace Core\Helpers;
|
namespace Core\Helpers;
|
||||||
|
|
||||||
use Core\Models\User;
|
use Core\Models\User;
|
||||||
|
use Core\CoreException;
|
||||||
|
|
||||||
class Log
|
class Log
|
||||||
{
|
{
|
||||||
@@ -31,6 +32,7 @@
|
|||||||
* @param bool $addEnv Флаг необходимости указания расширенных данных
|
* @param bool $addEnv Флаг необходимости указания расширенных данных
|
||||||
*
|
*
|
||||||
* @return int Количество сохранённых байт
|
* @return int Количество сохранённых байт
|
||||||
|
* @throws CoreException
|
||||||
*/
|
*/
|
||||||
public static function logToFile(
|
public static function logToFile(
|
||||||
string $message,
|
string $message,
|
||||||
|
|||||||
@@ -290,7 +290,13 @@
|
|||||||
*/
|
*/
|
||||||
public static function isUserExists(string $login): bool
|
public static function isUserExists(string $login): bool
|
||||||
{
|
{
|
||||||
|
$cacheId = md5('isUserExists_' . $login);
|
||||||
|
if(Cache::check($cacheId)) {
|
||||||
|
$result = Cache::get($cacheId);
|
||||||
|
} else {
|
||||||
$result = (DB::getInstance())->getItem(self::TABLE, ['login' => $login]);
|
$result = (DB::getInstance())->getItem(self::TABLE, ['login' => $login]);
|
||||||
|
Cache::set($cacheId, $result);
|
||||||
|
}
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
return true;
|
return true;
|
||||||
@@ -306,7 +312,13 @@
|
|||||||
*/
|
*/
|
||||||
public static function countUsers(): int
|
public static function countUsers(): int
|
||||||
{
|
{
|
||||||
|
$cacheId = md5('countUsers');
|
||||||
|
if(Cache::check($cacheId) && Cache::getAge($cacheId) < 300) {
|
||||||
|
$result = Cache::get($cacheId);
|
||||||
|
} else {
|
||||||
$result = (DB::getInstance())->getItems(self::TABLE, ['id' => '>0']);
|
$result = (DB::getInstance())->getItems(self::TABLE, ['id' => '>0']);
|
||||||
|
Cache::set($cacheId, $result);
|
||||||
|
}
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
return count($result);
|
return count($result);
|
||||||
@@ -416,7 +428,7 @@
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isset($_SESSION['authorize']) or empty($_SESSION['authorize']) or $_SESSION['authorize'] !== 'Y') {
|
if (!isset($_SESSION['authorize']) || empty($_SESSION['authorize']) || $_SESSION['authorize'] !== 'Y') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$result = (DB::getInstance())->getItem(self::TABLE, ['login' => $_SESSION['login']]);
|
$result = (DB::getInstance())->getItem(self::TABLE, ['login' => $_SESSION['login']]);
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Core;
|
||||||
|
use Core\Helpers\SystemFunctions;
|
||||||
|
|
||||||
|
class Template
|
||||||
|
{
|
||||||
|
private static $arTemplateVariables = [];
|
||||||
|
|
||||||
|
public static function set(string $varName, string $varValue): void
|
||||||
|
{
|
||||||
|
$varName = '{{' . $varName . '}}';
|
||||||
|
self::$arTemplateVariables[$varName] = $varValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function flush(): void
|
||||||
|
{
|
||||||
|
self::$arTemplateVariables = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function render($buffer): ?string
|
||||||
|
{
|
||||||
|
|
||||||
|
$buffer = str_replace(array_keys(self::$arTemplateVariables), array_values(self::$arTemplateVariables), $buffer);
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,32 +6,38 @@
|
|||||||
use Core\Helpers\{SystemFunctions, Cache, Log, Mail, Zip};
|
use Core\Helpers\{SystemFunctions, Cache, Log, Mail, Zip};
|
||||||
|
|
||||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/core/bootstrap.php';
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/core/bootstrap.php';
|
||||||
|
Log::logToFile('Скрипт начал свою работу', 'test.log');
|
||||||
|
|
||||||
echo '<a href="/admin/">Админка</a> | <a href="/?clear_cache=Y">Очистить кэш</a> | <a href="/">Обновить страницу</a> <br>';
|
echo '<a href="/admin/">{{ADMIN_PANEL_LINK_NAME}}</a> | <a href="/?clear_cache=Y">{{CLEAR_CACHE_LINK_NAME}}</a> | <a href="/">{{REFRESH_PAGE_LINK_NAME}}</a> <br>';
|
||||||
|
|
||||||
Telegram::init('5232660453:AAGfMWu6EcRfBGSSURJsEEvGPmAqhCyzYHU', './');
|
Telegram::init('5232660453:AAGfMWu6EcRfBGSSURJsEEvGPmAqhCyzYHU', './');
|
||||||
//$userId = User::registration('test', '12345', 'support@it-stories.ru', 'user', 'Тестировщик', 'http://samag.ru/uploads/5222447.png');
|
//$userId = User::registration('test', '12345', 'support@it-stories.ru', 'user', 'Тестировщик', 'http://samag.ru/uploads/5222447.png');
|
||||||
|
|
||||||
if (isset($_REQUEST['log']) && $_REQUEST['log'] === CODE_VALUE_Y) {
|
/*if (isset($_REQUEST['log']) && $_REQUEST['log'] === CODE_VALUE_Y) {
|
||||||
Log::logToFile('Тестовое сообщение в лог', 'test.log', $_REQUEST);
|
Log::logToFile('Тестовое сообщение в лог', 'test.log', $_REQUEST);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/*$user = new User($userId);*/
|
/*$user = new User($userId);*/
|
||||||
|
|
||||||
$userId = User::getCurrentUserId();
|
$userId = User::getCurrentUserId();
|
||||||
|
Log::logToFile('Проверка пользователя на авторизованность', 'test.log', ['userId' => $userId]);
|
||||||
try {
|
try {
|
||||||
$user = new User($userId);
|
$user = new User($userId);
|
||||||
User::authorize((int)$userId);
|
User::authorize((int)$userId);
|
||||||
echo 'Current user id: ' . $userId . '<br>';
|
echo 'Current user id: ' . $userId . '<br>';
|
||||||
} catch (CoreException $e) {
|
} catch (CoreException $e) {
|
||||||
|
Log::logToFile('CoreException: ' . $e->getMessage(), 'test.log', $_REQUEST);
|
||||||
echo $e->showTrace();
|
echo $e->showTrace();
|
||||||
}
|
}
|
||||||
echo '<br><br>';
|
echo '<br><br>';
|
||||||
|
|
||||||
if (User::isAuthorized()) {
|
if (User::isAuthorized()) {
|
||||||
|
Log::logToFile('Пользователь авторизован, выводим данные о нем', 'test.log', $user->getAllUserData());
|
||||||
echo SystemFunctions::arrayToTable($user->getAllUserData(), 'Информация о пользователе');
|
echo SystemFunctions::arrayToTable($user->getAllUserData(), 'Информация о пользователе');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
echo SystemFunctions::arrayToTable(Cache::getCacheInfo(), 'Информация о кэше');
|
echo SystemFunctions::arrayToTable(Cache::getCacheInfo(), 'Информация о кэше');
|
||||||
|
|
||||||
|
|
||||||
|
Log::logToFile('Скрипт завершил свою работу', 'test.log');
|
||||||
Reference in new issue
Block a user