Skip to content

Commit

Permalink
Make StatusWidget tools extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
luceos authored and rafaucau committed Dec 7, 2021
1 parent b7a9911 commit d217796
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 39 deletions.
1 change: 1 addition & 0 deletions js/dist-typings/admin/components/StatusWidget.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default class StatusWidget extends DashboardWidget {
items(): ItemList<any>;
toolsItems(): ItemList<any>;
handleClearCache(e: any): void;
}
import DashboardWidget from "./DashboardWidget";
Expand Down
13 changes: 12 additions & 1 deletion js/src/admin/components/StatusWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class StatusWidget extends DashboardWidget {
buttonClassName="Button"
menuClassName="Dropdown-menu--right"
>
<Button onclick={this.handleClearCache.bind(this)}>{app.translator.trans('core.admin.dashboard.clear_cache_button')}</Button>
{this.toolsItems().toArray()}
</Dropdown>
);

Expand All @@ -37,6 +37,17 @@ export default class StatusWidget extends DashboardWidget {
return items;
}

toolsItems() {
const items = new ItemList();

items.add(
'clearCache',
<Button onclick={this.handleClearCache.bind(this)}>{app.translator.trans('core.admin.dashboard.clear_cache_button')}</Button>
);

return items;
}

handleClearCache(e) {
app.modal.show(LoadingModal);

Expand Down
55 changes: 55 additions & 0 deletions src/Frontend/Compiler/FileVersioner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Frontend\Compiler;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;

class FileVersioner implements VersionerInterface
{
/**
* @var Filesystem
*/
protected $filesystem;
const REV_MANIFEST = 'rev-manifest.json';

public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

public function putRevision(string $file, ?string $revision)
{
if ($this->filesystem->has(static::REV_MANIFEST)) {
$manifest = json_decode($this->filesystem->read(static::REV_MANIFEST), true);
} else {
$manifest = [];
}

if ($revision) {
$manifest[$file] = $revision;
} else {
unset($manifest[$file]);
}

$this->filesystem->put(static::REV_MANIFEST, json_encode($manifest));
}

public function getRevision(string $file): ?string
{
if ($this->filesystem->has(static::REV_MANIFEST)) {
$manifest = json_decode($this->filesystem->read(static::REV_MANIFEST), true);

return Arr::get($manifest, $file);
}

return null;
}
}
51 changes: 13 additions & 38 deletions src/Frontend/Compiler/RevisionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\Compiler\Source\SourceInterface;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;

/**
* @internal
*/
class RevisionCompiler implements CompilerInterface
{
const REV_MANIFEST = 'rev-manifest.json';

const EMPTY_REVISION = 'empty';

/**
* @var Filesystem
*/
protected $assetsDir;
/**
* @var VersionerInterface
*/
protected $versioner;

/**
* @var string
Expand All @@ -41,11 +42,13 @@ class RevisionCompiler implements CompilerInterface
/**
* @param Filesystem $assetsDir
* @param string $filename
* @param VersionerInterface|null $versioner @deprecated nullable will be removed at v2.0
*/
public function __construct(Filesystem $assetsDir, string $filename)
public function __construct(Filesystem $assetsDir, string $filename, VersionerInterface $versioner = null)
{
$this->assetsDir = $assetsDir;
$this->filename = $filename;
$this->versioner = $versioner ?: new FileVersioner($assetsDir);
}

public function getFilename(): string
Expand All @@ -62,7 +65,7 @@ public function commit(bool $force = false)
{
$sources = $this->getSources();

$oldRevision = $this->getRevision();
$oldRevision = $this->versioner->getRevision($this->filename);

$newRevision = $this->calculateRevision($sources);

Expand All @@ -76,7 +79,7 @@ public function commit(bool $force = false)
$newRevision = static::EMPTY_REVISION;
}

$this->putRevision($newRevision);
$this->versioner->putRevision($this->filename, $newRevision);
}
}

Expand All @@ -101,12 +104,12 @@ protected function getSources(): array

public function getUrl(): ?string
{
$revision = $this->getRevision();
$revision = $this->versioner->getRevision($this->filename);

if (! $revision) {
$this->commit();

$revision = $this->getRevision();
$revision = $this->versioner->getRevision($this->filename);

if (! $revision) {
return null;
Expand Down Expand Up @@ -164,34 +167,6 @@ protected function format(string $string): string
return $string;
}

protected function getRevision(): ?string
{
if ($this->assetsDir->has(static::REV_MANIFEST)) {
$manifest = json_decode($this->assetsDir->read(static::REV_MANIFEST), true);

return Arr::get($manifest, $this->filename);
}

return null;
}

protected function putRevision(?string $revision)
{
if ($this->assetsDir->has(static::REV_MANIFEST)) {
$manifest = json_decode($this->assetsDir->read(static::REV_MANIFEST), true);
} else {
$manifest = [];
}

if ($revision) {
$manifest[$this->filename] = $revision;
} else {
unset($manifest[$this->filename]);
}

$this->assetsDir->put(static::REV_MANIFEST, json_encode($manifest));
}

/**
* @param SourceInterface[] $sources
* @return string
Expand All @@ -214,10 +189,10 @@ protected function getCacheDifferentiator(): ?array

public function flush()
{
if ($this->getRevision() !== null) {
if ($this->versioner->getRevision($this->filename) !== null) {
$this->delete($this->filename);

$this->putRevision(null);
$this->versioner->putRevision($this->filename, null);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/Frontend/Compiler/VersionerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Frontend\Compiler;

interface VersionerInterface
{
public function putRevision(string $file, ?string $revision);

public function getRevision(string $file): ?string;
}

0 comments on commit d217796

Please sign in to comment.