]> BookStack Code Mirror - bookstack/blob - app/Providers/AppServiceProvider.php
34a3a290f0c0cf200537217abf0f0d677c38a5d3
[bookstack] / app / Providers / AppServiceProvider.php
1 <?php
2
3 namespace BookStack\Providers;
4
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Entities\BreadcrumbsViewComposer;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Bookshelf;
10 use BookStack\Entities\Models\Chapter;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Exceptions\WhoopsBookStackPrettyHandler;
13 use BookStack\Settings\Setting;
14 use BookStack\Settings\SettingService;
15 use BookStack\Util\CspService;
16 use GuzzleHttp\Client;
17 use Illuminate\Contracts\Cache\Repository;
18 use Illuminate\Database\Eloquent\Relations\Relation;
19 use Illuminate\Support\Facades\Blade;
20 use Illuminate\Support\Facades\Schema;
21 use Illuminate\Support\Facades\URL;
22 use Illuminate\Support\Facades\View;
23 use Illuminate\Support\ServiceProvider;
24 use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
25 use Psr\Http\Client\ClientInterface as HttpClientInterface;
26 use Whoops\Handler\HandlerInterface;
27
28 class AppServiceProvider extends ServiceProvider
29 {
30     /**
31      * Bootstrap any application services.
32      *
33      * @return void
34      */
35     public function boot()
36     {
37         // Set root URL
38         $appUrl = config('app.url');
39         if ($appUrl) {
40             $isHttps = (strpos($appUrl, 'https://') === 0);
41             URL::forceRootUrl($appUrl);
42             URL::forceScheme($isHttps ? 'https' : 'http');
43         }
44
45         // Custom blade view directives
46         Blade::directive('icon', function ($expression) {
47             return "<?php echo icon($expression); ?>";
48         });
49
50         // Allow longer string lengths after upgrade to utf8mb4
51         Schema::defaultStringLength(191);
52
53         // Set morph-map due to namespace changes
54         Relation::morphMap([
55             'BookStack\\Bookshelf' => Bookshelf::class,
56             'BookStack\\Book'      => Book::class,
57             'BookStack\\Chapter'   => Chapter::class,
58             'BookStack\\Page'      => Page::class,
59         ]);
60
61         // View Composers
62         View::composer('entities.breadcrumbs', BreadcrumbsViewComposer::class);
63     }
64
65     /**
66      * Register any application services.
67      *
68      * @return void
69      */
70     public function register()
71     {
72         $this->app->bind(HandlerInterface::class, function ($app) {
73             return $app->make(WhoopsBookStackPrettyHandler::class);
74         });
75
76         $this->app->singleton(SettingService::class, function ($app) {
77             return new SettingService($app->make(Setting::class), $app->make(Repository::class));
78         });
79
80         $this->app->singleton(SocialAuthService::class, function ($app) {
81             return new SocialAuthService($app->make(SocialiteFactory::class), $app->make(LoginService::class));
82         });
83
84         $this->app->singleton(CspService::class, function ($app) {
85             return new CspService();
86         });
87
88         $this->app->bind(HttpClientInterface::class, function ($app) {
89             return new Client([
90                 'timeout' => 3,
91             ]);
92         });
93     }
94 }