yii-extension/simple-widget

为yii3提供的简单小部件。

dev-master / 1.0.x-dev 2022-04-12 12:56 UTC

README

简单小部件


Total Downloads Build Status codecov Mutation testing badge static analysis type-coverage

安装

composer require simple-widget

使用方法

创建无依赖的新小部件

<?php

declare(strict_types=1);

namespace App\Widget;

use Yii\Extension\Simple\Widget\AbstractWidget;
use Yiisoft\Html\Html;

final class Widget extends AbstractWidget
{
    protected function run(): string
    {
        return '<' . trim(html::renderTagAttributes($this->attributes)) . '>';
    }

    public function id(string $value): self
    {
        $new = clone $this;
        $new->attributes['id'] = $value;
        return $new;
    }

    protected function beforeRun(): bool
    {
        if (isset($this->attributes['id']) && $this->attributes['id'] === 'beforerun') {
            return false;
        }

        return parent::beforeRun();
    }

    protected function afterRun(string $result): string
    {
        $result = parent::afterRun($result);

        if (isset($this->attributes['id']) && $this->attributes['id'] === 'afterrun') {
            $result = '<div>' . $result . '</div>';
        }

        return $result;
    }
}

在视图中使用

<?php

declare(strict_types=1);
?>

Widget::create()->id('id-test')->attributes(['class' => 'text-danger'])->render();

生成的代码

<id="id-test" class="text-danger">

使用配置工厂在视图中使用小部件

<?php

declare(strict_types=1);
?>

Widget::create(['attributes()' => [['class' => 'test-class']], 'id()' => ['id-tests']])->render();

生成的代码

<id="id-tests" class="test-class">

使用配置文件在视图中使用小部件

从文件加载配置: /config/widget-definitions.php

return [
    'attributes()' => ['class' => 'test-class'],
    'id()' => 'id-tests',
];
<?php

declare(strict_types=1);
?>

Widget::create()->loadConfigFile(__DIR__ . '/config/widget-definitions.php')->render();

生成的代码

<id="id-tests" class="test-class">

创建带有依赖注入的新小部件

<?php

declare(strict_types=1);

namespace App\Widget;

use Yii\Extension\Simple\Widget\AbstractWidget;
use Yiisoft\Html\Html;

final class Widget extends AbstractWidget
{
    public function __construct(private HTML $html)
    {
    }

    protected function run(): string
    {
        return '<' . trim($this->html->renderTagAttributes($this->attributes)) . '>';
    }

    public function id(string $value): self
    {
        $new = clone $this;
        $new->attributes['id'] = $value;
        return $new;
    }
}

使用视图

<?php

declare(strict_types=1);

use App\Widget;

Widget::create(['attributes()' => [['class' => 'test-class']]], [new Html()])->id('w0')->render();

生成的代码

<id="w0" class="test-class">

使用CONSTANT加载小部件配置文件

定义CONSTANT:例如WIDGET_CONFIG_PATH

define('WIDGET_CONFIG_PATH', __DIR__ . '/config');

创建文件 /config/widget-definitions.php

<?php

declare(strict_types=1);

return [
    // Sintax for array shortNameWidget => [method() => [$value]
    'Widget' => [
        'attributes()' => [['class' => 'test-class']],
        'id()' => ['id-tests'],
    ],
];

单元测试

该软件包使用PHPUnit进行测试。要运行测试

./vendor/bin/phpunit

突变测试

该软件包的测试使用Infection突变框架进行测试。要运行它

./vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered

静态分析

代码使用Psalm进行静态分析。要运行静态分析

./vendor/bin/psalm

支持项目

Open Collective

许可

Yii Packages 的 simple-widget 是免费软件。它根据 BSD 许可证发布。有关更多信息,请参阅LICENSE

Yii Extension 维护。