From 58eaf79a987caf1e813a15912922afbd9aa1bc55 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 2 Oct 2015 17:42:34 +0930 Subject: [PATCH] API: Split BuildClientView into two separate events Much easier to work with. Extension stub hasn't been updated yet. --- src/Admin/Actions/ClientAction.php | 10 +++++ src/Events/BuildAdminClientView.php | 34 +++++++++++++++++ src/Events/BuildClientView.php | 59 ++++------------------------- src/Events/BuildForumClientView.php | 34 +++++++++++++++++ src/Forum/Actions/ClientAction.php | 10 +++++ src/Support/ClientAction.php | 10 ++++- 6 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 src/Events/BuildAdminClientView.php create mode 100644 src/Events/BuildForumClientView.php diff --git a/src/Admin/Actions/ClientAction.php b/src/Admin/Actions/ClientAction.php index cc2bb2732..7129eff8d 100644 --- a/src/Admin/Actions/ClientAction.php +++ b/src/Admin/Actions/ClientAction.php @@ -11,12 +11,14 @@ namespace Flarum\Admin\Actions; use Flarum\Support\ClientAction as BaseClientAction; +use Flarum\Support\ClientView; use Psr\Http\Message\ServerRequestInterface as Request; use Flarum\Core\Groups\Permission; use Flarum\Api\Client; use Flarum\Core\Settings\SettingsRepository; use Flarum\Locale\LocaleManager; use Flarum\Events\UnserializeConfig; +use Flarum\Events\BuildAdminClientView; class ClientAction extends BaseClientAction { @@ -42,6 +44,14 @@ class ClientAction extends BaseClientAction $this->layout = __DIR__.'/../../../views/admin.blade.php'; } + /** + * {@inheritdoc} + */ + protected function fireEvent(ClientView $view, array &$keys) + { + event(new BuildAdminClientView($this, $view, $keys)); + } + /** * {@inheritdoc} */ diff --git a/src/Events/BuildAdminClientView.php b/src/Events/BuildAdminClientView.php new file mode 100644 index 000000000..6e641d21c --- /dev/null +++ b/src/Events/BuildAdminClientView.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Events; + +use Flarum\Support\ClientView; +use Flarum\Admin\Actions\ClientAction; + +class BuildAdminClientView extends BuildClientView +{ + /** + * @var ClientAction + */ + public $action; + + /** + * @param ClientAction $action + * @param ClientView $view + * @param array $keys + */ + public function __construct(ClientAction $action, ClientView $view, array &$keys) + { + $this->action = $action; + $this->view = $view; + $this->keys = &$keys; + } +} diff --git a/src/Events/BuildClientView.php b/src/Events/BuildClientView.php index 1351bd23c..e8ea0bab2 100644 --- a/src/Events/BuildClientView.php +++ b/src/Events/BuildClientView.php @@ -12,10 +12,8 @@ namespace Flarum\Events; use Flarum\Support\ClientAction; use Flarum\Support\ClientView; -use Flarum\Forum\Actions\ClientAction as ForumClientAction; -use Flarum\Admin\Actions\ClientAction as AdminClientAction; -class BuildClientView +abstract class BuildClientView { /** * @var ClientAction @@ -32,61 +30,20 @@ class BuildClientView */ public $keys; - /** - * @param ClientAction $action - * @param ClientView $view - * @param array $keys - */ - public function __construct($action, $view, &$keys) + public function assets($files) { - $this->action = $action; - $this->view = $view; - $this->keys = &$keys; + $this->view->getAssets()->addFiles((array) $files); } - public function forumAssets($files) + public function bootstrapper($bootstrapper) { - if ($this->action instanceof ForumClientAction) { - $this->view->getAssets()->addFiles((array) $files); - } + $this->view->addBootstrapper($bootstrapper); } - public function forumBootstrapper($bootstrapper) + public function translations(array $keys) { - 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; - } - } - } - - public function adminAssets($files) - { - if ($this->action instanceof AdminClientAction) { - $this->view->getAssets()->addFiles((array) $files); - } - } - - public function adminBootstrapper($bootstrapper) - { - if ($this->action instanceof AdminClientAction) { - $this->view->addBootstrapper($bootstrapper); - } - } - - public function adminTranslations(array $keys) - { - if ($this->action instanceof AdminClientAction) { - foreach ($keys as $key) { - $this->keys[] = $key; - } + foreach ($keys as $key) { + $this->keys[] = $key; } } } diff --git a/src/Events/BuildForumClientView.php b/src/Events/BuildForumClientView.php new file mode 100644 index 000000000..e8ae0f7a1 --- /dev/null +++ b/src/Events/BuildForumClientView.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Events; + +use Flarum\Support\ClientView; +use Flarum\Forum\Actions\ClientAction; + +class BuildForumClientView extends BuildClientView +{ + /** + * @var ClientAction + */ + public $action; + + /** + * @param ClientAction $action + * @param ClientView $view + * @param array $keys + */ + public function __construct(ClientAction $action, ClientView $view, array &$keys) + { + $this->action = $action; + $this->view = $view; + $this->keys = &$keys; + } +} diff --git a/src/Forum/Actions/ClientAction.php b/src/Forum/Actions/ClientAction.php index 4876e0ba5..98f797cad 100644 --- a/src/Forum/Actions/ClientAction.php +++ b/src/Forum/Actions/ClientAction.php @@ -11,9 +11,11 @@ namespace Flarum\Forum\Actions; use Flarum\Support\ClientAction as BaseClientAction; +use Flarum\Support\ClientView; use Flarum\Api\Client; use Flarum\Core\Settings\SettingsRepository; use Flarum\Locale\LocaleManager; +use Flarum\Events\BuildForumClientView; class ClientAction extends BaseClientAction { @@ -39,6 +41,14 @@ class ClientAction extends BaseClientAction $this->layout = __DIR__.'/../../../views/forum.blade.php'; } + /** + * {@inheritdoc} + */ + protected function fireEvent(ClientView $view, array &$keys) + { + event(new BuildForumClientView($this, $view, $keys)); + } + /** * @inheritdoc */ diff --git a/src/Support/ClientAction.php b/src/Support/ClientAction.php index b67025636..60d447bc5 100644 --- a/src/Support/ClientAction.php +++ b/src/Support/ClientAction.php @@ -17,7 +17,6 @@ use Flarum\Assets\LessCompiler; use Flarum\Core; use Flarum\Core\Settings\SettingsRepository; use Flarum\Core\Users\User; -use Flarum\Events\BuildClientView; use Flarum\Locale\JsCompiler as LocaleJsCompiler; use Flarum\Locale\LocaleManager; use Psr\Http\Message\ServerRequestInterface as Request; @@ -122,7 +121,7 @@ abstract class ClientAction extends HtmlAction // compile only the ones we need. $keys = $this->translationKeys; - event(new BuildClientView($this, $view, $keys)); + $this->fireEvent($view, $keys); if ($localeCompiler) { $translations = $this->locales->getTranslations($locale); @@ -135,6 +134,13 @@ abstract class ClientAction extends HtmlAction return $view; } + /** + * @param ClientView $view + * @param array &$keys + * @return void + */ + abstract protected function fireEvent(ClientView $view, array &$keys); + /** * Flush the client's assets so that they will be regenerated from scratch * on the next render.