184 lines
6.0 KiB
PHP
184 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Handlers\Registrators\MenuExtensionRegistrator;
|
|
use App\Handlers\Registrators\SearchEngineRegistrator;
|
|
use App\Handlers\Registrators\SettingsExtensionRegistrator;
|
|
use App\Handlers\Registrators\TemplateRegistrator;
|
|
use App\Handlers\Registrators\WidgetRegistrator;
|
|
use App\Handlers\Search\Content;
|
|
use App\Handlers\Search\Files;
|
|
use App\Handlers\Search\Presenters\AdminContent as AdminContentPresenter;
|
|
use App\Handlers\Search\Presenters\AdminFiles;
|
|
use App\Models\Template;
|
|
use App\Widgets\HtmlCode\HtmlCode;
|
|
use App\Widgets\Menu\Menu;
|
|
use App\Widgets\OrbitSlider\OrbitSlider;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap any application services.
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
// ignore running from artisan
|
|
if (App::runningInConsole()) {
|
|
$this->publishViewResources();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->registerWidgets();
|
|
$this->registerTemplates();
|
|
$this->registerSettings();
|
|
$this->registerSearch();
|
|
|
|
// check if given template is valid
|
|
$theme = config('app.theme');
|
|
$themeDir = resource_path('views/visitor/' . $theme);
|
|
if (!$theme || empty($theme) || !is_dir($themeDir)) {
|
|
throw new InvalidArgumentException('No or invalid theme set! Use artisan theme:set or edit .env file manually and include THEME option, please');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register any application services.
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
if ($this->app->environment() == 'local') {
|
|
try {
|
|
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
|
|
} catch (\Exception $e) {
|
|
// this service provider may not exist when switching from other environment than local
|
|
// until composer install is run
|
|
// just ignore this exception and when no service provider is available, do not use debugbar
|
|
}
|
|
}
|
|
|
|
// register PingPong module extension
|
|
$this->app->register('Pingpong\Modules\ModulesServiceProvider');
|
|
$this->app->register('Pingpong\Menus\MenusServiceProvider');
|
|
|
|
// create singletons for registering widgets
|
|
$this->app->singleton('registerWidget', function ($app) {
|
|
return new WidgetRegistrator($app);
|
|
});
|
|
|
|
$this->app->singleton('registerMenuExtension', function ($app) {
|
|
return new MenuExtensionRegistrator($app);
|
|
});
|
|
|
|
// create registrator for template directories
|
|
$this->app->singleton('registerTemplateDir', function ($app) {
|
|
return new TemplateRegistrator($app);
|
|
});
|
|
|
|
$this->app->singleton('registerSettingsExtension', function ($app) {
|
|
return new SettingsExtensionRegistrator($app);
|
|
});
|
|
|
|
$this->app->singleton('registerSearchEngine', function ($app) {
|
|
return new SearchEngineRegistrator($app);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Method for registering widgets.
|
|
*/
|
|
private function registerWidgets()
|
|
{
|
|
/**
|
|
* @var $registrator WidgetRegistrator
|
|
*/
|
|
$registrator = $this->app->make('registerWidget');
|
|
|
|
// register all available widgets
|
|
$registrator->register(HtmlCode::class);
|
|
$registrator->register(Menu::class);
|
|
$registrator->register(OrbitSlider::class);
|
|
|
|
// register some menu extensions
|
|
// $menuExtensions = $this->app->make('registerMenuExtension');
|
|
// $menuExtensions->register(new URL());
|
|
}
|
|
|
|
/**
|
|
* Register path with editable contents
|
|
*/
|
|
private function registerTemplates()
|
|
{
|
|
/**
|
|
* @var $registrator TemplateRegistrator
|
|
*/
|
|
$registrator = $this->app->make('registerTemplateDir');
|
|
$registrator->setBaseDirectory('resources/views/visitor');
|
|
|
|
$registrator->register(config('app.theme'), 'admin/templates.core');
|
|
}
|
|
|
|
/**
|
|
* Register setting sections
|
|
*/
|
|
private function registerSettings()
|
|
{
|
|
$registrator = $this->app->make('registerSettingsExtension');
|
|
|
|
$registrator->register('basic', 'admin/settings.basic', 'admin.settings.parts.basic');
|
|
$registrator->register('meta', 'admin/settings.meta', 'admin.settings.parts.meta');
|
|
}
|
|
|
|
/**
|
|
* Register callbacks used for searching
|
|
*/
|
|
private function registerSearch()
|
|
{
|
|
$registrator = $this->app->make('registerSearchEngine');
|
|
|
|
$registrator->register(new Content(), new AdminContentPresenter(), SearchEngineRegistrator::ADMIN);
|
|
$registrator->register(new Files(), new AdminFiles(), SearchEngineRegistrator::ADMIN);
|
|
}
|
|
|
|
/**
|
|
* Publish all public assets from all themes
|
|
*/
|
|
private function publishViewResources()
|
|
{
|
|
$theme = config('app.theme');
|
|
$themeDir = resource_path('views/visitor/' . $theme);
|
|
|
|
// publish assets if exists for current theme
|
|
if (is_dir($themeDir . '/' . Template::ASSETS_DIRECTORY)) {
|
|
$this->publishes([$themeDir . '/' . Template::ASSETS_DIRECTORY => public_path('assets/' . $theme)], 'public-assets');
|
|
}
|
|
|
|
if (is_dir($themeDir . '/' . Template::MODULES_DIRECTORY)) {
|
|
$this->publishModuleViews($themeDir . '/' . Template::MODULES_DIRECTORY);
|
|
}
|
|
}
|
|
|
|
private function publishModuleViews($themeModules)
|
|
{
|
|
$theme = config('app.theme');
|
|
$modules = File::directories($themeModules);
|
|
|
|
$moduleViews = [];
|
|
foreach ($modules as $module) {
|
|
$moduleName = strtolower(basename($module));
|
|
$moduleViews[$module] = resource_path('views/modules/' . $moduleName . '/visitor/' . $theme);
|
|
}
|
|
|
|
if (!empty($moduleViews)) {
|
|
$this->publishes($moduleViews, 'module-views');
|
|
}
|
|
}
|
|
}
|