Files
core/app/Handlers/Registrators/WidgetRegistrator.php
T

47 lines
1.3 KiB
PHP

<?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');
}
}