Add route to clear cache

This commit is contained in:
Alex Grintsvayg 2017-10-31 07:55:58 -04:00
parent 2141ec591f
commit 49d0809af5
5 changed files with 30 additions and 8 deletions

View file

@ -97,6 +97,7 @@ class Controller
$router->post('/postcommit', 'OpsActions::executePostCommit'); $router->post('/postcommit', 'OpsActions::executePostCommit');
$router->post('/log-upload', 'OpsActions::executeLogUpload'); $router->post('/log-upload', 'OpsActions::executeLogUpload');
$router->get('/clear-cache', 'OpsActions::executeClearCache');
$router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->any('/list/subscribe', 'MailActions::executeSubscribe');
$router->any('/list/subscribed', 'MailActions::executeSubscribed'); $router->any('/list/subscribed', 'MailActions::executeSubscribed');

View file

@ -2,6 +2,20 @@
class OpsActions extends Actions 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 public static function executePostCommit(): array
{ {
$payload = Request::getParam('payload'); $payload = Request::getParam('payload');

View file

@ -352,7 +352,7 @@ class Response
return $statusTexts[$code] ?? null; return $statusTexts[$code] ?? null;
} }
protected static function normalizeHeaderName($name) protected static function normalizeHeaderName($name): string
{ {
return preg_replace_callback( return preg_replace_callback(
'/\-(.)/', '/\-(.)/',

View file

@ -19,7 +19,7 @@ class View
const CSS_DIR = self::WEB_DIR . '/css'; const CSS_DIR = self::WEB_DIR . '/css';
const JS_DIR = self::WEB_DIR . '/js'; 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)) 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))); 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) return preg_replace_callback('/{{[\w\.]+}}/is', function ($m)
{ {
@ -162,12 +162,12 @@ class View
}, $html); }, $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')); 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) return implode('', array_map(function ($k, $v)
{ {
@ -175,13 +175,19 @@ class View
}, array_keys($attributes), array_values($attributes))); }, 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)) : ''; 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</%s>', $tag, static::attributesToHtml($attributes), $content, $tag) : ''; return $tag ? sprintf('<%s%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]];
}
} }

View file

@ -0,0 +1 @@
<?php echo json_encode($json ?? [], JSON_PRETTY_PRINT) ?>