From 7e1b343900ca7ed13e1c8448f321186ed73c19f5 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 5 May 2015 14:30:45 +0930 Subject: [PATCH] Add a base ServiceProvider with useful public APIs --- .../NotificationServiceProvider.php | 14 ++-- .../core/src/Core/Notifications/Notifier.php | 11 +-- .../Extensions/ExtensionsServiceProvider.php | 1 + .../core/src/Support/ServiceProvider.php | 78 +++++++++++++++++++ 4 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 framework/core/src/Support/ServiceProvider.php diff --git a/framework/core/src/Core/Notifications/NotificationServiceProvider.php b/framework/core/src/Core/Notifications/NotificationServiceProvider.php index d4417ec7c..964c57cc7 100644 --- a/framework/core/src/Core/Notifications/NotificationServiceProvider.php +++ b/framework/core/src/Core/Notifications/NotificationServiceProvider.php @@ -1,8 +1,9 @@ subscribe('Flarum\Core\Handlers\Events\DiscussionRenamedNotifier'); $notifier->registerMethod('alert', 'Flarum\Core\Notifications\Senders\NotificationAlerter'); $notifier->registerMethod('email', 'Flarum\Core\Notifications\Senders\NotificationEmailer'); - $notifier->registerType('Flarum\Core\Notifications\Types\DiscussionRenamedNotification', ['alert' => true]); - - $events->subscribe('Flarum\Core\Handlers\Events\DiscussionRenamedNotifier'); + $this->notificationType('Flarum\Core\Notifications\Types\DiscussionRenamedNotification', ['alert' => true]); } public function register() @@ -31,5 +30,6 @@ class NotificationServiceProvider extends ServiceProvider ); $this->app->singleton('Flarum\Core\Notifications\Notifier'); + $this->app->alias('Flarum\Core\Notifications\Notifier', 'flarum.notifier'); } } diff --git a/framework/core/src/Core/Notifications/Notifier.php b/framework/core/src/Core/Notifications/Notifier.php index abcf207ea..66bc0be85 100644 --- a/framework/core/src/Core/Notifications/Notifier.php +++ b/framework/core/src/Core/Notifications/Notifier.php @@ -33,18 +33,9 @@ class Notifier $this->methods[$name] = $class; } - public function registerType($class, $defaultPreferences = []) + public function registerType($class) { $this->types[] = $class; - - NotificationModel::registerType($class); - - foreach ($this->methods as $method => $sender) { - $sender = $this->container->make($sender); - if ($sender->compatibleWith($class)) { - User::registerPreference(User::notificationPreferenceKey($class::getType(), $method), 'boolval', array_get($defaultPreferences, $method, false)); - } - } } public function getMethods() diff --git a/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php b/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php index 694b23db8..97955140b 100644 --- a/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php +++ b/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php @@ -22,6 +22,7 @@ class ExtensionsServiceProvider extends ServiceProvider */ public function register() { + $app = $this->app; $extensions = json_decode(DB::table('config')->where('key', 'extensions_enabled')->pluck('value'), true); foreach ($extensions as $extension) { diff --git a/framework/core/src/Support/ServiceProvider.php b/framework/core/src/Support/ServiceProvider.php new file mode 100644 index 000000000..715d59fbb --- /dev/null +++ b/framework/core/src/Support/ServiceProvider.php @@ -0,0 +1,78 @@ +app['events']->listen('Flarum\Forum\Events\RenderView', function ($event) use ($assets) { + $event->assets->addFile($assets); + }); + } + + protected function postType($class) + { + Post::addType($class); + } + + protected function discussionGambit($class) + { + $this->app['events']->listen('Flarum\Core\Events\RegisterDiscussionGambits', function ($event) use ($class) { + $event->gambits->add($class); + }); + } + + protected function notificationType($class, $defaultPreferences = []) + { + $notifier = $this->app['flarum.notifier']; + + $notifier->registerType($class); + + Notification::registerType($class); + + foreach ($notifier->getMethods() as $method => $sender) { + if ($sender::compatibleWith($class)) { + User::registerPreference(User::notificationPreferenceKey($class::getType(), $method), 'boolval', array_get($defaultPreferences, $method, false)); + } + } + } + + protected function relationship($parent, $type, $name, $child = null) + { + $parent::addRelationship($name, function ($model) use ($type, $name, $child) { + if ($type instanceof Closure) { + return $type($model); + } elseif ($type === 'belongsTo') { + return $model->belongsTo($child, null, null, $name); + } else { + // @todo + } + }); + } + + protected function serializeRelationship($parent, $type, $name, $child = null) + { + $parent::addRelationship($name, function ($serializer) use ($type, $name, $child) { + if ($type instanceof Closure) { + return $type(); + } else { + return $serializer->$type($child, $name); + } + }); + } +}