FEATURE: move widget and template dir installation into middleware, optimize middleware groups
This commit is contained in:
@@ -28,6 +28,10 @@ class TemplateRegistrator
|
||||
* @var int ID of module
|
||||
*/
|
||||
protected $moduleId;
|
||||
/**
|
||||
* @var array list of all registered template paths
|
||||
*/
|
||||
protected $paths = [];
|
||||
|
||||
/**
|
||||
* Set base path
|
||||
@@ -80,24 +84,39 @@ class TemplateRegistrator
|
||||
* Registers new template path.
|
||||
* This method registeres given path itself with all subdirectories
|
||||
*
|
||||
* @param $baseDirectory
|
||||
* @param string $path path relatively to root of the project
|
||||
* @param string $name human readable identification of given template path
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function register($path, $name)
|
||||
public function register($baseDirectory, $path, $name)
|
||||
{
|
||||
// check if base directory was set
|
||||
if (!$this->baseDirectory || !is_dir($this->baseDirectory)) {
|
||||
throw new \Exception('Set $baseDirectory before registering template path.');
|
||||
$this->paths[] = [
|
||||
'namespace' => $this->namespace,
|
||||
'moduleId' => $this->moduleId,
|
||||
'baseDirectory' => $baseDirectory,
|
||||
'path' => $this->baseDirectory . DIRECTORY_SEPARATOR . $path,
|
||||
'name' => $name
|
||||
];
|
||||
$this->unsetNamespace();
|
||||
}
|
||||
|
||||
public function saveTemplates()
|
||||
{
|
||||
if (!is_array($this->paths) || empty($this->paths)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->registeredPath = $this->baseDirectory . DIRECTORY_SEPARATOR . $path;
|
||||
foreach ($this->paths as $path) {
|
||||
$this->setNamespace($path['namespace']);
|
||||
$this->setModuleId($path['moduleId']);
|
||||
$this->setBaseDirectory($path['baseDirectory']);
|
||||
|
||||
// register path
|
||||
$this->registerPath($this->registeredPath, $name);
|
||||
|
||||
$this->unsetNamespace();
|
||||
$this->registeredPath = $this->baseDirectory . DIRECTORY_SEPARATOR . $path['path'];
|
||||
// register path
|
||||
$this->registerPath($this->registeredPath, $path['name']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,17 +3,11 @@ namespace App\Handlers\Registrators;
|
||||
|
||||
use App\Enumerations\WidgetTypesWithHandlers;
|
||||
use App\Models\WidgetType;
|
||||
use App\Widgets\Contracts\PublicWidget;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
class WidgetRegistrator
|
||||
{
|
||||
protected $widgets;
|
||||
protected $registeredWidgets = [];
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->widgets = WidgetTypesWithHandlers::getSimple();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used for registering new widgets
|
||||
@@ -24,27 +18,41 @@ class WidgetRegistrator
|
||||
*/
|
||||
public function register($classname, $templateId = null, $moduleId = null)
|
||||
{
|
||||
$class = new $classname;
|
||||
if (!$class instanceof PublicWidget) {
|
||||
throw new InvalidArgumentException($class . ' must implement PublicWidget interface.');
|
||||
}
|
||||
$this->registeredWidgets[] = [
|
||||
'class' => $classname,
|
||||
'templateId' => $templateId,
|
||||
'moduleId' => $moduleId
|
||||
];
|
||||
}
|
||||
|
||||
// register widget as new widget type
|
||||
if (!in_array($classname, $this->widgets)) {
|
||||
$widgetName = call_user_func($classname . '::getName');
|
||||
$widgetDescription = call_user_func($classname . '::getDescription');
|
||||
/**
|
||||
* Install all non-installed widgets
|
||||
*/
|
||||
public function installWidgets()
|
||||
{
|
||||
$installed = WidgetTypesWithHandlers::getSimple();
|
||||
foreach ($this->registeredWidgets as $registeredWidget) {
|
||||
$classname = $registeredWidget['class'];
|
||||
$templateId = $registeredWidget['templateId'];
|
||||
$moduleId = $registeredWidget['moduleId'];
|
||||
|
||||
// create row in widget types
|
||||
WidgetType::create([
|
||||
'handler' => $classname,
|
||||
'name' => $widgetName,
|
||||
'module_id' => $moduleId,
|
||||
'template_id' => $templateId,
|
||||
'description' => $widgetDescription
|
||||
]);
|
||||
// register widget as new widget type
|
||||
if (!in_array($classname, $installed)) {
|
||||
$widgetName = call_user_func($classname . '::getName');
|
||||
$widgetDescription = call_user_func($classname . '::getDescription');
|
||||
|
||||
// install widget (if neccessary)
|
||||
call_user_func($classname . '::install');
|
||||
// create row in widget types
|
||||
WidgetType::create([
|
||||
'handler' => $classname,
|
||||
'name' => $widgetName,
|
||||
'module_id' => $moduleId,
|
||||
'template_id' => $templateId,
|
||||
'description' => $widgetDescription
|
||||
]);
|
||||
|
||||
// install widget (if neccessary)
|
||||
call_user_func($classname . '::install');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace App\Helpers;
|
||||
|
||||
|
||||
use App\Models\Module;
|
||||
|
||||
class CurrentTemplate
|
||||
@@ -20,6 +19,12 @@ class CurrentTemplate
|
||||
self::$template = Module::where('is_template', true)
|
||||
->where('enabled', true)
|
||||
->first();
|
||||
|
||||
if (self::$template) {
|
||||
self::$template->module_settings = json_decode(self::$template->module_settings);
|
||||
}
|
||||
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
return self::$template;
|
||||
|
||||
@@ -38,6 +38,7 @@ class Settings
|
||||
|
||||
if (isset(self::$settings->$key)) {
|
||||
$exists = true;
|
||||
self::$initialized = true;
|
||||
return self::$settings->$key;
|
||||
}
|
||||
|
||||
|
||||
+18
-8
@@ -2,8 +2,13 @@
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use App\Http\Middleware\TemplatePathInstaller;
|
||||
use App\Http\Middleware\WidgetInstaller;
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||
use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter;
|
||||
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes;
|
||||
use Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect;
|
||||
@@ -18,7 +23,10 @@ class Kernel extends HttpKernel
|
||||
* @var array
|
||||
*/
|
||||
protected $middleware = [
|
||||
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
|
||||
Middleware\EncryptCookies::class,
|
||||
StartSession::class,
|
||||
ShareErrorsFromSession::class,
|
||||
Middleware\VerifyCsrfToken::class
|
||||
];
|
||||
/**
|
||||
* The application's route middleware groups.
|
||||
@@ -26,14 +34,16 @@ class Kernel extends HttpKernel
|
||||
* @var array
|
||||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
Middleware\VerifyCsrfToken::class,
|
||||
'web' => [
|
||||
CheckForMaintenanceMode::class,
|
||||
],
|
||||
'api' => [
|
||||
'admin' => [
|
||||
Middleware\Authenticate::class,
|
||||
Middleware\AdminMenuRegistrator::class,
|
||||
TemplatePathInstaller::class,
|
||||
WidgetInstaller::class,
|
||||
],
|
||||
'api' => [
|
||||
'throttle:60,1',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Handlers\Registrators\TemplateRegistrator;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class TemplatePathInstaller
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
/**
|
||||
* @var $registator TemplateRegistrator
|
||||
*/
|
||||
$registator = App::make('registerTemplateDir');
|
||||
|
||||
// install registered widgets
|
||||
$registator->saveTemplates();
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Handlers\Registrators\WidgetRegistrator;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class WidgetInstaller
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
/**
|
||||
* @var $registator WidgetRegistrator
|
||||
*/
|
||||
$registator = App::make('registerWidget');
|
||||
|
||||
// install registered widgets
|
||||
$registator->installWidgets();
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -19,7 +19,7 @@ Route::group([
|
||||
* This route group serves to handle request in admin
|
||||
*/
|
||||
Route::group([
|
||||
'middleware' => ['web', 'auth', 'adminMenu'],
|
||||
'middleware' => ['admin'],
|
||||
'namespace' => 'Admin',
|
||||
'prefix' => 'admin'
|
||||
], function () {
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
<?php namespace Modules\Staticpages\Providers;
|
||||
|
||||
use App\Enumerations\ModulesHandlerToId;
|
||||
use App\Enumerations\ModulesWithIds;
|
||||
use App\Handlers\Registrators\TemplateRegistrator;
|
||||
use App\Models\ActionLogType;
|
||||
use App\Models\Module;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
class StaticPagesServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -45,7 +39,7 @@ class StaticPagesServiceProvider extends ServiceProvider
|
||||
*/
|
||||
protected function registerConfig()
|
||||
{
|
||||
$config = __DIR__ . DIRECTORY_SEPARATOR .'../Config/config.php';
|
||||
$config = __DIR__ . DIRECTORY_SEPARATOR . '../Config/config.php';
|
||||
$this->publishes([$config => config_path('staticpages.php')]);
|
||||
$this->mergeConfigFrom($config, 'staticpages');
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ abstract class OnePage implements TemplateHandler
|
||||
* Name of settings key for list of pages used on homepage
|
||||
*/
|
||||
const HOMEPAGE_PAGES_SETTINGS_KEY = 'template.onepage.homepagePages';
|
||||
protected static $homepagePagesInitialized = false;
|
||||
protected static $homepagePages;
|
||||
|
||||
/**
|
||||
* Register admin menu (or multiple ones)
|
||||
@@ -40,22 +42,28 @@ abstract class OnePage implements TemplateHandler
|
||||
*/
|
||||
public static function getPagesOnHomepage()
|
||||
{
|
||||
$pageIds = (array)json_decode(Settings::get(self::HOMEPAGE_PAGES_SETTINGS_KEY, '[]'));
|
||||
if (!self::$homepagePagesInitialized) {
|
||||
$pageIds = (array)json_decode(Settings::get(self::HOMEPAGE_PAGES_SETTINGS_KEY, '[]'));
|
||||
|
||||
if (empty($pageIds)) {
|
||||
return [];
|
||||
if (empty($pageIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// get all available pages
|
||||
$pages = Content::whereIn('id', $pageIds);
|
||||
|
||||
if (Auth::user()) {
|
||||
$pages = $pages->where('status', '!=', Content::STATUS_DRAFT);
|
||||
} else {
|
||||
$pages = $pages->where('status', Content::STATUS_PUBLIC);
|
||||
}
|
||||
|
||||
self:: $homepagePages = $pages->get();
|
||||
|
||||
self::$homepagePagesInitialized = true;
|
||||
}
|
||||
|
||||
// get all available pages
|
||||
$pages = Content::whereIn('id', $pageIds);
|
||||
|
||||
if (Auth::user()) {
|
||||
$pages = $pages->where('status', '!=', Content::STATUS_DRAFT);
|
||||
} else {
|
||||
$pages = $pages->where('status', Content::STATUS_PUBLIC);
|
||||
}
|
||||
|
||||
return $pages->get();
|
||||
return self::$homepagePages;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user