mirror of
https://github.com/flarum/framework.git
synced 2025-02-09 00:03:59 +08:00
![Sami Mazouz](/assets/img/avatar_default.png)
This PR introduces the ability to just override a LESS file's contents through an extender.
This is mainly useful for theme development, as there are times in extensively customized themes where overriding the actual file makes a huge difference vs overriding CSS styles which can turn into a maintenance hell real fast.
Overriding styles is more tedious than overriding files. When you're designing an element, you would normally rather start from a blank canvas, than a styled element. With an already styled element you have to first override and undo the styles you do not wish to have, only then can you start shaping it, but even then you'd always end up constantly undoing default styles. This mostly applies for more advanced themes. (example: 851c55516d/less/forum/DiscussionList.less
)
229 lines
5.0 KiB
PHP
229 lines
5.0 KiB
PHP
<?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;
|
|
|
|
use Flarum\Frontend\Compiler\CompilerInterface;
|
|
use Flarum\Frontend\Compiler\JsCompiler;
|
|
use Flarum\Frontend\Compiler\LessCompiler;
|
|
use Flarum\Frontend\Compiler\Source\SourceCollector;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
|
|
/**
|
|
* A factory class for creating frontend asset compilers.
|
|
*
|
|
* @internal
|
|
*/
|
|
class Assets
|
|
{
|
|
/**
|
|
* @var array
|
|
*/
|
|
public $sources = [
|
|
'js' => [],
|
|
'css' => [],
|
|
'localeJs' => [],
|
|
'localeCss' => []
|
|
];
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $name;
|
|
|
|
/**
|
|
* @var Filesystem
|
|
*/
|
|
protected $assetsDir;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $cacheDir;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $lessImportDirs;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $lessImportOverrides = [];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fileSourceOverrides = [];
|
|
|
|
public function __construct(string $name, Filesystem $assetsDir, string $cacheDir = null, array $lessImportDirs = null)
|
|
{
|
|
$this->name = $name;
|
|
$this->assetsDir = $assetsDir;
|
|
$this->cacheDir = $cacheDir;
|
|
$this->lessImportDirs = $lessImportDirs;
|
|
}
|
|
|
|
public function js($sources)
|
|
{
|
|
$this->addSources('js', $sources);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function css($callback)
|
|
{
|
|
$this->addSources('css', $callback);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function localeJs($callback)
|
|
{
|
|
$this->addSources('localeJs', $callback);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function localeCss($callback)
|
|
{
|
|
$this->addSources('localeCss', $callback);
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function addSources($type, $callback)
|
|
{
|
|
$this->sources[$type][] = $callback;
|
|
}
|
|
|
|
private function populate(CompilerInterface $compiler, string $type, string $locale = null)
|
|
{
|
|
$compiler->addSources(function (SourceCollector $sources) use ($type, $locale) {
|
|
foreach ($this->sources[$type] as $callback) {
|
|
$callback($sources, $locale);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function makeJs(): JsCompiler
|
|
{
|
|
$compiler = $this->makeJsCompiler($this->name.'.js');
|
|
|
|
$this->populate($compiler, 'js');
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
public function makeCss(): LessCompiler
|
|
{
|
|
$compiler = $this->makeLessCompiler($this->name.'.css');
|
|
|
|
$this->populate($compiler, 'css');
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
public function makeLocaleJs(string $locale): JsCompiler
|
|
{
|
|
$compiler = $this->makeJsCompiler($this->name.'-'.$locale.'.js');
|
|
|
|
$this->populate($compiler, 'localeJs', $locale);
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
public function makeLocaleCss(string $locale): LessCompiler
|
|
{
|
|
$compiler = $this->makeLessCompiler($this->name.'-'.$locale.'.css');
|
|
|
|
$this->populate($compiler, 'localeCss', $locale);
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
protected function makeJsCompiler(string $filename)
|
|
{
|
|
return new JsCompiler($this->assetsDir, $filename);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
if ($this->lessImportOverrides) {
|
|
$compiler->setLessImportOverrides($this->lessImportOverrides);
|
|
}
|
|
|
|
if ($this->fileSourceOverrides) {
|
|
$compiler->setFileSourceOverrides($this->fileSourceOverrides);
|
|
}
|
|
|
|
return $compiler;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function setName(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
public function getAssetsDir(): Filesystem
|
|
{
|
|
return $this->assetsDir;
|
|
}
|
|
|
|
public function setAssetsDir(Filesystem $assetsDir)
|
|
{
|
|
$this->assetsDir = $assetsDir;
|
|
}
|
|
|
|
public function getCacheDir(): ?string
|
|
{
|
|
return $this->cacheDir;
|
|
}
|
|
|
|
public function setCacheDir(?string $cacheDir)
|
|
{
|
|
$this->cacheDir = $cacheDir;
|
|
}
|
|
|
|
public function getLessImportDirs(): array
|
|
{
|
|
return $this->lessImportDirs;
|
|
}
|
|
|
|
public function setLessImportDirs(array $lessImportDirs)
|
|
{
|
|
$this->lessImportDirs = $lessImportDirs;
|
|
}
|
|
|
|
public function addLessImportOverrides(array $lessImportOverrides)
|
|
{
|
|
$this->lessImportOverrides = array_merge($this->lessImportOverrides, $lessImportOverrides);
|
|
}
|
|
|
|
public function addFileSourceOverrides(array $fileSourceOverrides)
|
|
{
|
|
$this->fileSourceOverrides = array_merge($this->fileSourceOverrides, $fileSourceOverrides);
|
|
}
|
|
}
|