0 follower

Final Class Yiisoft\Widget\WidgetFactory

InheritanceYiisoft\Widget\WidgetFactory

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()}.

Method Details

Hide inherited methods

createWidget() public static method

Creates a widget defined by config passed.

See also \Yiisoft\Factory\Factory::create().

public static Yiisoft\Widget\Widget createWidget ( array $config, string|null $theme null )
$config array

The parameters for creating a widget.

$theme string|null

The widget theme.

throws \Yiisoft\Definitions\Exception\CircularReferenceException
throws \Yiisoft\Definitions\Exception\InvalidConfigException
throws \Yiisoft\Factory\NotFoundException
throws \Yiisoft\Definitions\Exception\NotInstantiableException

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

            
initialize() public static method

See also \Yiisoft\Factory\Factory::__construct().

public static void initialize ( \Psr\Container\ContainerInterface|null $container null, array $definitions = [], boolean $validate true, array $themes = [], string|null $defaultTheme null, array $widgetDefaultThemes = [] )
$container \Psr\Container\ContainerInterface|null
$definitions array
$validate boolean
$themes array
$defaultTheme string|null
$widgetDefaultThemes array
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                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;
}

            
setDefaultTheme() public static method

public static void setDefaultTheme ( string|null $theme )
$theme string|null

                public static function setDefaultTheme(?string $theme): void
{
    self::$defaultTheme = $theme;
}