]> BookStack Code Mirror - bookstack/blob - app/Providers/AppServiceProvider.php
fa10d4874ca25178211a82888633fc8e4a781268
[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 Validator;
16
17 class AppServiceProvider extends ServiceProvider
18 {
19     /**
20      * Bootstrap any application services.
21      *
22      * @return void
23      */
24     public function boot()
25     {
26         // Custom validation methods
27         Validator::extend('image_extension', function ($attribute, $value, $parameters, $validator) {
28             $validImageExtensions = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'tiff', 'webp'];
29             return in_array(strtolower($value->getClientOriginalExtension()), $validImageExtensions);
30         });
31
32         Validator::extend('no_double_extension', function ($attribute, $value, $parameters, $validator) {
33             $uploadName = $value->getClientOriginalName();
34             return substr_count($uploadName, '.') < 2;
35         });
36
37         // Custom blade view directives
38         Blade::directive('icon', function ($expression) {
39             return "<?php echo icon($expression); ?>";
40         });
41
42         // Allow longer string lengths after upgrade to utf8mb4
43         Schema::defaultStringLength(191);
44
45         // Set morph-map due to namespace changes
46         Relation::morphMap([
47             'BookStack\\Bookshelf' => Bookshelf::class,
48             'BookStack\\Book' => Book::class,
49             'BookStack\\Chapter' => Chapter::class,
50             'BookStack\\Page' => Page::class,
51         ]);
52
53         // View Composers
54         View::composer('partials.breadcrumbs', BreadcrumbsViewComposer::class);
55     }
56
57     /**
58      * Register any application services.
59      *
60      * @return void
61      */
62     public function register()
63     {
64         $this->app->singleton(SettingService::class, function ($app) {
65             return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
66         });
67     }
68 }