mirror of
https://github.com/flarum/framework.git
synced 2025-02-15 22:52:56 +08:00
![Toby Zerner](/assets/img/avatar_default.png)
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
240 lines
5.3 KiB
PHP
240 lines
5.3 KiB
PHP
<?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;
|
|
|
|
use Flarum\Frontend\Asset\AssetInterface;
|
|
use Flarum\Frontend\Compiler\CompilerInterface;
|
|
use Flarum\Frontend\Compiler\JsCompiler;
|
|
use Flarum\Frontend\Compiler\LessCompiler;
|
|
use Flarum\Frontend\Compiler\Source\SourceCollector;
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
|
|
/**
|
|
* A factory class for creating frontend asset compilers.
|
|
*/
|
|
class CompilerFactory
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $name;
|
|
|
|
/**
|
|
* @var FilesystemAdapter
|
|
*/
|
|
protected $assetsDir;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $cacheDir;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $lessImportDirs;
|
|
|
|
/**
|
|
* @var AssetInterface[]
|
|
*/
|
|
protected $assets = [];
|
|
|
|
/**
|
|
* @var callable[]
|
|
*/
|
|
protected $addCallbacks = [];
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param FilesystemAdapter $assetsDir
|
|
* @param string $cacheDir
|
|
* @param array|null $lessImportDirs
|
|
*/
|
|
public function __construct(string $name, FilesystemAdapter $assetsDir, string $cacheDir = null, array $lessImportDirs = null)
|
|
{
|
|
$this->name = $name;
|
|
$this->assetsDir = $assetsDir;
|
|
$this->cacheDir = $cacheDir;
|
|
$this->lessImportDirs = $lessImportDirs;
|
|
}
|
|
|
|
/**
|
|
* @param callable $callback
|
|
*/
|
|
public function add(callable $callback)
|
|
{
|
|
$this->addCallbacks[] = $callback;
|
|
}
|
|
|
|
/**
|
|
* @return JsCompiler
|
|
*/
|
|
public function makeJs(): JsCompiler
|
|
{
|
|
$compiler = new JsCompiler($this->assetsDir, $this->name.'.js');
|
|
|
|
$this->addSources($compiler, function (AssetInterface $asset, SourceCollector $sources) {
|
|
$asset->js($sources);
|
|
});
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
/**
|
|
* @return LessCompiler
|
|
*/
|
|
public function makeCss(): LessCompiler
|
|
{
|
|
$compiler = $this->makeLessCompiler($this->name.'.css');
|
|
|
|
$this->addSources($compiler, function (AssetInterface $asset, SourceCollector $sources) {
|
|
$asset->css($sources);
|
|
});
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
/**
|
|
* @param string $locale
|
|
* @return JsCompiler
|
|
*/
|
|
public function makeLocaleJs(string $locale): JsCompiler
|
|
{
|
|
$compiler = new JsCompiler($this->assetsDir, $this->name.'-'.$locale.'.js');
|
|
|
|
$this->addSources($compiler, function (AssetInterface $asset, SourceCollector $sources) use ($locale) {
|
|
$asset->localeJs($sources, $locale);
|
|
});
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
/**
|
|
* @param string $locale
|
|
* @return LessCompiler
|
|
*/
|
|
public function makeLocaleCss(string $locale): LessCompiler
|
|
{
|
|
$compiler = $this->makeLessCompiler($this->name.'-'.$locale.'.css');
|
|
|
|
$this->addSources($compiler, function (AssetInterface $asset, SourceCollector $sources) use ($locale) {
|
|
$asset->localeCss($sources, $locale);
|
|
});
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
/**
|
|
* @param string $filename
|
|
* @return LessCompiler
|
|
*/
|
|
protected function makeLessCompiler(string $filename): LessCompiler
|
|
{
|
|
$compiler = new LessCompiler($this->assetsDir, $filename);
|
|
|
|
if ($this->cacheDir) {
|
|
$compiler->setCacheDir($this->cacheDir.'/less');
|
|
}
|
|
|
|
if ($this->lessImportDirs) {
|
|
$compiler->setImportDirs($this->lessImportDirs);
|
|
}
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
protected function fireAddCallbacks()
|
|
{
|
|
foreach ($this->addCallbacks as $callback) {
|
|
$assets = $callback($this);
|
|
$this->assets = array_merge($this->assets, is_array($assets) ? $assets : [$assets]);
|
|
}
|
|
|
|
$this->addCallbacks = [];
|
|
}
|
|
|
|
private function addSources(CompilerInterface $compiler, callable $callback)
|
|
{
|
|
$compiler->addSources(function ($sources) use ($callback) {
|
|
$this->fireAddCallbacks();
|
|
|
|
foreach ($this->assets as $asset) {
|
|
$callback($asset, $sources);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
*/
|
|
public function setName(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
/**
|
|
* @return FilesystemAdapter
|
|
*/
|
|
public function getAssetsDir(): FilesystemAdapter
|
|
{
|
|
return $this->assetsDir;
|
|
}
|
|
|
|
/**
|
|
* @param FilesystemAdapter $assetsDir
|
|
*/
|
|
public function setAssetsDir(FilesystemAdapter $assetsDir)
|
|
{
|
|
$this->assetsDir = $assetsDir;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getCacheDir(): ?string
|
|
{
|
|
return $this->cacheDir;
|
|
}
|
|
|
|
/**
|
|
* @param string $cacheDir
|
|
*/
|
|
public function setCacheDir(?string $cacheDir)
|
|
{
|
|
$this->cacheDir = $cacheDir;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getLessImportDirs(): array
|
|
{
|
|
return $this->lessImportDirs;
|
|
}
|
|
|
|
/**
|
|
* @param array $lessImportDirs
|
|
*/
|
|
public function setLessImportDirs(array $lessImportDirs)
|
|
{
|
|
$this->lessImportDirs = $lessImportDirs;
|
|
}
|
|
}
|