mirror of
https://github.com/flarum/core.git
synced 2025-07-26 19:20:21 +02:00
Lay the groundwork for translation & refactor asset compilation
Ditched the idea of having language packs as extensions. Reasoning: 1. Because we use machine keys for translations (rather than English keys), extensions need to be able to define default translations. If English translations are to be included in extensions and not in a language pack extension, then it doesn’t make sense to have other languages as language pack extensions. Inconsistency → complexity. 2. Translations should maintain version parity with their respective extensions. There’s no way to do this if extension translations are external to the extension. Instead, localisation will be a core effort, as well as a per-extension effort. Translators will be encouraged to send PRs to core + extensions. In core, each locale has a directory containing three files: - translations.yml - config.js: contains pluralisation logic for the JS app, as well as moment.js localisation if necessary - config.php: contains pluralisation logic for the PHP app Extensions can use the Flarum\Extend\Locale extender to add/override translations/config to a locale. Asset compilation has been completely refactored with a better architecture. Translations + config.js are compiled and cached for the currently active locale.
This commit is contained in:
60
framework/core/src/Assets/AssetManager.php
Normal file
60
framework/core/src/Assets/AssetManager.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php namespace Flarum\Assets;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class AssetManager
|
||||
{
|
||||
protected $less;
|
||||
|
||||
protected $js;
|
||||
|
||||
public function __construct(CompilerInterface $js, CompilerInterface $less)
|
||||
{
|
||||
$this->js = $js;
|
||||
$this->less = $less;
|
||||
}
|
||||
|
||||
public function addFile($file)
|
||||
{
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
|
||||
switch ($ext) {
|
||||
case 'js':
|
||||
$this->js->addFile($file);
|
||||
break;
|
||||
|
||||
case 'css':
|
||||
case 'less':
|
||||
$this->less->addFile($file);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new RuntimeException('Unsupported asset type: '.$ext);
|
||||
}
|
||||
}
|
||||
|
||||
public function addFiles(array $files)
|
||||
{
|
||||
array_walk($files, [$this, 'addFile']);
|
||||
}
|
||||
|
||||
public function addLess($string)
|
||||
{
|
||||
$this->less->addString($string);
|
||||
}
|
||||
|
||||
public function addJs($strings)
|
||||
{
|
||||
$this->js->addString($string);
|
||||
}
|
||||
|
||||
public function getCssFile()
|
||||
{
|
||||
return $this->less->getFile();
|
||||
}
|
||||
|
||||
public function getJsFile()
|
||||
{
|
||||
return $this->js->getFile();
|
||||
}
|
||||
}
|
10
framework/core/src/Assets/CompilerInterface.php
Normal file
10
framework/core/src/Assets/CompilerInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php namespace Flarum\Assets;
|
||||
|
||||
interface CompilerInterface
|
||||
{
|
||||
public function addFile($file);
|
||||
|
||||
public function addString($string);
|
||||
|
||||
public function getFile();
|
||||
}
|
9
framework/core/src/Assets/JsCompiler.php
Normal file
9
framework/core/src/Assets/JsCompiler.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php namespace Flarum\Assets;
|
||||
|
||||
class JsCompiler extends RevisionCompiler
|
||||
{
|
||||
public function format($string)
|
||||
{
|
||||
return $string.';';
|
||||
}
|
||||
}
|
26
framework/core/src/Assets/LessCompiler.php
Normal file
26
framework/core/src/Assets/LessCompiler.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php namespace Flarum\Assets;
|
||||
|
||||
use Less_Parser;
|
||||
|
||||
class LessCompiler extends RevisionCompiler
|
||||
{
|
||||
public function compile()
|
||||
{
|
||||
ini_set('xdebug.max_nesting_level', 200);
|
||||
|
||||
$parser = new Less_Parser([
|
||||
'compress' => true,
|
||||
'cache_dir' => storage_path().'/less'
|
||||
]);
|
||||
|
||||
foreach ($this->files as $file) {
|
||||
$parser->parseFile($file);
|
||||
}
|
||||
|
||||
foreach ($this->strings as $string) {
|
||||
$parser->parse($string);
|
||||
}
|
||||
|
||||
return $parser->getCss();
|
||||
}
|
||||
}
|
95
framework/core/src/Assets/RevisionCompiler.php
Normal file
95
framework/core/src/Assets/RevisionCompiler.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php namespace Flarum\Assets;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RevisionCompiler implements CompilerInterface
|
||||
{
|
||||
protected $files = [];
|
||||
|
||||
protected $strings = [];
|
||||
|
||||
public function __construct($path, $filename)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->filename = $filename;
|
||||
}
|
||||
|
||||
public function addFile($file)
|
||||
{
|
||||
$this->files[] = $file;
|
||||
}
|
||||
|
||||
public function addString($string)
|
||||
{
|
||||
$this->strings[] = $string;
|
||||
}
|
||||
|
||||
public function getFile()
|
||||
{
|
||||
if (! ($revision = $this->getRevision())) {
|
||||
$revision = Str::quickRandom();
|
||||
$this->putRevision($revision);
|
||||
}
|
||||
|
||||
$lastModTime = 0;
|
||||
foreach ($this->files as $file) {
|
||||
$lastModTime = max($lastModTime, filemtime($file));
|
||||
}
|
||||
|
||||
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
|
||||
$file = $this->path.'/'.substr_replace($this->filename, '-'.$revision, -strlen($ext) - 1, 0);
|
||||
|
||||
if (! file_exists($file)
|
||||
|| filemtime($file) < $lastModTime) {
|
||||
file_put_contents($file, $this->compile());
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
protected function format($string)
|
||||
{
|
||||
return $string;
|
||||
}
|
||||
|
||||
protected function compile()
|
||||
{
|
||||
$output = '';
|
||||
|
||||
foreach ($this->files as $file) {
|
||||
$output .= $this->format(file_get_contents($file));
|
||||
}
|
||||
|
||||
foreach ($this->strings as $string) {
|
||||
$output .= $this->format($string);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
protected function getRevisionFile()
|
||||
{
|
||||
return $this->path.'/rev-manifest.json';
|
||||
}
|
||||
|
||||
protected function getRevision()
|
||||
{
|
||||
if (file_exists($file = $this->getRevisionFile())) {
|
||||
$manifest = json_decode(file_get_contents($file), true);
|
||||
return array_get($manifest, $this->filename);
|
||||
}
|
||||
}
|
||||
|
||||
protected function putRevision($revision)
|
||||
{
|
||||
if (file_exists($file = $this->getRevisionFile())) {
|
||||
$manifest = json_decode(file_get_contents($file), true);
|
||||
} else {
|
||||
$manifest = [];
|
||||
}
|
||||
|
||||
$manifest[$this->filename] = $revision;
|
||||
|
||||
return file_put_contents($this->getRevisionFile(), json_encode($manifest));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user