119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\CMS\Contracts\Repositories\Template as TemplateContract;
|
|
use App\CMS\Handlers\Registrators\TemplateRegistrator;
|
|
use App\CMS\Helpers\CurrentTemplate;
|
|
use App\CMS\Repositories\Module;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class TemplateServiceProvider extends ServiceProvider
|
|
{
|
|
public function boot()
|
|
{
|
|
try {
|
|
$template = CurrentTemplate::get();
|
|
} catch (QueryException $e) {
|
|
return;
|
|
}
|
|
|
|
// ignore running from artisan
|
|
if (App::runningInConsole() || App::environment('testing') || !$template) {
|
|
$this->publishViewResources();
|
|
|
|
return;
|
|
}
|
|
|
|
// include files
|
|
$files = isset($template->moduleSettings->files) ? $template->moduleSettings->files : [];
|
|
|
|
foreach ($files as $file) {
|
|
include_once $file;
|
|
}
|
|
|
|
// register templates
|
|
$publicTemplates = CurrentTemplate::getPublicTemplatePaths();
|
|
|
|
if (!is_array($publicTemplates)) {
|
|
throw new \ErrorException(CurrentTemplate::handler() . '::getPublicTemplatePaths() must return array!');
|
|
}
|
|
|
|
$this->registerTemplatePaths($publicTemplates);
|
|
}
|
|
|
|
/**
|
|
* Register template paths from public templates
|
|
*
|
|
* @param $publicTemplates
|
|
*/
|
|
public function registerTemplatePaths($publicTemplates)
|
|
{
|
|
$modules = app(Module::class)
|
|
->all()
|
|
->pluck('id', 'alias')
|
|
->toArray();
|
|
|
|
/**
|
|
* @var $registrator TemplateRegistrator
|
|
*/
|
|
$registrator = $this->app->make('registerTemplateDir');
|
|
|
|
foreach ($publicTemplates as $template) {
|
|
// setup some global variables
|
|
$module = strtolower($template['module']);
|
|
$alias = isset($template['alias']) ? $template['alias'] : $module;
|
|
$namespace = isset($template['namespace']) ? $template['namespace'] : strtolower($template['module']);
|
|
|
|
if (!isset($modules[$alias])) {
|
|
continue;
|
|
}
|
|
|
|
// register path with current template name
|
|
$registrator->setNamespace($namespace);
|
|
$registrator->setModuleId($modules[$alias]);
|
|
$registrator->register('resources/views/modules/' . $alias, '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 || is_null($template->module_settings)) {
|
|
return;
|
|
}
|
|
|
|
$templateDir = base_path('templates/' . $template->static_name . '/Resources');
|
|
$assetsDir = $templateDir . '/' . TemplateContract::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');
|
|
}
|
|
}
|
|
} |