118 lines
3.5 KiB
PHP
118 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Handlers\Registrators\TemplateRegistrator;
|
|
use App\Models\Module;
|
|
use App\Models\Template;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use InvalidArgumentException;
|
|
|
|
class TemplateServiceProvider extends ServiceProvider
|
|
{
|
|
private function getTemplate()
|
|
{
|
|
// select template
|
|
$template = Module::where('is_template', true)
|
|
->where('enabled', true)
|
|
->first();
|
|
|
|
if ($template) {
|
|
$template->settings = json_decode($template->module_settings);
|
|
}
|
|
|
|
return $template;
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
// ignore running from artisan
|
|
if (App::runningInConsole()) {
|
|
$this->publishViewResources();
|
|
|
|
return;
|
|
}
|
|
|
|
$template = $this->getTemplate();
|
|
|
|
if (!$template) {
|
|
throw new \ErrorException('Template is not set, cannot continue');
|
|
}
|
|
|
|
// include files
|
|
$files = isset($template->settings->files) ? $template->settings->files : [];
|
|
|
|
foreach ($files as $file) {
|
|
include_once $file;
|
|
}
|
|
|
|
// register templates
|
|
$publicTemplates = call_user_func([$template->handler, 'getPublicTemplatePaths']);
|
|
|
|
if (!is_array($publicTemplates)) {
|
|
throw new \ErrorException($template->handler . '::getPublicTemplatePaths() must return array!');
|
|
}
|
|
|
|
if (empty($publicTemplates)) {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @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']);
|
|
try {
|
|
$registrator->setBaseDirectory('resources/views/modules/' . $module);
|
|
$registrator->setNamespace($namespace);
|
|
$registrator->setModuleId(call_user_func([config($module . '.handler'), 'getModuleId']));
|
|
|
|
// register path with current template name
|
|
$registrator->register('visitor', $template['templateName']);
|
|
} catch (InvalidArgumentException $e) {
|
|
// ignore this exception
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register the service provider.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Publish all public assets from all themes
|
|
*/
|
|
private function publishViewResources()
|
|
{
|
|
$template = $this->getTemplate();
|
|
if (!$template) {
|
|
return;
|
|
}
|
|
|
|
$templateDir = app_path('../templates/' . $template->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');
|
|
}
|
|
}
|
|
} |