1
0
mirror of https://github.com/flarum/core.git synced 2025-10-14 16:34:26 +02:00

Frontend refactor (#1471)

Refactor Frontend + Asset code

- Use Laravel's Filesystem component for asset IO, meaning theoretically
  assets should be storable on S3 etc.

- More reliable checking for asset recompilation when debug mode is on,
  so you don't have to constantly delete the compiled assets to force
  a recompile. Should also fix issues with locale JS files being
  recompiled with the same name and cached.

- Remove JavaScript minification, because it will be done by Webpack
  (exception is for the TextFormatter JS).

- Add support for JS sourcemaps.

- Separate frontend view and assets completely. This is an important
  distinction because frontend assets are compiled independent of a
  request, whereas putting together a view depends on a request.

- Bind frontend view/asset factory instances to the container (in
  service providers) rather than subclassing. Asset and content
  populators can be added to these factories – these are simply objects
  that populate the asset compilers or the view with information.

- Add RouteHandlerFactory functions that make it easy to hook up a
  frontend controller with a frontend instance ± some content.

- Remove the need for "nojs"

- Fix cache:clear command

- Recompile assets when settings/enabled extensions change
This commit is contained in:
Toby Zerner
2018-06-30 12:31:12 +09:30
committed by GitHub
parent 0f5ddc1c43
commit 0e73785498
73 changed files with 2846 additions and 2176 deletions

View File

@@ -0,0 +1,39 @@
<?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\Frontend\Compiler;
interface CompilerInterface
{
/**
* @return string
*/
public function getFilename(): string;
/**
* @param string $filename
*/
public function setFilename(string $filename);
/**
* @param callable $callback
*/
public function addSources(callable $callback);
public function commit();
/**
* @return string|null
*/
public function getUrl(): ?string;
public function flush();
}

View File

@@ -0,0 +1,84 @@
<?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\Frontend\Compiler;
use axy\sourcemap\SourceMap;
use Flarum\Frontend\Compiler\Source\FileSource;
class JsCompiler extends RevisionCompiler
{
/**
* {@inheritdoc}
*/
protected function save(string $file, array $sources): bool
{
$mapFile = $file.'.map';
$map = new SourceMap();
$map->file = $mapFile;
$output = [];
$line = 0;
// For each of the sources, get their content and add it to the
// output. For file sources, if a sourcemap is present, add it to
// the output sourcemap.
foreach ($sources as $source) {
$content = $source->getContent();
if ($source instanceof FileSource) {
$sourceMap = $source->getPath().'.map';
if (file_exists($sourceMap)) {
$map->concat($sourceMap, $line);
}
}
$content = $this->format($content);
$output[] = $content;
$line += substr_count($content, "\n") + 1;
}
// Add a comment to the end of our file to point to the sourcemap
// we just constructed. We will then write the JS file, save the
// map to a temporary location, and then move it to the asset dir.
$output[] = '//# sourceMappingURL='.$this->assetsDir->url($mapFile);
$this->assetsDir->put($file, implode("\n", $output));
$mapTemp = tempnam(sys_get_temp_dir(), $mapFile);
$map->save($mapTemp);
$this->assetsDir->put($mapFile, file_get_contents($mapTemp));
@unlink($mapTemp);
return true;
}
/**
* {@inheritdoc}
*/
protected function format(string $string): string
{
return preg_replace('~//# sourceMappingURL.*$~s', '', $string).";\n";
}
/**
* {@inheritdoc}
*/
protected function delete(string $file)
{
parent::delete($file);
if ($this->assetsDir->has($mapFile = $file.'.map')) {
$this->assetsDir->delete($mapFile);
}
}
}

View File

@@ -0,0 +1,96 @@
<?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\Frontend\Compiler;
use Flarum\Frontend\Compiler\Source\FileSource;
use Less_Parser;
class LessCompiler extends RevisionCompiler
{
/**
* @var string
*/
protected $cacheDir;
/**
* @var array
*/
protected $importDirs = [];
/**
* @return string
*/
public function getCacheDir(): string
{
return $this->cacheDir;
}
/**
* @param string $cacheDir
*/
public function setCacheDir(string $cacheDir)
{
$this->cacheDir = $cacheDir;
}
/**
* @return array
*/
public function getImportDirs(): array
{
return $this->importDirs;
}
/**
* @param array $importDirs
*/
public function setImportDirs(array $importDirs)
{
$this->importDirs = $importDirs;
}
/**
* {@inheritdoc}
*/
protected function compile(array $sources): string
{
if (! count($sources)) {
return '';
}
ini_set('xdebug.max_nesting_level', 200);
$parser = new Less_Parser([
'compress' => true,
'cache_dir' => $this->cacheDir,
'import_dirs' => $this->importDirs
]);
foreach ($sources as $source) {
if ($source instanceof FileSource) {
$parser->parseFile($source->getPath());
} else {
$parser->parse($source->getContent());
}
}
return $parser->getCss();
}
/**
* @return mixed
*/
protected function getCacheDifferentiator()
{
return time();
}
}

View File

@@ -0,0 +1,276 @@
<?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\Frontend\Compiler;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\Compiler\Source\SourceInterface;
use Illuminate\Filesystem\FilesystemAdapter;
class RevisionCompiler implements CompilerInterface
{
const REV_MANIFEST = 'rev-manifest.json';
const EMPTY_REVISION = 'empty';
/**
* @var FilesystemAdapter
*/
protected $assetsDir;
/**
* @var string
*/
protected $filename;
/**
* @var callable[]
*/
protected $sourcesCallbacks = [];
/**
* @param FilesystemAdapter $assetsDir
* @param string $filename
*/
public function __construct(FilesystemAdapter $assetsDir, string $filename)
{
$this->assetsDir = $assetsDir;
$this->filename = $filename;
}
/**
* {@inheritdoc}
*/
public function getFilename(): string
{
return $this->filename;
}
/**
* {@inheritdoc}
*/
public function setFilename(string $filename)
{
$this->filename = $filename;
}
/**
* {@inheritdoc}
*/
public function commit()
{
$sources = $this->getSources();
$oldRevision = $this->getRevision();
$newRevision = $this->calculateRevision($sources);
$oldFile = $oldRevision ? $this->getFilenameForRevision($oldRevision) : null;
if ($oldRevision !== $newRevision || ($oldFile && ! $this->assetsDir->has($oldFile))) {
$newFile = $this->getFilenameForRevision($newRevision);
if (! $this->save($newFile, $sources)) {
// If no file was written (because the sources were empty), we
// will set the revision to a special value so that we can tell
// that this file does not have a URL.
$newRevision = static::EMPTY_REVISION;
}
$this->putRevision($newRevision);
if ($oldFile) {
$this->delete($oldFile);
}
}
}
/**
* {@inheritdoc}
*/
public function addSources(callable $callback)
{
$this->sourcesCallbacks[] = $callback;
}
/**
* @return SourceInterface[]
*/
protected function getSources()
{
$sources = new SourceCollector;
foreach ($this->sourcesCallbacks as $callback) {
$callback($sources);
}
return $sources->getSources();
}
/**
* {@inheritdoc}
*/
public function getUrl(): ?string
{
$revision = $this->getRevision();
if (! $revision) {
$this->commit();
$revision = $this->getRevision();
if (! $revision) {
return null;
}
}
if ($revision === static::EMPTY_REVISION) {
return null;
}
$file = $this->getFilenameForRevision($revision);
return $this->assetsDir->url($file);
}
/**
* @param string $file
* @param SourceInterface[] $sources
* @return bool true if the file was written, false if there was nothing to write
*/
protected function save(string $file, array $sources): bool
{
if ($content = $this->compile($sources)) {
$this->assetsDir->put($file, $content);
return true;
}
return false;
}
/**
* @param SourceInterface[] $sources
* @return string
*/
protected function compile(array $sources): string
{
$output = '';
foreach ($sources as $source) {
$output .= $this->format($source->getContent());
}
return $output;
}
/**
* @param string $string
* @return string
*/
protected function format(string $string): string
{
return $string;
}
/**
* Get the filename for the given revision.
*
* @param string $revision
* @return string
*/
protected function getFilenameForRevision(string $revision): string
{
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
return substr_replace($this->filename, '-'.$revision, -strlen($ext) - 1, 0);
}
/**
* @return string|null
*/
protected function getRevision(): ?string
{
if ($this->assetsDir->has(static::REV_MANIFEST)) {
$manifest = json_decode($this->assetsDir->read(static::REV_MANIFEST), true);
return array_get($manifest, $this->filename);
}
return null;
}
/**
* @param string|null $revision
*/
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
*/
protected function calculateRevision(array $sources): string
{
$cacheDifferentiator = [$this->getCacheDifferentiator()];
foreach ($sources as $source) {
$cacheDifferentiator[] = $source->getCacheDifferentiator();
}
return hash('crc32b', serialize($cacheDifferentiator));
}
/**
* @return mixed
*/
protected function getCacheDifferentiator()
{
}
/**
* {@inheritdoc}
*/
public function flush()
{
if ($revision = $this->getRevision()) {
$file = $this->getFilenameForRevision($revision);
$this->delete($file);
$this->putRevision(null);
}
}
/**
* @param string $file
*/
protected function delete(string $file)
{
if ($this->assetsDir->has($file)) {
$this->assetsDir->delete($file);
}
}
}

View File

@@ -0,0 +1,52 @@
<?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\Frontend\Compiler\Source;
class FileSource implements SourceInterface
{
/**
* @var string
*/
protected $path;
/**
* @param string $path
*/
public function __construct(string $path)
{
$this->path = $path;
}
/**
* @return string
*/
public function getContent(): string
{
return file_get_contents($this->path);
}
/**
* @return mixed
*/
public function getCacheDifferentiator()
{
return [$this->path, filemtime($this->path)];
}
/**
* @return string
*/
public function getPath(): string
{
return $this->path;
}
}

View File

@@ -0,0 +1,50 @@
<?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\Frontend\Compiler\Source;
class SourceCollector
{
/**
* @var SourceInterface[]
*/
protected $sources = [];
/**
* @param string $file
* @return $this
*/
public function addFile(string $file)
{
$this->sources[] = new FileSource($file);
return $this;
}
/**
* @param callable $callback
* @return $this
*/
public function addString(callable $callback)
{
$this->sources[] = new StringSource($callback);
return $this;
}
/**
* @return SourceInterface[]
*/
public function getSources()
{
return $this->sources;
}
}

View File

@@ -0,0 +1,25 @@
<?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\Frontend\Compiler\Source;
interface SourceInterface
{
/**
* @return string
*/
public function getContent(): string;
/**
* @return mixed
*/
public function getCacheDifferentiator();
}

View File

@@ -0,0 +1,50 @@
<?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\Frontend\Compiler\Source;
class StringSource implements SourceInterface
{
/**
* @var callable
*/
protected $callback;
private $content;
/**
* @param callable $callback
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}
/**
* @return string
*/
public function getContent(): string
{
if (is_null($this->content)) {
$this->content = call_user_func($this->callback);
}
return $this->content;
}
/**
* @return mixed
*/
public function getCacheDifferentiator()
{
return $this->getContent();
}
}