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']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +165,7 @@ class TemplateRegistrator
|
||||
Template::ASSETS_DIRECTORY,
|
||||
Template::MODULES_DIRECTORY
|
||||
];
|
||||
|
||||
|
||||
if (in_array($dirName, $ignoredDirectories)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user