1
0
mirror of https://github.com/flarum/core.git synced 2025-10-10 22:44:25 +02:00

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

@@ -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);
}
});
}
}