diff --git a/js/forum/Gulpfile.js b/js/forum/Gulpfile.js index 8a1ab4698..2eca8d9f6 100644 --- a/js/forum/Gulpfile.js +++ b/js/forum/Gulpfile.js @@ -30,7 +30,6 @@ gulp({ 'src/**/*.js', '../lib/**/*.js' ], - bootstrapFiles: [], modulePrefix: 'flarum', externalHelpers: true, outputFile: 'dist/app.js' diff --git a/src/Events/BuildClientView.php b/src/Events/BuildClientView.php index 0a76b6bed..2528f91a5 100644 --- a/src/Events/BuildClientView.php +++ b/src/Events/BuildClientView.php @@ -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; + } + } + }} diff --git a/src/Events/RegisterLocales.php b/src/Events/RegisterLocales.php index 0d8e8a86e..737698433 100644 --- a/src/Events/RegisterLocales.php +++ b/src/Events/RegisterLocales.php @@ -16,4 +16,9 @@ class RegisterLocales { $this->manager = $manager; } + + public function addTranslations($locale, $file) + { + $this->manager->addTranslations($locale, $file); + } } diff --git a/src/Locale/JsCompiler.php b/src/Locale/JsCompiler.php index 1d574956a..96b18e10f 100644 --- a/src/Locale/JsCompiler.php +++ b/src/Locale/JsCompiler.php @@ -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; } diff --git a/src/Support/ClientView.php b/src/Support/ClientView.php index c36388c64..c2679a6f2 100644 --- a/src/Support/ClientView.php +++ b/src/Support/ClientView.php @@ -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 . * @@ -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(); } diff --git a/stubs/extension/bootstrap.php b/stubs/extension/bootstrap.php index 1d29892f9..891396609 100644 --- a/stubs/extension/bootstrap.php +++ b/stubs/extension/bootstrap.php @@ -4,6 +4,7 @@ // classes in the src directory to be autoloaded. require __DIR__.'/vendor/autoload.php'; -// Register our service provider with the Flarum application. In here we can -// register bindings and execute code when the application boots. -return $this->app->register('{{namespace}}\{{classPrefix}}ServiceProvider'); +// Return the name of our Extension class. Flarum will register it as a service +// provider, allowing it to register bindings and execute code when the +// application boots. +return '{{namespace}}\Extension'; diff --git a/stubs/extension/js/bootstrap.js b/stubs/extension/js/bootstrap.js deleted file mode 100644 index 421294ea7..000000000 --- a/stubs/extension/js/bootstrap.js +++ /dev/null @@ -1,8 +0,0 @@ -import { extend, override } from 'flarum/extension-utils'; -import app from 'flarum/app'; - -app.initializers.add('{{name}}', function() { - - // @todo - -}); diff --git a/stubs/extension/js/src/main.js b/stubs/extension/js/src/main.js new file mode 100644 index 000000000..ac168d85b --- /dev/null +++ b/stubs/extension/js/src/main.js @@ -0,0 +1,4 @@ +import { extend } from 'flarum/extend'; +import app from 'flarum/app'; + +// TODO diff --git a/stubs/extension/src/Extension.php b/stubs/extension/src/Extension.php new file mode 100644 index 000000000..a64d047d1 --- /dev/null +++ b/stubs/extension/src/Extension.php @@ -0,0 +1,27 @@ +subscribe('{{namespace}}\Listeners\AddClientAssets'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + // + } +} diff --git a/stubs/extension/src/Listeners/AddClientAssets.php b/stubs/extension/src/Listeners/AddClientAssets.php new file mode 100755 index 000000000..4a3634b50 --- /dev/null +++ b/stubs/extension/src/Listeners/AddClientAssets.php @@ -0,0 +1,33 @@ +listen(RegisterLocales::class, __CLASS__.'@addLocale'); + $events->listen(BuildClientView::class, __CLASS__.'@addAssets'); + } + + public function addLocale(RegisterLocales $event) + { + $event->addTranslations('en', __DIR__.'/../../locale/en.yml'); + } + + public function addAssets(BuildClientView $event) + { + $event->forumAssets([ + __DIR__.'/../../js/dist/extension.js', + __DIR__.'/../../less/extension.less' + ]); + + $event->forumBootstrapper('{{name}}/main'); + + $event->forumTranslations([ + // '{{name}}.hello_world' + ]); + } +} diff --git a/stubs/extension/src/ServiceProvider.php b/stubs/extension/src/ServiceProvider.php deleted file mode 100644 index dc71b1235..000000000 --- a/stubs/extension/src/ServiceProvider.php +++ /dev/null @@ -1,39 +0,0 @@ -extend([ - (new Extend\Locale('en'))->translations(__DIR__.'/../locale/en.yml'), - - (new Extend\ForumClient()) - ->assets([ - __DIR__.'/../js/dist/extension.js', - __DIR__.'/../less/extension.less' - ]) - ->translations([ - // Add the keys of translations you would like to be available - // for use by the JS client application. - ]) - ]); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - // - } -} diff --git a/views/app.blade.php b/views/app.blade.php index 57ad2af8d..5a3627f03 100644 --- a/views/app.blade.php +++ b/views/app.blade.php @@ -35,7 +35,11 @@ document: {!! json_encode($document) !!}, session: {!! json_encode($session) !!} }; - initLocale(app); + + @foreach ($bootstrappers as $bootstrapper) + System.import('{{ $bootstrapper }}'); + @endforeach + app.boot(); } catch (e) { document.write('
Something went wrong.
');