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
+33 -25
View File
@@ -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');
}
}
}
}