diff --git a/controller/Controller.class.php b/controller/Controller.class.php index e07e7422..8ccc81e8 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -97,6 +97,7 @@ class Controller $router->post('/postcommit', 'OpsActions::executePostCommit'); $router->post('/log-upload', 'OpsActions::executeLogUpload'); + $router->get('/clear-cache', 'OpsActions::executeClearCache'); $router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->any('/list/subscribed', 'MailActions::executeSubscribed'); diff --git a/controller/action/OpsActions.class.php b/controller/action/OpsActions.class.php index b3508bbf..5214d10e 100644 --- a/controller/action/OpsActions.class.php +++ b/controller/action/OpsActions.class.php @@ -2,6 +2,20 @@ class OpsActions extends Actions { + public static function executeClearCache(): array + { + if (!ini_get('apc.enabled') || !function_exists('apc_clear_cache')) + { + return View::renderJson(['success' => false, 'error' => 'Cache not enabled']); + } + + apc_clear_cache(); + apc_clear_cache('user'); + apc_clear_cache('opcode'); + + return View::renderJson(['success' => true]); + } + public static function executePostCommit(): array { $payload = Request::getParam('payload'); diff --git a/view/Response.class.php b/view/Response.class.php index 14f91b22..0a5ef721 100644 --- a/view/Response.class.php +++ b/view/Response.class.php @@ -352,7 +352,7 @@ class Response return $statusTexts[$code] ?? null; } - protected static function normalizeHeaderName($name) + protected static function normalizeHeaderName($name): string { return preg_replace_callback( '/\-(.)/', diff --git a/view/View.class.php b/view/View.class.php index bca3bac2..cba1b22f 100644 --- a/view/View.class.php +++ b/view/View.class.php @@ -19,7 +19,7 @@ class View const CSS_DIR = self::WEB_DIR . '/css'; const JS_DIR = self::WEB_DIR . '/js'; - public static function render($template, array $vars = []) + public static function render($template, array $vars = []): string { if (static::isMarkdown($template)) { @@ -78,7 +78,7 @@ class View } } - public static function markdownToHtml($path) + public static function markdownToHtml($path): string { return ParsedownExtra::instance()->text(trim(file_get_contents($path))); } @@ -154,7 +154,7 @@ class View } } - protected static function interpolateTokens($html) + protected static function interpolateTokens($html): string { return preg_replace_callback('/{{[\w\.]+}}/is', function ($m) { @@ -162,12 +162,12 @@ class View }, $html); } - protected static function escapeOnce($value) + protected static function escapeOnce($value): string { return preg_replace('/&([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', htmlspecialchars((string)$value, ENT_QUOTES, 'utf-8')); } - protected static function attributesToHtml($attributes) + protected static function attributesToHtml($attributes): string { return implode('', array_map(function ($k, $v) { @@ -175,13 +175,19 @@ class View }, array_keys($attributes), array_values($attributes))); } - public static function renderTag($tag, $attributes = []) + public static function renderTag($tag, $attributes = []): string { return $tag ? sprintf('<%s%s />', $tag, static::attributesToHtml($attributes)) : ''; } - public static function renderContentTag($tag, $content = null, $attributes = []) + public static function renderContentTag($tag, $content = null, $attributes = []): string { return $tag ? sprintf('<%s%s>%s', $tag, static::attributesToHtml($attributes), $content, $tag) : ''; } + + public static function renderJson($data): array + { + Response::setHeader(Response::HEADER_CONTENT_TYPE, 'application/json'); + return ['internal/json', ['json' => $data, '_no_layout' => true]]; + } } \ No newline at end of file diff --git a/view/template/internal/json.php b/view/template/internal/json.php new file mode 100644 index 00000000..a03a649f --- /dev/null +++ b/view/template/internal/json.php @@ -0,0 +1 @@ + \ No newline at end of file