]> BookStack Code Mirror - bookstack/blob - app/Providers/AppServiceProvider.php
Update settings.php
[bookstack] / app / Providers / AppServiceProvider.php
1 <?php namespace BookStack\Providers;
2
3 use Blade;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\BreadcrumbsViewComposer;
7 use BookStack\Entities\Chapter;
8 use BookStack\Entities\Page;
9 use BookStack\Settings\Setting;
10 use BookStack\Settings\SettingService;
11 use Illuminate\Database\Eloquent\Relations\Relation;
12 use Illuminate\Support\Facades\View;
13 use Illuminate\Support\ServiceProvider;
14 use Schema;
15 use URL;
16 use Validator;
17
18 class AppServiceProvider extends ServiceProvider
19 {
20     /**
21      * Bootstrap any application services.
22      *
23      * @return void
24      */
25     public function boot()
26     {
27         // Set root URL
28         $appUrl = config('app.url');
29         if ($appUrl) {
30             $isHttps = (strpos($appUrl, 'https://') === 0);
31             URL::forceRootUrl($appUrl);
32             URL::forceScheme($isHttps ? 'https' : 'http');
33         }
34
35         // Custom validation methods
36         Validator::extend('image_extension', function ($attribute, $value, $parameters, $validator) {
37             $validImageExtensions = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'tiff', 'webp'];
38             return in_array(strtolower($value->getClientOriginalExtension()), $validImageExtensions);
39         });
40
41         Validator::extend('no_double_extension', function ($attribute, $value, $parameters, $validator) {
42             $uploadName = $value->getClientOriginalName();
43             return substr_count($uploadName, '.') < 2;
44         });
45
46         // Custom blade view directives
47         Blade::directive('icon', function ($expression) {
48             return "<?php echo icon($expression); ?>";
49         });
50
51         Blade::directive('exposeTranslations', function($expression) {
52             return "<?php \$__env->startPush('translations'); ?>" .
53                 "<?php foreach({$expression} as \$key): ?>" .
54                 '<meta name="translation" key="<?php echo e($key); ?>" value="<?php echo e(trans($key)); ?>">' . "\n" .
55                 "<?php endforeach; ?>" .
56                 '<?php $__env->stopPush(); ?>';
57         });
58
59         // Allow longer string lengths after upgrade to utf8mb4
60         Schema::defaultStringLength(191);
61
62         // Set morph-map due to namespace changes
63         Relation::morphMap([
64             'BookStack\\Bookshelf' => Bookshelf::class,
65             'BookStack\\Book' => Book::class,
66             'BookStack\\Chapter' => Chapter::class,
67             'BookStack\\Page' => Page::class,
68         ]);
69
70         // View Composers
71         View::composer('partials.breadcrumbs', BreadcrumbsViewComposer::class);
72     }
73
74     /**
75      * Register any application services.
76      *
77      * @return void
78      */
79     public function register()
80     {
81         $this->app->singleton(SettingService::class, function ($app) {
82             return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
83         });
84     }
85 }