FEATURE: move widget and template dir installation into middleware, optimize middleware groups

This commit is contained in:
Michal Kváček
2016-09-25 12:42:26 +02:00
parent d7b60690b2
commit 8f4e6268a4
12 changed files with 184 additions and 123 deletions
+1 -19
View File
@@ -16,6 +16,7 @@ use App\Widgets\HtmlCode\HtmlCode;
use App\Widgets\Menu\Menu;
use App\Widgets\OrbitSlider\OrbitSlider;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Process\Exception\InvalidArgumentException;
@@ -34,7 +35,6 @@ class AppServiceProvider extends ServiceProvider
}
$this->registerWidgets();
$this->registerTemplates();
$this->registerSettings();
$this->registerSearch();
}
@@ -97,24 +97,6 @@ class AppServiceProvider extends ServiceProvider
$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');
}
/**
+12 -39
View File
@@ -3,32 +3,13 @@
namespace App\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use App\Models\Module;
use App\Helpers\CurrentTemplate;
use App\Models\Template;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
class TemplateServiceProvider extends ServiceProvider
{
private function getTemplate()
{
// select template
try {
$template = Module::where('is_template', true)
->where('enabled', true)
->first();
if ($template) {
$template->settings = json_decode($template->module_settings);
}
} catch (\Exception $e) {
return null;
}
return $template;
}
public function boot()
{
// ignore running from artisan
@@ -38,28 +19,24 @@ class TemplateServiceProvider extends ServiceProvider
return;
}
$template = $this->getTemplate();
$template = CurrentTemplate::get();
if (!$template) {
throw new \ErrorException('Template is not set, cannot continue');
}
// include files
$files = isset($template->settings->files) ? $template->settings->files : [];
$files = isset($template->module_settings->files) ? $template->module_settings->files : [];
foreach ($files as $file) {
include_once $file;
}
// register templates
$publicTemplates = call_user_func([$template->handler, 'getPublicTemplatePaths']);
$publicTemplates = call_user_func([CurrentTemplate::handler(), 'getPublicTemplatePaths']);
if (!is_array($publicTemplates)) {
throw new \ErrorException($template->handler . '::getPublicTemplatePaths() must return array!');
}
if (empty($publicTemplates)) {
return;
throw new \ErrorException(CurrentTemplate::handler() . '::getPublicTemplatePaths() must return array!');
}
/**
@@ -70,16 +47,12 @@ class TemplateServiceProvider extends ServiceProvider
// 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 path with current template name
$registrator->setNamespace($namespace);
$registrator->setModuleId(call_user_func([config($module . '.handler'), 'getModuleId']));
$registrator->register('resources/views/modules/' . $module, 'visitor', $template['templateName']);
}
}
@@ -97,12 +70,12 @@ class TemplateServiceProvider extends ServiceProvider
*/
private function publishViewResources()
{
$template = $this->getTemplate();
$template = CurrentTemplate::get();
if (!$template) {
return;
}
$templateDir = app_path('../templates/' . $template->settings->staticName . '/Resources');
$templateDir = base_path('templates/' . $template->module_settings->staticName . '/Resources');
$assetsDir = $templateDir . '/' . Template::ASSETS_DIRECTORY;
$viewsDir = $templateDir . '/views';