mirror of
https://github.com/flarum/framework.git
synced 2024-11-24 00:30:47 +08:00
Add a base ServiceProvider with useful public APIs
This commit is contained in:
parent
2850c1b38c
commit
92a75fd786
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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) {
|
||||
|
|
78
src/Support/ServiceProvider.php
Normal file
78
src/Support/ServiceProvider.php
Normal 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user