2018-06-30 11:01:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
2019-11-28 08:16:50 +08:00
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
2018-06-30 11:01:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Frontend;
|
|
|
|
|
|
|
|
use Flarum\Frontend\Compiler\CompilerInterface;
|
|
|
|
use Flarum\Frontend\Compiler\JsCompiler;
|
|
|
|
use Flarum\Frontend\Compiler\LessCompiler;
|
|
|
|
use Flarum\Frontend\Compiler\Source\SourceCollector;
|
2018-11-16 11:24:13 +08:00
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
2018-06-30 11:01:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A factory class for creating frontend asset compilers.
|
2021-05-13 22:26:24 +08:00
|
|
|
*
|
|
|
|
* @internal
|
2018-06-30 11:01:12 +08:00
|
|
|
*/
|
2018-11-16 11:24:13 +08:00
|
|
|
class Assets
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
2018-11-16 11:24:13 +08:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sources = [
|
|
|
|
'js' => [],
|
|
|
|
'css' => [],
|
|
|
|
'localeJs' => [],
|
|
|
|
'localeCss' => []
|
|
|
|
];
|
|
|
|
|
2018-06-30 11:01:12 +08:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
2018-11-16 11:24:13 +08:00
|
|
|
* @var Filesystem
|
2018-06-30 11:01:12 +08:00
|
|
|
*/
|
|
|
|
protected $assetsDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $cacheDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $lessImportDirs;
|
|
|
|
|
2021-09-11 01:45:18 +08:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $lessImportOverrides = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fileSourceOverrides = [];
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
public function __construct(string $name, Filesystem $assetsDir, string $cacheDir = null, array $lessImportDirs = null)
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->assetsDir = $assetsDir;
|
|
|
|
$this->cacheDir = $cacheDir;
|
|
|
|
$this->lessImportDirs = $lessImportDirs;
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
public function js($sources)
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
2018-11-16 11:24:13 +08:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
});
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeJs(): JsCompiler
|
|
|
|
{
|
2020-09-30 07:03:51 +08:00
|
|
|
$compiler = $this->makeJsCompiler($this->name.'.js');
|
2018-06-30 11:01:12 +08:00
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
$this->populate($compiler, 'js');
|
2018-06-30 11:01:12 +08:00
|
|
|
|
|
|
|
return $compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeCss(): LessCompiler
|
|
|
|
{
|
|
|
|
$compiler = $this->makeLessCompiler($this->name.'.css');
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
$this->populate($compiler, 'css');
|
2018-06-30 11:01:12 +08:00
|
|
|
|
|
|
|
return $compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeLocaleJs(string $locale): JsCompiler
|
|
|
|
{
|
2020-09-30 07:03:51 +08:00
|
|
|
$compiler = $this->makeJsCompiler($this->name.'-'.$locale.'.js');
|
2018-06-30 11:01:12 +08:00
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
$this->populate($compiler, 'localeJs', $locale);
|
2018-06-30 11:01:12 +08:00
|
|
|
|
|
|
|
return $compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeLocaleCss(string $locale): LessCompiler
|
|
|
|
{
|
|
|
|
$compiler = $this->makeLessCompiler($this->name.'-'.$locale.'.css');
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
$this->populate($compiler, 'localeCss', $locale);
|
2018-06-30 11:01:12 +08:00
|
|
|
|
|
|
|
return $compiler;
|
|
|
|
}
|
|
|
|
|
2020-09-30 07:03:51 +08:00
|
|
|
protected function makeJsCompiler(string $filename)
|
|
|
|
{
|
|
|
|
return new JsCompiler($this->assetsDir, $filename);
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:01:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-11 01:45:18 +08:00
|
|
|
if ($this->lessImportOverrides) {
|
|
|
|
$compiler->setLessImportOverrides($this->lessImportOverrides);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->fileSourceOverrides) {
|
|
|
|
$compiler->setFileSourceOverrides($this->fileSourceOverrides);
|
|
|
|
}
|
|
|
|
|
2018-06-30 11:01:12 +08:00
|
|
|
return $compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName(string $name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
public function getAssetsDir(): Filesystem
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
|
|
|
return $this->assetsDir;
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
public function setAssetsDir(Filesystem $assetsDir)
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
2021-09-11 01:45:18 +08:00
|
|
|
|
|
|
|
public function addLessImportOverrides(array $lessImportOverrides)
|
|
|
|
{
|
|
|
|
$this->lessImportOverrides = array_merge($this->lessImportOverrides, $lessImportOverrides);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addFileSourceOverrides(array $fileSourceOverrides)
|
|
|
|
{
|
|
|
|
$this->fileSourceOverrides = array_merge($this->fileSourceOverrides, $fileSourceOverrides);
|
|
|
|
}
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|