1
0
mirror of https://github.com/flarum/core.git synced 2025-08-24 00:53:27 +02:00

Rework extension bootstrapping

System JS modules don't execute when they're registered, so we need to
import them explicitly. While we're at it, we may as well make the
locale bootstrapper a module too.
This commit is contained in:
Toby Zerner
2015-07-20 18:08:28 +09:30
parent 19fe138770
commit 6b7cf1b6bf
12 changed files with 130 additions and 59 deletions

View File

@@ -2,23 +2,24 @@
use Flarum\Support\ClientAction;
use Flarum\Support\ClientView;
use Flarum\Forum\Actions\ClientAction as ForumClientAction;
class BuildClientView
{
/**
* @var ClientAction
*/
protected $action;
public $action;
/**
* @var ClientView
*/
protected $view;
public $view;
/**
* @var array
*/
protected $keys;
public $keys;
/**
* @param ClientAction $action
@@ -31,4 +32,26 @@ class BuildClientView
$this->view = $view;
$this->keys = &$keys;
}
}
public function forumAssets($files)
{
if ($this->action instanceof ForumClientAction) {
$this->view->getAssets()->addFiles((array) $files);
}
}
public function forumBootstrapper($bootstrapper)
{
if ($this->action instanceof ForumClientAction) {
$this->view->addBootstrapper($bootstrapper);
}
}
public function forumTranslations(array $keys)
{
if ($this->action instanceof ForumClientAction) {
foreach ($keys as $key) {
$this->keys[] = $key;
}
}
}}

View File

@@ -16,4 +16,9 @@ class RegisterLocales
{
$this->manager = $manager;
}
public function addTranslations($locale, $file)
{
$this->manager->addTranslations($locale, $file);
}
}

View File

@@ -13,14 +13,18 @@ class JsCompiler extends RevisionCompiler
public function compile()
{
$output = "var initLocale = function(app) {
app.translator.translations = ".json_encode($this->translations).";";
$output = "System.register('locale', [], function() {
return {
execute: function() {
app.translator.translations = ".json_encode($this->translations).";\n";
foreach ($this->files as $filename) {
$output .= file_get_contents($filename);
}
$output .= "};";
$output .= "}
};
});";
return $output;
}

View File

@@ -47,6 +47,13 @@ class ClientView implements Renderable
*/
protected $layout;
/**
* An array of JS modules to import before booting the app.
*
* @var array
*/
protected $bootstrappers = ['locale'];
/**
* An array of strings to append to the page's <head>.
*
@@ -156,6 +163,16 @@ class ClientView implements Renderable
$this->footStrings[] = $string;
}
/**
* Add a JavaScript module to be imported before the app is booted.
*
* @param string $string
*/
public function addBootstrapper($string)
{
$this->bootstrappers[] = $string;
}
/**
* Get the view's asset manager.
*
@@ -196,6 +213,7 @@ class ClientView implements Renderable
$view->head = implode("\n", $this->headStrings);
$view->foot = implode("\n", $this->footStrings);
$view->bootstrappers = $this->bootstrappers;
return $view->render();
}