Add a base ServiceProvider with useful public APIs

This commit is contained in:
Toby Zerner 2015-05-05 14:30:45 +09:30
parent 2850c1b38c
commit 92a75fd786
4 changed files with 87 additions and 17 deletions

View File

@ -1,8 +1,9 @@
<?php namespace Flarum\Core\Notifications;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use Flarum\Support\ServiceProvider;
use Flarum\Core\Models\User;
use Flarum\Core\Notifications\Notifier;
use Illuminate\Contracts\Events\Dispatcher;
class NotificationServiceProvider extends ServiceProvider
{
@ -11,16 +12,14 @@ class NotificationServiceProvider extends ServiceProvider
*
* @return void
*/
public function boot(Dispatcher $events)
public function boot(Dispatcher $events, Notifier $notifier)
{
$notifier = app('Flarum\Core\Notifications\Notifier');
$events->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');
}
}

View File

@ -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()

View File

@ -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) {

View File

@ -0,0 +1,78 @@
<?php namespace Flarum\Support;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Core\Models\Notification;
use Flarum\Core\Models\User;
use Flarum\Core\Models\Post;
class ServiceProvider extends IlluminateServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
protected function forumAssets($assets)
{
$this->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);
}
});
}
}