]> BookStack Code Mirror - bookstack/blob - app/Providers/AppServiceProvider.php
Cleaned testing service provider usage
[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\Pagination\Paginator;
20 use Illuminate\Support\Facades\Blade;
21 use Illuminate\Support\Facades\Schema;
22 use Illuminate\Support\Facades\URL;
23 use Illuminate\Support\Facades\View;
24 use Illuminate\Support\ServiceProvider;
25 use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
26 use Psr\Http\Client\ClientInterface as HttpClientInterface;
27 use Whoops\Handler\HandlerInterface;
28
29 class AppServiceProvider extends ServiceProvider
30 {
31     /**
32      * Bootstrap any application services.
33      *
34      * @return void
35      */
36     public function boot()
37     {
38         // Set root URL
39         $appUrl = config('app.url');
40         if ($appUrl) {
41             $isHttps = (strpos($appUrl, 'https://') === 0);
42             URL::forceRootUrl($appUrl);
43             URL::forceScheme($isHttps ? 'https' : 'http');
44         }
45
46         // Custom blade view directives
47         Blade::directive('icon', function ($expression) {
48             return "<?php echo icon($expression); ?>";
49         });
50
51         // Allow longer string lengths after upgrade to utf8mb4
52         Schema::defaultStringLength(191);
53
54         // Set morph-map for our relations to friendlier aliases
55         Relation::enforceMorphMap([
56             'bookshelf' => Bookshelf::class,
57             'book'      => Book::class,
58             'chapter'   => Chapter::class,
59             'page'      => Page::class,
60         ]);
61
62         // View Composers
63         View::composer('entities.breadcrumbs', BreadcrumbsViewComposer::class);
64
65         // Set paginator to use bootstrap-style pagination
66         Paginator::useBootstrap();
67     }
68
69     /**
70      * Register any application services.
71      *
72      * @return void
73      */
74     public function register()
75     {
76         $this->app->bind(HandlerInterface::class, function ($app) {
77             return $app->make(WhoopsBookStackPrettyHandler::class);
78         });
79
80         $this->app->singleton(SettingService::class, function ($app) {
81             return new SettingService($app->make(Setting::class), $app->make(Repository::class));
82         });
83
84         $this->app->singleton(SocialAuthService::class, function ($app) {
85             return new SocialAuthService($app->make(SocialiteFactory::class), $app->make(LoginService::class));
86         });
87
88         $this->app->singleton(CspService::class, function ($app) {
89             return new CspService();
90         });
91
92         $this->app->bind(HttpClientInterface::class, function ($app) {
93             return new Client([
94                 'timeout' => 3,
95             ]);
96         });
97     }
98 }