2016-08-28 10:05:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2018-07-17 09:29:10 +08:00
|
|
|
use Blade;
|
2016-12-10 22:01:05 +08:00
|
|
|
use Event;
|
|
|
|
use App\Events;
|
2018-07-17 09:29:10 +08:00
|
|
|
use App\Models\User;
|
2018-07-22 16:23:49 +08:00
|
|
|
use ReflectionException;
|
2019-02-27 23:44:50 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2019-03-21 22:51:30 +08:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2018-07-22 16:23:49 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2019-03-02 22:58:37 +08:00
|
|
|
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()
|
|
|
|
{
|
2018-07-23 08:56:24 +08:00
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
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']), [
|
2017-01-14 23:33:01 +08:00
|
|
|
'base_url' => url('/'),
|
2018-07-25 15:32:08 +08:00
|
|
|
'site_name' => option_localized('site_name'),
|
|
|
|
'route' => request()->path(),
|
2019-03-21 19:45:52 +08:00
|
|
|
'extra' => [],
|
2016-12-10 22:01:05 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$event->addContent('<script>var blessing = '.json_encode($blessing).';</script>');
|
|
|
|
});
|
2018-07-22 16:23:49 +08:00
|
|
|
|
|
|
|
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 {
|
2019-03-20 11:05:41 +08:00
|
|
|
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()
|
|
|
|
{
|
2018-07-22 16:23:49 +08:00
|
|
|
$this->app->singleton('cipher', 'App\Services\Cipher\\'.config('secure.cipher'));
|
2019-03-02 22:58:37 +08:00
|
|
|
$this->app->singleton('options', OptionRepository::class);
|
2018-09-08 20:09:56 +08:00
|
|
|
$this->app->singleton('parsedown', \Parsedown::class);
|
2018-07-23 08:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
2018-07-23 08:56:24 +08:00
|
|
|
}
|
2016-08-28 10:05:21 +08:00
|
|
|
}
|
|
|
|
}
|