100 lines
3.2 KiB
PHP
100 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\CMS\Handlers\Registrators\TemplateRegistrator;
|
|
use App\CMS\Helpers\CurrentTemplate;
|
|
use App\Models\Templates\Template;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class TemplateServiceProvider extends ServiceProvider
|
|
{
|
|
public function boot()
|
|
{
|
|
// ignore running from artisan
|
|
if (App::runningInConsole()) {
|
|
$this->publishViewResources();
|
|
|
|
return;
|
|
}
|
|
|
|
$template = CurrentTemplate::get();
|
|
|
|
if (!$template) {
|
|
throw new \ErrorException('Template is not set, cannot continue');
|
|
}
|
|
|
|
// include files
|
|
$files = isset($template->module_settings->files) ? $template->module_settings->files : [];
|
|
|
|
foreach ($files as $file) {
|
|
include_once $file;
|
|
}
|
|
|
|
// register templates
|
|
$publicTemplates = call_user_func([CurrentTemplate::handler(), 'getPublicTemplatePaths']);
|
|
|
|
if (!is_array($publicTemplates)) {
|
|
throw new \ErrorException(CurrentTemplate::handler() . '::getPublicTemplatePaths() must return array!');
|
|
}
|
|
|
|
/**
|
|
* @var $registrator TemplateRegistrator
|
|
*/
|
|
$registrator = $this->app->make('registerTemplateDir');
|
|
foreach ($publicTemplates as $template) {
|
|
// setup some global variables
|
|
$module = strtolower($template['module']);
|
|
$namespace = isset($template['namespace']) ? $template['namespace'] : strtolower($template['module']);
|
|
|
|
// register path with current template name
|
|
|
|
$registrator->setNamespace($namespace);
|
|
if (is_callable([config($module . '.handler'), 'getModuleId'])) {
|
|
$registrator->setModuleId(call_user_func([config($module . '.handler'), 'getModuleId']));
|
|
}
|
|
$registrator->register('resources/views/modules/' . $module, 'visitor', $template['templateName']);
|
|
}
|
|
|
|
// register also global resources/views/visitor directory
|
|
$registrator->register('resources/views/', 'visitor', 'admin/templates.core');
|
|
}
|
|
|
|
/**
|
|
* Register the service provider.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Publish all public assets from all themes
|
|
*/
|
|
private function publishViewResources()
|
|
{
|
|
$template = CurrentTemplate::get();
|
|
if (!$template) {
|
|
return;
|
|
}
|
|
|
|
$templateDir = base_path('templates/' . $template->module_settings->staticName . '/Resources');
|
|
$assetsDir = $templateDir . '/' . Template::ASSETS_DIRECTORY;
|
|
$viewsDir = $templateDir . '/views';
|
|
|
|
// publish assets if exists for current theme
|
|
if (is_dir($assetsDir)) {
|
|
$this->publishes([$assetsDir => public_path('assets/visitor')], 'public-assets');
|
|
}
|
|
|
|
// publish base visitors views
|
|
$this->publishes([$viewsDir . '/visitor' => resource_path('views/visitor')], 'module-views');
|
|
|
|
// publish admin views
|
|
if (is_dir($viewsDir . '/modules')) {
|
|
$this->publishes([$viewsDir . '/modules' => resource_path('views/modules')], 'module-views');
|
|
}
|
|
}
|
|
} |