FEATURE #1422: prepare application for registering widgets
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Enumerations;
|
||||||
|
|
||||||
|
use App\Models\WidgetType;
|
||||||
|
|
||||||
|
class WidgetTypes extends BaseEnumeration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Init list of all installed modules
|
||||||
|
*
|
||||||
|
* @throws Exceptions\EnumerationExistsException
|
||||||
|
*/
|
||||||
|
protected static function init()
|
||||||
|
{
|
||||||
|
$widgets = WidgetType::all(['id', 'name']);
|
||||||
|
|
||||||
|
foreach ($widgets as $widget) {
|
||||||
|
self::push($widget->id, $widget->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Enumerations;
|
||||||
|
|
||||||
|
use App\Models\WidgetType;
|
||||||
|
|
||||||
|
class WidgetTypesWithHandlers extends BaseEnumeration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Init list of all installed modules
|
||||||
|
*
|
||||||
|
* @throws Exceptions\EnumerationExistsException
|
||||||
|
*/
|
||||||
|
protected static function init()
|
||||||
|
{
|
||||||
|
$modules = WidgetType::all(['handler', 'name']);
|
||||||
|
|
||||||
|
foreach ($modules as $module) {
|
||||||
|
self::push($module->handler, $module->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Handlers\Registrators\WidgetRegistrator;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
@@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
//
|
$this->registerWidgets();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,5 +32,23 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
// register PingPong module extension
|
// register PingPong module extension
|
||||||
$this->app->register('Pingpong\Modules\ModulesServiceProvider');
|
$this->app->register('Pingpong\Modules\ModulesServiceProvider');
|
||||||
$this->app->register('Pingpong\Menus\MenusServiceProvider');
|
$this->app->register('Pingpong\Menus\MenusServiceProvider');
|
||||||
|
|
||||||
|
// create singletons for registering widgets
|
||||||
|
$this->app->singleton('registerWidget', function ($app) {
|
||||||
|
return new WidgetRegistrator($app);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for registering widgets.
|
||||||
|
*/
|
||||||
|
private function registerWidgets()
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var $registrator WidgetRegistrator
|
||||||
|
*/
|
||||||
|
$registrator = $this->app->make('registerWidget');
|
||||||
|
|
||||||
|
// register all available widgets
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets\Contracts;
|
||||||
|
|
||||||
|
interface AdminDashboardWidget extends PublicWidget
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Getter for list of all roles which are allowed to display the widget
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function renderForRoles();
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets\Contracts;
|
||||||
|
|
||||||
|
use App\Http\Requests\Request;
|
||||||
|
use App\Models\Widget;
|
||||||
|
|
||||||
|
interface PublicWidget
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Name getter
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for description
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getDescription();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for presenter/renderer of this widget
|
||||||
|
*
|
||||||
|
* @return WidgetRenderer
|
||||||
|
*/
|
||||||
|
public static function getPresenter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Widget $widget
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function save(Widget $widget, Request $request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function for installing given widget
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function install();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function for uninstalling widget
|
||||||
|
*/
|
||||||
|
public static function uninstall();
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets\Contracts;
|
||||||
|
|
||||||
|
use App\Models\Widget;
|
||||||
|
use App\Models\WidgetType;
|
||||||
|
|
||||||
|
interface WidgetRenderer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Method used for displaying widget.
|
||||||
|
*
|
||||||
|
* This method renders some HTML code for displaying.
|
||||||
|
*
|
||||||
|
* @param object $settings
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function render($settings);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display form for creating new instance of widget
|
||||||
|
*
|
||||||
|
* @param WidgetType $type
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function renderAddForm(WidgetType $type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display form for editing already existing widget
|
||||||
|
*
|
||||||
|
* @param Widget $widget
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function renderEditForm(Widget $widget);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class MissingParameterException extends \Exception
|
||||||
|
{
|
||||||
|
public function __construct($message = "", $code = 0, Exception $previous = null)
|
||||||
|
{
|
||||||
|
$message = trans('admin/widgets.missingValue', ['field' => $message]);
|
||||||
|
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets;
|
||||||
|
|
||||||
|
abstract class NonInstallableWidget extends Widget
|
||||||
|
{
|
||||||
|
public static function install()
|
||||||
|
{
|
||||||
|
// nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function uninstall()
|
||||||
|
{
|
||||||
|
// nothing to do
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Widgets;
|
||||||
|
|
||||||
|
use App\Widgets\Contracts\PublicWidget;
|
||||||
|
|
||||||
|
abstract class Widget implements PublicWidget {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user