]> BookStack Code Mirror - bookstack/blob - app/App/Providers/AppServiceProvider.php
Notifications: Switched testing from string to reference levels
[bookstack] / app / App / Providers / AppServiceProvider.php
1 <?php
2
3 namespace BookStack\App\Providers;
4
5 use BookStack\Access\SocialAuthService;
6 use BookStack\Activity\Tools\ActivityLogger;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Models\Bookshelf;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Models\Page;
11 use BookStack\Exceptions\BookStackExceptionHandlerPage;
12 use BookStack\Permissions\PermissionApplicator;
13 use BookStack\Settings\SettingService;
14 use BookStack\Util\CspService;
15 use GuzzleHttp\Client;
16 use Illuminate\Contracts\Foundation\ExceptionRenderer;
17 use Illuminate\Database\Eloquent\Relations\Relation;
18 use Illuminate\Support\Facades\Schema;
19 use Illuminate\Support\Facades\URL;
20 use Illuminate\Support\ServiceProvider;
21 use Psr\Http\Client\ClientInterface as HttpClientInterface;
22
23 class AppServiceProvider extends ServiceProvider
24 {
25     /**
26      * Custom container bindings to register.
27      * @var string[]
28      */
29     public $bindings = [
30         ExceptionRenderer::class => BookStackExceptionHandlerPage::class,
31     ];
32
33     /**
34      * Custom singleton bindings to register.
35      * @var string[]
36      */
37     public $singletons = [
38         'activity' => ActivityLogger::class,
39         SettingService::class => SettingService::class,
40         SocialAuthService::class => SocialAuthService::class,
41         CspService::class => CspService::class,
42     ];
43
44     /**
45      * Bootstrap any application services.
46      *
47      * @return void
48      */
49     public function boot()
50     {
51         // Set root URL
52         $appUrl = config('app.url');
53         if ($appUrl) {
54             $isHttps = (strpos($appUrl, 'https://') === 0);
55             URL::forceRootUrl($appUrl);
56             URL::forceScheme($isHttps ? 'https' : 'http');
57         }
58
59         // Allow longer string lengths after upgrade to utf8mb4
60         Schema::defaultStringLength(191);
61
62         // Set morph-map for our relations to friendlier aliases
63         Relation::enforceMorphMap([
64             'bookshelf' => Bookshelf::class,
65             'book'      => Book::class,
66             'chapter'   => Chapter::class,
67             'page'      => Page::class,
68         ]);
69     }
70
71     /**
72      * Register any application services.
73      *
74      * @return void
75      */
76     public function register()
77     {
78         $this->app->bind(HttpClientInterface::class, function ($app) {
79             return new Client([
80                 'timeout' => 3,
81             ]);
82         });
83
84         $this->app->singleton(PermissionApplicator::class, function ($app) {
85             return new PermissionApplicator(null);
86         });
87     }
88 }