Final Class Yiisoft\Widget\WidgetFactory
| Inheritance | Yiisoft\ |
|---|
WidgetFactory creates an instance of the widget based on the specified configuration {@see WidgetFactory::createWidget()}. Before creating a widget, you need to initialize the WidgetFactory with {@see WidgetFactory::initialize()}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| createWidget() | Creates a widget defined by config passed. | Yiisoft\ |
| initialize() | Yiisoft\ |
|
| setDefaultTheme() | Yiisoft\ |
Method Details
Creates a widget defined by config passed.
See also \
| public static Yiisoft\ | ||
| $config | array |
The parameters for creating a widget. |
| $theme | string|null |
The widget theme. |
| throws | \ |
|
|---|---|---|
| throws | \ |
|
| throws | \ |
|
| throws | \ |
|
public static function createWidget(array $config, ?string $theme = null): Widget
{
if (self::$factory === null) {
self::$factory = new Factory();
}
$className = $config[ArrayDefinition::CLASS_NAME] ?? null;
if (is_string($className)) {
$theme ??= self::$widgetDefaultThemes[$className] ?? self::$defaultTheme;
if ($theme !== null && isset(self::$themes[$theme][$className])) {
$config = ArrayDefinitionHelper::merge(
self::$themes[$theme][$className],
$config,
);
}
}
try {
return self::$factory->create($config);
} catch (FactoryNotInstantiableException $exception) {
/**
* @var string $className When `$className` is not string, `$factory->create()` does not throw
* {@see FactoryNotInstantiableException} exception.
*/
throw new NotInstantiableException($className, self::$initialized, $exception);
}
}
See also \
| public static void initialize ( ?\ | ||
| $container | ?\ |
|
| $definitions | array | |
| $validate | boolean | |
| $themes | array | |
| $defaultTheme | ?string | |
| $widgetDefaultThemes | array | |
| throws | \ |
|
|---|---|---|
public static function initialize(
?ContainerInterface $container = null,
array $definitions = [],
bool $validate = true,
array $themes = [],
?string $defaultTheme = null,
array $widgetDefaultThemes = [],
): void {
self::$factory = new Factory($container, $definitions, $validate);
if ($validate) {
self::assertThemesStructure($themes);
self::assertWidgetDefaultThemesStructure($widgetDefaultThemes);
}
self::$themes = $themes;
self::$defaultTheme = $defaultTheme;
self::$widgetDefaultThemes = $widgetDefaultThemes;
self::$initialized = true;
}
| public static void setDefaultTheme ( ?string $theme ) | ||
| $theme | ?string | |
public static function setDefaultTheme(?string $theme): void
{
self::$defaultTheme = $theme;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.