From 736f22a31a5fef811fca18de8fd8a367aec4e759 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 16:57:24 -0500 Subject: [PATCH 01/16] Create ExtensionWillBeDisabled --- src/Event/ExtensionWillBeDisabled | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/Event/ExtensionWillBeDisabled diff --git a/src/Event/ExtensionWillBeDisabled b/src/Event/ExtensionWillBeDisabled new file mode 100644 index 000000000..595374266 --- /dev/null +++ b/src/Event/ExtensionWillBeDisabled @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Event; + +use Flarum\Extension\Extension; + +class ExtensionWillBeDisabled +{ + /** + * @var string + */ + protected $extension; + /** + * @param Extension $extension + */ + public function __construct(Extension $extension) + { + $this->extension = $extension; + } +} From 939a1e9ca8fa3cfc23f4d3efe1efefbad5817e1e Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 17:05:41 -0500 Subject: [PATCH 02/16] Forgot the extension :/ --- .../{ExtensionWillBeDisabled => ExtensionWillBeDisabled.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Event/{ExtensionWillBeDisabled => ExtensionWillBeDisabled.php} (100%) diff --git a/src/Event/ExtensionWillBeDisabled b/src/Event/ExtensionWillBeDisabled.php similarity index 100% rename from src/Event/ExtensionWillBeDisabled rename to src/Event/ExtensionWillBeDisabled.php From 58f9c2237562c47b87bf5b2221fd0853aee5874b Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 17:07:00 -0500 Subject: [PATCH 03/16] Create ExtensionWillBeEnabled.php --- src/Event/ExtensionWillBeEnabled.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/Event/ExtensionWillBeEnabled.php diff --git a/src/Event/ExtensionWillBeEnabled.php b/src/Event/ExtensionWillBeEnabled.php new file mode 100644 index 000000000..e17ceb92b --- /dev/null +++ b/src/Event/ExtensionWillBeEnabled.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Event; + +use Flarum\Extension\Extension; + +class ExtensionWillBeEnabled +{ + /** + * @var string + */ + protected $extension; + /** + * @param Extension $extension + */ + public function __construct(Extension $extension) + { + $this->extension = $extension; + } +} From 3702ffa998b321716ef071f955cf73fcb83ab1b5 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 19:14:30 -0500 Subject: [PATCH 04/16] Create ExtensionValidator.php --- src/Core/Listener/ExtensionValidator.php | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/Core/Listener/ExtensionValidator.php diff --git a/src/Core/Listener/ExtensionValidator.php b/src/Core/Listener/ExtensionValidator.php new file mode 100644 index 000000000..142004bfc --- /dev/null +++ b/src/Core/Listener/ExtensionValidator.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Core\Listener; + +use Flarum\Event\ExtensionWillBeEnabled; +use Flarum\Event\ExtensionWillBeDisabled; +use Illuminate\Contracts\Events\Dispatcher; +use Flarum\Http\Exception\MethodNotAllowedException; + +class ExtensionModificationValidator +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ExtensionWillBeEnabled::class, [$this, 'whenExtensionWillBeEnabled']); + $events->listen(ExtensionWillBeDisabled::class, [$this, 'whenExtensionWillBeDisabled']); + } + + /** + * @param ExtensionWillBeEnabled $event + */ + public function whenExtensionWillBeEnabled(ExtensionWillBeEnabled $event) + { + + } + + /** + * @param ExtensionWillBeDisabled $event + * @throws MethodNotAllowedException + */ + public function whenExtensionWillBeDisabled(ExtensionWillBeDisabled $event) + { + if (in_array('flarum-locale', $event->extension->extra)) { + $default_locale = $this->app->make('flarum.settings')->get('default_locale'); + $locale = array_get($event->extension->extra, 'flarum-locale.code'); + if ($locale === $default_locale) { + throw new MethodNotAllowedException('You cannot remove all your language packs!'); + } + } + } +} From c29ea98d485906a8eaf3aaa2fc057c801d6023f7 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:17:54 -0500 Subject: [PATCH 05/16] Add WillBe Modifiers --- src/Extension/ExtensionManager.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 3796ca05f..bb1fb77c6 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -13,6 +13,8 @@ namespace Flarum\Extension; use Flarum\Database\Migrator; use Flarum\Event\ExtensionWasDisabled; use Flarum\Event\ExtensionWasEnabled; +use Flarum\Event\ExtensionWillBeEnabled; +use Flarum\Event\ExtensionWillBeDisabled; use Flarum\Event\ExtensionWasUninstalled; use Flarum\Foundation\Application; use Flarum\Settings\SettingsRepositoryInterface; @@ -110,6 +112,8 @@ class ExtensionManager public function enable($name) { if (! $this->isEnabled($name)) { + $this->dispatcher->fire(new ExtensionWillBeEnabled($extension)); + $extension = $this->getExtension($name); $enabled = $this->getEnabled(); @@ -138,6 +142,8 @@ class ExtensionManager $enabled = $this->getEnabled(); if (($k = array_search($name, $enabled)) !== false) { + $this->dispatcher->fire(new ExtensionWillBeDisabled($extension)); + unset($enabled[$k]); $extension = $this->getExtension($name); From 1a5d7a337d90008b0173ba05d1252680a99e22de Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:19:47 -0500 Subject: [PATCH 06/16] Remove useless code --- src/Core/Listener/ExtensionValidator.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Core/Listener/ExtensionValidator.php b/src/Core/Listener/ExtensionValidator.php index 142004bfc..0f8048a56 100644 --- a/src/Core/Listener/ExtensionValidator.php +++ b/src/Core/Listener/ExtensionValidator.php @@ -10,7 +10,6 @@ namespace Flarum\Core\Listener; -use Flarum\Event\ExtensionWillBeEnabled; use Flarum\Event\ExtensionWillBeDisabled; use Illuminate\Contracts\Events\Dispatcher; use Flarum\Http\Exception\MethodNotAllowedException; @@ -22,18 +21,9 @@ class ExtensionModificationValidator */ public function subscribe(Dispatcher $events) { - $events->listen(ExtensionWillBeEnabled::class, [$this, 'whenExtensionWillBeEnabled']); $events->listen(ExtensionWillBeDisabled::class, [$this, 'whenExtensionWillBeDisabled']); } - /** - * @param ExtensionWillBeEnabled $event - */ - public function whenExtensionWillBeEnabled(ExtensionWillBeEnabled $event) - { - - } - /** * @param ExtensionWillBeDisabled $event * @throws MethodNotAllowedException From c8122a7879071ed345cdb44759820cbe08d2e017 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:23:31 -0500 Subject: [PATCH 07/16] Make StyleCL Happy --- src/Core/Listener/ExtensionValidator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Core/Listener/ExtensionValidator.php b/src/Core/Listener/ExtensionValidator.php index 0f8048a56..49da05f5e 100644 --- a/src/Core/Listener/ExtensionValidator.php +++ b/src/Core/Listener/ExtensionValidator.php @@ -1,4 +1,5 @@ Date: Mon, 12 Sep 2016 22:24:57 -0500 Subject: [PATCH 08/16] StyleCL --- src/Event/ExtensionWillBeEnabled.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Event/ExtensionWillBeEnabled.php b/src/Event/ExtensionWillBeEnabled.php index e17ceb92b..e0a9214e3 100644 --- a/src/Event/ExtensionWillBeEnabled.php +++ b/src/Event/ExtensionWillBeEnabled.php @@ -1,4 +1,5 @@ Date: Mon, 12 Sep 2016 22:26:22 -0500 Subject: [PATCH 09/16] StyleCl --- src/Extension/ExtensionManager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index bb1fb77c6..29971a418 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -13,9 +13,9 @@ namespace Flarum\Extension; use Flarum\Database\Migrator; use Flarum\Event\ExtensionWasDisabled; use Flarum\Event\ExtensionWasEnabled; +use Flarum\Event\ExtensionWasUninstalled; use Flarum\Event\ExtensionWillBeEnabled; use Flarum\Event\ExtensionWillBeDisabled; -use Flarum\Event\ExtensionWasUninstalled; use Flarum\Foundation\Application; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; @@ -113,7 +113,7 @@ class ExtensionManager { if (! $this->isEnabled($name)) { $this->dispatcher->fire(new ExtensionWillBeEnabled($extension)); - + $extension = $this->getExtension($name); $enabled = $this->getEnabled(); @@ -143,7 +143,7 @@ class ExtensionManager if (($k = array_search($name, $enabled)) !== false) { $this->dispatcher->fire(new ExtensionWillBeDisabled($extension)); - + unset($enabled[$k]); $extension = $this->getExtension($name); From b048498b846f1a8f023fe43c109b0f359a829ef7 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:27:18 -0500 Subject: [PATCH 10/16] StyleCl --- src/Extension/ExtensionManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 29971a418..8a07bc9b9 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -14,8 +14,8 @@ use Flarum\Database\Migrator; use Flarum\Event\ExtensionWasDisabled; use Flarum\Event\ExtensionWasEnabled; use Flarum\Event\ExtensionWasUninstalled; -use Flarum\Event\ExtensionWillBeEnabled; use Flarum\Event\ExtensionWillBeDisabled; +use Flarum\Event\ExtensionWillBeEnabled; use Flarum\Foundation\Application; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; From 1192867c4f0ce77ed0759d9a3a067d5f1888ffd6 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:28:03 -0500 Subject: [PATCH 11/16] StyleCl --- src/Event/ExtensionWillBeDisabled.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Event/ExtensionWillBeDisabled.php b/src/Event/ExtensionWillBeDisabled.php index 595374266..6df58a41c 100644 --- a/src/Event/ExtensionWillBeDisabled.php +++ b/src/Event/ExtensionWillBeDisabled.php @@ -1,4 +1,5 @@ Date: Mon, 12 Sep 2016 22:28:50 -0500 Subject: [PATCH 12/16] StyleCl FINALLY! --- src/Extension/ExtensionManager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 8a07bc9b9..0e008230a 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -1,4 +1,5 @@ Date: Mon, 12 Sep 2016 22:31:03 -0500 Subject: [PATCH 13/16] Forgot to subscribe --- src/Core/CoreServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Core/CoreServiceProvider.php b/src/Core/CoreServiceProvider.php index 478baf97d..68ec477e0 100644 --- a/src/Core/CoreServiceProvider.php +++ b/src/Core/CoreServiceProvider.php @@ -105,6 +105,7 @@ class CoreServiceProvider extends AbstractServiceProvider $events->subscribe('Flarum\Core\Listener\DiscussionMetadataUpdater'); $events->subscribe('Flarum\Core\Listener\UserMetadataUpdater'); + $events->subscribe('Flarum\Core\Listener\ExtensionValidator'); $events->subscribe('Flarum\Core\Listener\EmailConfirmationMailer'); $events->subscribe('Flarum\Core\Listener\DiscussionRenamedNotifier'); From c702e911b36900196612bf6f79a9eb952a170072 Mon Sep 17 00:00:00 2001 From: Davis Date: Mon, 12 Sep 2016 22:31:55 -0500 Subject: [PATCH 14/16] StyleCl is making me hate myself --- src/Core/CoreServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Core/CoreServiceProvider.php b/src/Core/CoreServiceProvider.php index 68ec477e0..d6c3f7de2 100644 --- a/src/Core/CoreServiceProvider.php +++ b/src/Core/CoreServiceProvider.php @@ -1,4 +1,5 @@ Date: Tue, 4 Oct 2016 15:09:43 -0500 Subject: [PATCH 15/16] Change exception message --- src/Core/Listener/ExtensionValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Listener/ExtensionValidator.php b/src/Core/Listener/ExtensionValidator.php index 49da05f5e..2f25af476 100644 --- a/src/Core/Listener/ExtensionValidator.php +++ b/src/Core/Listener/ExtensionValidator.php @@ -35,7 +35,7 @@ class ExtensionValidator $default_locale = $this->app->make('flarum.settings')->get('default_locale'); $locale = array_get($event->extension->extra, 'flarum-locale.code'); if ($locale === $default_locale) { - throw new MethodNotAllowedException('You cannot remove all your language packs!'); + throw new MethodNotAllowedException('You cannot disable the default language pack!'); } } } From f3bdc163fad857b93d2f1483a7041722f4801d34 Mon Sep 17 00:00:00 2001 From: Davis Date: Wed, 5 Oct 2016 12:46:14 -0500 Subject: [PATCH 16/16] $extension was undefined --- src/Extension/ExtensionManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Extension/ExtensionManager.php b/src/Extension/ExtensionManager.php index 0e008230a..f0e62838c 100644 --- a/src/Extension/ExtensionManager.php +++ b/src/Extension/ExtensionManager.php @@ -113,9 +113,9 @@ class ExtensionManager public function enable($name) { if (! $this->isEnabled($name)) { - $this->dispatcher->fire(new ExtensionWillBeEnabled($extension)); - $extension = $this->getExtension($name); + + $this->dispatcher->fire(new ExtensionWillBeEnabled($extension)); $enabled = $this->getEnabled(); @@ -143,12 +143,12 @@ class ExtensionManager $enabled = $this->getEnabled(); if (($k = array_search($name, $enabled)) !== false) { + $extension = $this->getExtension($name); + $this->dispatcher->fire(new ExtensionWillBeDisabled($extension)); unset($enabled[$k]); - $extension = $this->getExtension($name); - $this->setEnabled($enabled); $extension->setEnabled(false);