winter/modules/backend/classes/WidgetManager.php

222 lines
5.4 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Classes;
use Str;
use File;
2015-01-28 18:03:35 +11:00
use Yaml;
2014-05-14 23:24:20 +10:00
use Closure;
use Illuminate\Container\Container;
use System\Classes\PluginManager;
/**
* Widget manager
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class WidgetManager
{
use \October\Rain\Support\Traits\Singleton;
/**
* @var array An array of report widgets.
*/
protected $formWidgets;
/**
* @var array Cache of report widget registration callbacks.
*/
2014-08-01 18:20:55 +10:00
protected $formWidgetCallbacks = [];
2014-05-14 23:24:20 +10:00
/**
* @var array An array of report widgets.
*/
protected $formWidgetHints;
2014-05-14 23:24:20 +10:00
/**
* @var array An array of report widgets.
*/
protected $reportWidgets;
/**
* @var array Cache of report widget registration callbacks.
*/
2014-08-01 18:20:55 +10:00
protected $reportWidgetCallbacks = [];
2014-05-14 23:24:20 +10:00
/**
* @var System\Classes\PluginManager
*/
protected $pluginManager;
/**
* Initialize this singleton.
*/
protected function init()
{
$this->pluginManager = PluginManager::instance();
}
//
// Form Widgets
//
/**
* Returns a list of registered form widgets.
* @return array Array keys are class names.
*/
public function listFormWidgets()
{
if ($this->formWidgets === null) {
$this->formWidgets = [];
/*
* Load module widgets
*/
foreach ($this->formWidgetCallbacks as $callback) {
$callback($this);
}
/*
* Load plugin widgets
*/
$plugins = $this->pluginManager->getPlugins();
foreach ($plugins as $plugin) {
2014-10-10 23:12:50 +02:00
if (!is_array($widgets = $plugin->registerFormWidgets())) {
2014-05-14 23:24:20 +10:00
continue;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-10 23:12:50 +02:00
foreach ($widgets as $className => $widgetInfo) {
2014-05-14 23:24:20 +10:00
$this->registerFormWidget($className, $widgetInfo);
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
}
}
return $this->formWidgets;
}
2014-09-20 10:33:09 +00:00
/**
2014-05-14 23:24:20 +10:00
* Registers a single form form widget.
2014-09-20 10:33:09 +00:00
* @param string $className Widget class name.
* @param array $widgetInfo Registration information, can contain an 'code' key.
2014-09-20 10:33:09 +00:00
* @return void
2014-05-14 23:24:20 +10:00
*/
public function registerFormWidget($className, $widgetInfo = null)
{
$widgetCode = isset($widgetInfo['code']) ? $widgetInfo['code'] : null;
if (!$widgetCode) {
$widgetCode = Str::getClassId($className);
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$this->formWidgets[$className] = $widgetInfo;
$this->formWidgetHints[$widgetCode] = $className;
2014-05-14 23:24:20 +10:00
}
/**
* Manually registers form widget for consideration.
* Usage:
* <pre>
* WidgetManager::registerFormWidgets(function($manager){
* $manager->registerFormWidget('Backend\FormWidgets\CodeEditor', [
* 'name' => 'Code editor',
* 'code' => 'codeeditor'
* ]);
2014-05-14 23:24:20 +10:00
* });
* </pre>
*/
public function registerFormWidgets(callable $definitions)
{
$this->formWidgetCallbacks[] = $definitions;
}
/**
* Returns a class name from a form widget code
* Normalizes a class name or converts an code to it's class name.
* @param string $name Class name or form widget code.
2014-05-21 09:37:58 +10:00
* @return string The class name resolved, or the original name.
2014-05-14 23:24:20 +10:00
*/
public function resolveFormWidget($name)
{
2014-10-10 23:12:50 +02:00
if ($this->formWidgets === null) {
2014-05-14 23:24:20 +10:00
$this->listFormWidgets();
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$hints = $this->formWidgetHints;
2014-05-14 23:24:20 +10:00
if (isset($hints[$name])) {
return $hints[$name];
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
$_name = Str::normalizeClassName($name);
2014-10-10 23:12:50 +02:00
if (isset($this->formWidgets[$_name])) {
2014-05-14 23:24:20 +10:00
return $_name;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
return $name;
}
//
// Report Widgets
//
/**
* Returns a list of registered report widgets.
* @return array Array keys are class names.
*/
public function listReportWidgets()
{
if ($this->reportWidgets === null) {
$this->reportWidgets = [];
/*
* Load module widgets
*/
foreach ($this->reportWidgetCallbacks as $callback) {
$callback($this);
}
/*
* Load plugin widgets
*/
$plugins = $this->pluginManager->getPlugins();
foreach ($plugins as $plugin) {
2014-10-10 23:12:50 +02:00
if (!is_array($widgets = $plugin->registerReportWidgets())) {
2014-05-14 23:24:20 +10:00
continue;
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-10 23:12:50 +02:00
foreach ($widgets as $className => $widgetInfo) {
2014-05-14 23:24:20 +10:00
$this->registerReportWidget($className, $widgetInfo);
2014-10-10 23:12:50 +02:00
}
2014-05-14 23:24:20 +10:00
}
}
return $this->reportWidgets;
}
/*
* Registers a single report widget.
*/
public function registerReportWidget($className, $widgetInfo)
{
$this->reportWidgets[$className] = $widgetInfo;
}
/**
* Manually registers report widget for consideration.
* Usage:
* <pre>
* WidgetManager::registerReportWidgets(function($manager){
* $manager->registerReportWidget('RainLab\GoogleAnalytics\ReportWidgets\TrafficOverview', [
* 'name'=>'Google Analytics traffic overview',
* 'context'=>'dashboard'
* ]);
* });
* </pre>
*/
public function registerReportWidgets(callable $definitions)
{
$this->reportWidgetCallbacks[] = $definitions;
}
2014-10-10 23:12:50 +02:00
}