]> BookStack Code Mirror - bookstack/blob - app/Providers/AppServiceProvider.php
Updated template test to be more stable
[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         URL::forceRootUrl(config('app.url'));
29
30         // Custom validation methods
31         Validator::extend('image_extension', function ($attribute, $value, $parameters, $validator) {
32             $validImageExtensions = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'tiff', 'webp'];
33             return in_array(strtolower($value->getClientOriginalExtension()), $validImageExtensions);
34         });
35
36         Validator::extend('no_double_extension', function ($attribute, $value, $parameters, $validator) {
37             $uploadName = $value->getClientOriginalName();
38             return substr_count($uploadName, '.') < 2;
39         });
40
41         // Custom blade view directives
42         Blade::directive('icon', function ($expression) {
43             return "<?php echo icon($expression); ?>";
44         });
45
46         Blade::directive('exposeTranslations', function($expression) {
47             return "<?php \$__env->startPush('translations'); ?>" .
48                 "<?php foreach({$expression} as \$key): ?>" .
49                 '<meta name="translation" key="<?php echo e($key); ?>" value="<?php echo e(trans($key)); ?>">' . "\n" .
50                 "<?php endforeach; ?>" .
51                 '<?php $__env->stopPush(); ?>';
52         });
53
54         // Allow longer string lengths after upgrade to utf8mb4
55         Schema::defaultStringLength(191);
56
57         // Set morph-map due to namespace changes
58         Relation::morphMap([
59             'BookStack\\Bookshelf' => Bookshelf::class,
60             'BookStack\\Book' => Book::class,
61             'BookStack\\Chapter' => Chapter::class,
62             'BookStack\\Page' => Page::class,
63         ]);
64
65         // View Composers
66         View::composer('partials.breadcrumbs', BreadcrumbsViewComposer::class);
67     }
68
69     /**
70      * Register any application services.
71      *
72      * @return void
73      */
74     public function register()
75     {
76         $this->app->singleton(SettingService::class, function ($app) {
77             return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
78         });
79     }
80 }