Use revision versioner to allow custom asset versioning (#3183)

This commit is contained in:
Daniël Klabbers 2021-12-06 18:49:21 +01:00 committed by GitHub
parent 0649680902
commit 2f086d648d
3 changed files with 85 additions and 38 deletions

View File

@ -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;
}
}

View File

@ -12,21 +12,22 @@ namespace Flarum\Frontend\Compiler;
use Flarum\Frontend\Compiler\Source\SourceCollector; use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\Compiler\Source\SourceInterface; use Flarum\Frontend\Compiler\Source\SourceInterface;
use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
/** /**
* @internal * @internal
*/ */
class RevisionCompiler implements CompilerInterface class RevisionCompiler implements CompilerInterface
{ {
const REV_MANIFEST = 'rev-manifest.json';
const EMPTY_REVISION = 'empty'; const EMPTY_REVISION = 'empty';
/** /**
* @var Filesystem * @var Filesystem
*/ */
protected $assetsDir; protected $assetsDir;
/**
* @var VersionerInterface
*/
protected $versioner;
/** /**
* @var string * @var string
@ -41,11 +42,13 @@ class RevisionCompiler implements CompilerInterface
/** /**
* @param Filesystem $assetsDir * @param Filesystem $assetsDir
* @param string $filename * @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->assetsDir = $assetsDir;
$this->filename = $filename; $this->filename = $filename;
$this->versioner = $versioner ?: new FileVersioner($assetsDir);
} }
public function getFilename(): string public function getFilename(): string
@ -62,7 +65,7 @@ class RevisionCompiler implements CompilerInterface
{ {
$sources = $this->getSources(); $sources = $this->getSources();
$oldRevision = $this->getRevision(); $oldRevision = $this->versioner->getRevision($this->filename);
$newRevision = $this->calculateRevision($sources); $newRevision = $this->calculateRevision($sources);
@ -76,7 +79,7 @@ class RevisionCompiler implements CompilerInterface
$newRevision = static::EMPTY_REVISION; $newRevision = static::EMPTY_REVISION;
} }
$this->putRevision($newRevision); $this->versioner->putRevision($this->filename, $newRevision);
} }
} }
@ -101,12 +104,12 @@ class RevisionCompiler implements CompilerInterface
public function getUrl(): ?string public function getUrl(): ?string
{ {
$revision = $this->getRevision(); $revision = $this->versioner->getRevision($this->filename);
if (! $revision) { if (! $revision) {
$this->commit(); $this->commit();
$revision = $this->getRevision(); $revision = $this->versioner->getRevision($this->filename);
if (! $revision) { if (! $revision) {
return null; return null;
@ -164,34 +167,6 @@ class RevisionCompiler implements CompilerInterface
return $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 * @param SourceInterface[] $sources
* @return string * @return string
@ -214,10 +189,10 @@ class RevisionCompiler implements CompilerInterface
public function flush() public function flush()
{ {
if ($this->getRevision() !== null) { if ($this->versioner->getRevision($this->filename) !== null) {
$this->delete($this->filename); $this->delete($this->filename);
$this->putRevision(null); $this->versioner->putRevision($this->filename, null);
} }
} }

View File

@ -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;
}