From 49d0809af565740e24e41c8d28cd34e42ead12c5 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 31 Oct 2017 07:55:58 -0400 Subject: [PATCH 1/2] Add route to clear cache --- controller/Controller.class.php | 1 + controller/action/OpsActions.class.php | 14 ++++++++++++++ view/Response.class.php | 2 +- view/View.class.php | 20 +++++++++++++------- view/template/internal/json.php | 1 + 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 view/template/internal/json.php 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 From f89b15a60e0ef7449d1b28f6c53c0e385bfc64b7 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 31 Oct 2017 15:35:13 -0400 Subject: [PATCH 2/2] clear cache on post-commit update --- controller/Controller.class.php | 4 +++- update.php | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/controller/Controller.class.php b/controller/Controller.class.php index 8ccc81e8..4e194753 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -2,6 +2,8 @@ class Controller { + const CACHE_CLEAR_PATH = '/clear-cache'; + protected static $queuedFunctions = []; public static function dispatch($uri) @@ -97,7 +99,7 @@ class Controller $router->post('/postcommit', 'OpsActions::executePostCommit'); $router->post('/log-upload', 'OpsActions::executeLogUpload'); - $router->get('/clear-cache', 'OpsActions::executeClearCache'); + $router->get(static::CACHE_CLEAR_PATH, 'OpsActions::executeClearCache'); $router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->any('/list/subscribed', 'MailActions::executeSubscribed'); diff --git a/update.php b/update.php index 5b8e033d..40f501c9 100755 --- a/update.php +++ b/update.php @@ -21,3 +21,6 @@ Shell::exec('git fetch && git reset --hard origin/master'); View::compileCss(); View::gzipAssets(); + +// clear cache +Curl::get('localhost'.Controller::CACHE_CLEAR_PATH); \ No newline at end of file