yii-extension / simple-widget
Simple widget for yii3.
Installs: 13 273
Dependents: 4
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- roave/infection-static-analysis-plugin: ^1.7
- vimeo/psalm: ^4.6
- yiisoft/html: ^2.0
This package is auto-updated.
Last update: 2024-11-18 09:54:55 UTC
README
Simple widget
Installation
composer require simple-widget
Usage
Create a new widget without dependencies:
<?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; } }
Using in view:
<?php declare(strict_types=1); ?> Widget::create()->id('id-test')->attributes(['class' => 'text-danger'])->render();
Code generated:
<id="id-test" class="text-danger">
Using widget in view with config factory:
<?php declare(strict_types=1); ?> Widget::create(['attributes()' => [['class' => 'test-class']], 'id()' => ['id-tests']])->render();
Code generated:
<id="id-tests" class="test-class">
Using widget in view with config file:
Load config from file: /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();
Code generated:
<id="id-tests" class="test-class">
Create a new widget with depedencie injection:
<?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; } }
Using view:
<?php declare(strict_types=1); use App\Widget; Widget::create(['attributes()' => [['class' => 'test-class']]], [new Html()])->id('w0')->render();
Code generated:
<id="w0" class="test-class">
Using widget load config file with CONSTANT
:
Defined CONSTANT
: WIDGET_CONFIG_PATH
for example:
define('WIDGET_CONFIG_PATH', __DIR__ . '/config');
Create file /config/widget-definitions.php
:
<?php declare(strict_types=1); return [ // Sintax for array shortNameWidget => [method() => [$value] 'Widget' => [ 'attributes()' => [['class' => 'test-class']], 'id()' => ['id-tests'], ], ];
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
Support the project
License
The simple-widget for Yii Packages is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Extension.