Add "clear cache" button to admin

This commit is contained in:
Toby Zerner 2018-11-22 08:03:43 +10:30
parent 1b788a09f7
commit e98a1d33e9
4 changed files with 86 additions and 6 deletions

View File

@ -8,9 +8,11 @@
*/
import DashboardWidget from './DashboardWidget';
import icon from '../../common/helpers/icon';
import listItems from '../../common/helpers/listItems';
import ItemList from '../../common/utils/ItemList';
import Dropdown from '../../common/components/Dropdown';
import Button from '../../common/components/Button';
import LoadingModal from './LoadingModal';
export default class StatusWidget extends DashboardWidget {
className() {
@ -26,10 +28,16 @@ export default class StatusWidget extends DashboardWidget {
items() {
const items = new ItemList();
items.add('help', (
<a href="http://flarum.org/docs/troubleshooting" target="_blank">
{icon('fas fa-question-circle')} {app.translator.trans('core.admin.dashboard.help_link')}
</a>
items.add('tools', (
<Dropdown
label={app.translator.trans('core.admin.dashboard.tools_button')}
icon="fas fa-cog"
buttonClassName="Button"
menuClassName="Dropdown-menu--right">
<Button onclick={this.handleClearCache.bind(this)}>
{app.translator.trans('core.admin.dashboard.clear_cache_button')}
</Button>
</Dropdown>
));
items.add('version-flarum', [<strong>Flarum</strong>, <br/>, app.forum.attribute('version')]);
@ -38,4 +46,13 @@ export default class StatusWidget extends DashboardWidget {
return items;
}
handleClearCache(e) {
app.modal.show(new LoadingModal());
app.request({
method: 'DELETE',
url: app.forum.attribute('apiUrl') + '/cache'
}).then(() => window.location.reload());
}
}

View File

@ -38,7 +38,7 @@
overflow: hidden;
text-overflow: ellipsis;
}
&.item-help {
&.item-tools {
float: right;
margin-right: 0;
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Foundation\Application;
use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Zend\Diactoros\Response\EmptyResponse;
class ClearCacheController extends AbstractDeleteController
{
use AssertPermissionTrait;
/**
* @var CacheClearCommand
*/
protected $command;
/**
* @param CacheClearCommand $command
*/
public function __construct(CacheClearCommand $command)
{
$this->command = $command;
}
/**
* {@inheritdoc}
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$this->command->run(
new ArrayInput([]),
new NullOutput()
);
return new EmptyResponse(204);
}
}

View File

@ -301,4 +301,11 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
'favicon.delete',
$route->toController(Controller\DeleteFaviconController::class)
);
// Clear the cache
$map->delete(
'/cache',
'cache.clear',
$route->toController(Controller\ClearCacheController::class)
);
};