blessing-skin-server/app/Http/View/Composers/FootComposer.php

99 lines
2.8 KiB
PHP
Raw Normal View History

2019-09-17 23:10:44 +08:00
<?php
namespace App\Http\View\Composers;
2019-12-14 11:10:37 +08:00
use App\Services\Translations\JavaScript;
2019-09-17 23:10:44 +08:00
use App\Services\Webpack;
2019-12-14 11:10:37 +08:00
use Illuminate\Contracts\Events\Dispatcher;
2019-09-17 23:10:44 +08:00
use Illuminate\Http\Request;
2019-09-20 18:50:08 +08:00
use Illuminate\Support\Str;
2019-12-14 11:10:37 +08:00
use Illuminate\View\View;
2019-09-17 23:10:44 +08:00
class FootComposer
{
/** @var Request */
protected $request;
/** @var Webpack */
protected $webpack;
/** @var JavaScript */
protected $javascript;
/** @var Dispatcher */
protected $dispatcher;
public function __construct(
Request $request,
Webpack $webpack,
JavaScript $javascript,
Dispatcher $dispatcher
) {
$this->request = $request;
$this->webpack = $webpack;
$this->javascript = $javascript;
$this->dispatcher = $dispatcher;
}
public function compose(View $view)
{
$this->injectJavaScript($view);
$this->addExtra($view);
}
public function injectJavaScript(View $view)
{
$scripts = [];
$locale = app()->getLocale();
2020-05-18 16:40:31 +08:00
$scripts[] = [
'src' => $this->javascript->generate($locale),
'defer' => true,
];
2019-09-17 23:10:44 +08:00
if ($pluginI18n = $this->javascript->plugin($locale)) {
2020-05-18 16:40:31 +08:00
$scripts[] = [
'src' => $pluginI18n,
'defer' => true,
];
2019-09-17 23:10:44 +08:00
}
2019-09-20 18:50:08 +08:00
if (Str::startsWith(config('app.asset.env'), 'dev')) {
2020-05-18 16:40:31 +08:00
$scripts[] = [
'src' => $this->webpack->url('style.js'),
'async' => true,
'defer' => true,
];
2020-03-30 17:21:02 +08:00
} else {
2020-05-18 16:40:31 +08:00
$scripts[] = [
2020-05-19 17:10:55 +08:00
'src' => 'https://cdn.jsdelivr.net/npm/react@16.13.1/umd/react.production.min.js',
'integrity' => 'sha256-yUhvEmYVhZ/GGshIQKArLvySDSh6cdmdcIx0spR3UP4=',
'crossorigin' => 'anonymous',
];
$scripts[] = [
'src' => 'https://cdn.jsdelivr.net/npm/react-dom@16.13.1/umd/react-dom.production.min.js',
'integrity' => 'sha256-vFt3l+illeNlwThbDUdoPTqF81M8WNSZZZt3HEjsbSU=',
2020-05-18 16:40:31 +08:00
'crossorigin' => 'anonymous',
];
2019-09-20 18:50:08 +08:00
}
2020-05-18 16:40:31 +08:00
$scripts[] = [
'src' => 'https://cdn.jsdelivr.net/npm/@blessing-skin/admin-lte@3.0/dist/admin-lte.min.js',
'integrity' => 'sha256-lEpMZPtZ0RgHYZFOcJeX94WMegvHJ+beF5V7XtCcWxY=',
'crossorigin' => 'anonymous',
];
$scripts[] = [
'src' => $this->webpack->url('app.js'),
];
2019-09-17 23:10:44 +08:00
$view->with([
'scripts' => $scripts,
'inline_js' => option('custom_js'),
]);
}
public function addExtra(View $view)
{
$content = [];
$this->dispatcher->dispatch(new \App\Events\RenderingFooter($content));
$view->with('extra_foot', $content);
}
}