Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route to clear cache #247

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions controller/Controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Controller
{
const CACHE_CLEAR_PATH = '/clear-cache';

protected static $queuedFunctions = [];

public static function dispatch($uri)
Expand Down Expand Up @@ -97,6 +99,7 @@ protected static function getRouterWithRoutes(): \Routing\RouteCollector

$router->post('/postcommit', 'OpsActions::executePostCommit');
$router->post('/log-upload', 'OpsActions::executeLogUpload');
$router->get(static::CACHE_CLEAR_PATH, 'OpsActions::executeClearCache');

$router->any('/list/subscribe', 'MailActions::executeSubscribe');
$router->any('/list/subscribed', 'MailActions::executeSubscribed');
Expand Down
14 changes: 14 additions & 0 deletions controller/action/OpsActions.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions update.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

View::compileCss();
View::gzipAssets();

// clear cache
Curl::get('localhost'.Controller::CACHE_CLEAR_PATH);
2 changes: 1 addition & 1 deletion view/Response.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public static function getStatusTextForCode($code)
return $statusTexts[$code] ?? null;
}

protected static function normalizeHeaderName($name)
protected static function normalizeHeaderName($name): string
{
return preg_replace_callback(
'/\-(.)/',
Expand Down
20 changes: 13 additions & 7 deletions view/View.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -78,7 +78,7 @@ protected static function getTemplateSafely(string $___template, array $___vars)
}
}

public static function markdownToHtml($path)
public static function markdownToHtml($path): string
{
return ParsedownExtra::instance()->text(trim(file_get_contents($path)));
}
Expand Down Expand Up @@ -154,34 +154,40 @@ public static function gzipAssets()
}
}

protected static function interpolateTokens($html)
protected static function interpolateTokens($html): string
{
return preg_replace_callback('/{{[\w\.]+}}/is', function ($m)
{
return i18n::translate(trim($m[0], '}{'));
}, $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)
{
return $v === true ? " $k" : ($v === false || $v === null || ($v === '' && $k != 'value') ? '' : sprintf(' %s="%s"', $k, static::escapeOnce($v)));
}, 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</%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]];
}
}
1 change: 1 addition & 0 deletions view/template/internal/json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo json_encode($json ?? [], JSON_PRETTY_PRINT) ?>