FEATURE #1422: prepare application for registering widgets
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
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;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->widgets = WidgetTypesWithHandlers::getSimple();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used for registering new widgets
|
||||
*
|
||||
* @param $classname
|
||||
*/
|
||||
public function register($classname)
|
||||
{
|
||||
$class = new $classname;
|
||||
if (!$class instanceof PublicWidget) {
|
||||
throw new InvalidArgumentException($class . ' must implement PublicWidget interface.');
|
||||
}
|
||||
|
||||
// register widget as new widget type
|
||||
if (!in_array($classname, $this->widgets)) {
|
||||
$widgetName = call_user_func($classname . '::getName');
|
||||
$widgetDescription = call_user_func($classname . '::getDescription');
|
||||
|
||||
// create row in widget types
|
||||
WidgetType::create([
|
||||
'handler' => $classname,
|
||||
'name' => $widgetName,
|
||||
'description' => $widgetDescription
|
||||
]);
|
||||
}
|
||||
|
||||
// install widget (if neccessary)
|
||||
call_user_func($classname . '::install');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user