* @package tao */ class WidgetRegistry { const CACHE_KEY = 'tao_widget_data'; /** * Cache of widget informations * @var array */ private static $widgetCache = null; public static function getWidgetDefinitionById($id) { $widgets = self::getWidgetDefinitions(); foreach ($widgets as $widgetDefinition) { if ($widgetDefinition['id'] == strtolower($id)) { return $widgetDefinition; } } // Many widgets don't have a proper definition, don't throw any error // common_Logger::w('Widget with id "'.strtolower($id).'" not found'); return null; } public static function getWidgetDefinition(\core_kernel_classes_Resource $widget) { $widgets = self::getWidgetDefinitions(); if (isset($widgets[$widget->getUri()])) { return $widgets[$widget->getUri()]; } else { common_Logger::w('Widget "' . $widget->getUri() . '" not found'); return null; } } protected static function getWidgetDefinitions() { if (is_null(self::$widgetCache)) { try { self::$widgetCache = common_cache_FileCache::singleton()->get(self::CACHE_KEY); } catch (common_cache_NotFoundException $e) { // not in cache need to load self::$widgetCache = self::getWidgetsFromOntology(); common_cache_FileCache::singleton()->put(self::$widgetCache, self::CACHE_KEY); } } return self::$widgetCache; } /** * @return array */ private static function getWidgetsFromOntology() { $class = new core_kernel_classes_Class(WidgetRdf::CLASS_URI_WIDGET); $rendererClass = new core_kernel_classes_Class(WidgetRdf::CLASS_URI_WIDGET_RENDERER); $widgets = []; foreach ($class->getInstances(true) as $widgetResource) { $id = $widgetResource->getOnePropertyValue(new core_kernel_classes_Property(WidgetRdf::PROPERTY_WIDGET_ID)); if (!is_null($id)) { $renderers = $rendererClass->searchInstances([ WidgetRdf::PROPERTY_WIDGET_RENDERER_WIDGET => $widgetResource->getUri() ], ['like' => false]); $rendererClasses = []; foreach ($renderers as $renderer) { $props = $renderer->getPropertiesValues([ WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE, WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION ]); if (count($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE]) == 1 && count($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION])) { $mode = (string)reset($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_MODE]); $class = (string)reset($props[WidgetRdf::PROPERTY_WIDGET_RENDERER_IMPLEMENTATION]); $rendererClasses[$mode] = $class; } else { common_Logger::w('Definition of $widget renderer.' . $renderer->getUri() . ') invalid'); } } $widgets[$widgetResource->getUri()] = [ 'id' => strtolower($id), 'renderers' => $rendererClasses ]; } else { // deprecated widget $id = substr($widgetResource->getUri(), strpos($widgetResource->getUri(), '#') + 1); $className = "tao_helpers_form_elements_xhtml_" . ucfirst(strtolower($id)); $widgets[$widgetResource->getUri()] = [ 'id' => strtolower($id), 'renderers' => ['xhtml' => $className] ]; } } return $widgets; } }