blessing-skin-server/app/Providers/AppServiceProvider.php

96 lines
2.7 KiB
PHP
Raw Normal View History

2016-08-28 10:05:21 +08:00
<?php
namespace App\Providers;
use Blade;
use Event;
2019-03-20 11:00:14 +08:00
use Redis;
use App\Events;
use App\Models\User;
use ReflectionException;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
use App\Exceptions\PrettyPageException;
use Illuminate\Support\ServiceProvider;
2018-07-22 16:37:39 +08:00
use App\Services\Repositories\OptionRepository;
2016-08-28 10:05:21 +08:00
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Control the URL generated by url() function
$this->configureUrlGenerator();
2018-07-22 16:37:39 +08:00
2019-02-27 23:44:50 +08:00
Blade::if('admin', function (User $user) {
return $user->isAdmin();
});
Event::listen(Events\RenderingHeader::class, function ($event) {
2018-02-16 17:31:04 +08:00
// Provide some application information for javascript
2019-02-27 23:44:50 +08:00
$blessing = array_merge(Arr::except(config('app'), ['key', 'providers', 'aliases', 'cipher', 'log', 'url']), [
'base_url' => url('/'),
2018-07-25 15:32:08 +08:00
'site_name' => option_localized('site_name'),
'route' => request()->path(),
]);
$event->addContent('<script>var blessing = '.json_encode($blessing).';</script>');
});
try {
$this->app->make('cipher');
} catch (ReflectionException $e) {
throw new PrettyPageException(trans('errors.cipher.unsupported', ['cipher' => config('secure.cipher')]));
}
2019-03-20 11:00:14 +08:00
try {
if (option('enable_redis') && Redis::ping()) {
2019-03-20 11:00:14 +08:00
config(['cache.default' => 'redis']);
config(['session.driver' => 'redis']);
}
} catch (\Exception $e) {
//
}
2016-08-28 10:05:21 +08:00
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('cipher', 'App\Services\Cipher\\'.config('secure.cipher'));
2016-10-23 11:41:52 +08:00
$this->app->singleton('users', \App\Services\Repositories\UserRepository::class);
$this->app->singleton('options', OptionRepository::class);
$this->app->singleton('parsedown', \Parsedown::class);
}
/**
* Configure the \Illuminate\Routing\UrlGenerator.
*
* @return void
*
* @codeCoverageIgnore
*/
protected function configureUrlGenerator()
{
if (! option('auto_detect_asset_url')) {
$rootUrl = option('site_url');
// Replace HTTP_HOST with site_url set in options,
// to prevent CDN source problems.
if ($this->app['url']->isValidUrl($rootUrl)) {
$this->app['url']->forceRootUrl($rootUrl);
}
}
2018-08-17 22:54:26 +08:00
if (option('force_ssl') || is_request_secure()) {
2018-08-21 10:40:37 +08:00
$this->app['url']->forceScheme('https');
}
2016-08-28 10:05:21 +08:00
}
}