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