app->extend(UrlGenerator::class, function (UrlGenerator $url) { return $url->addCollection('api', $this->app->make('flarum.api.routes'), 'api'); }); $this->app->singleton('flarum.api.routes', function () { $routes = new RouteCollection; $this->populateRoutes($routes); return $routes; }); $this->app->singleton('flarum.api.middleware', function () { return [ 'flarum.api.error_handler', HttpMiddleware\ParseJsonBody::class, Middleware\FakeHttpMethods::class, HttpMiddleware\StartSession::class, HttpMiddleware\RememberFromCookie::class, HttpMiddleware\AuthenticateWithSession::class, HttpMiddleware\AuthenticateWithHeader::class, HttpMiddleware\CheckCsrfToken::class, HttpMiddleware\SetLocale::class, ]; }); $this->app->bind('flarum.api.error_handler', function () { return new HttpMiddleware\HandleErrors( $this->app->make(Registry::class), new JsonApiFormatter($this->app['flarum']->inDebugMode()), $this->app->tagged(Reporter::class) ); }); $this->app->singleton('flarum.api.handler', function () { $pipe = new MiddlewarePipe; foreach ($this->app->make('flarum.api.middleware') as $middleware) { $pipe->pipe($this->app->make($middleware)); } $pipe->pipe(new HttpMiddleware\DispatchRoute($this->app->make('flarum.api.routes'))); return $pipe; }); } /** * {@inheritdoc} */ public function boot() { $this->registerNotificationSerializers(); AbstractSerializeController::setContainer($this->app); AbstractSerializeController::setEventDispatcher($events = $this->app->make('events')); AbstractSerializer::setContainer($this->app); AbstractSerializer::setEventDispatcher($events); } /** * Register notification serializers. */ protected function registerNotificationSerializers() { $blueprints = []; $serializers = [ 'discussionRenamed' => BasicDiscussionSerializer::class ]; $this->app->make('events')->dispatch( new ConfigureNotificationTypes($blueprints, $serializers) ); foreach ($serializers as $type => $serializer) { NotificationSerializer::setSubjectSerializer($type, $serializer); } } /** * Populate the API routes. * * @param RouteCollection $routes */ protected function populateRoutes(RouteCollection $routes) { $factory = $this->app->make(RouteHandlerFactory::class); $callback = include __DIR__.'/routes.php'; $callback($routes, $factory); } }