Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yii-extension/simple-widget

Simple widget for PHP.
https://github.com/yii-extension/simple-widget

php widget yii2 yii3

Last synced: 25 days ago
JSON representation

Simple widget for PHP.

Awesome Lists containing this project

README

        





Simple widget



[![Total Downloads](https://poser.pugx.org/yii-extension/simple-widget/downloads.png)](https://packagist.org/packages/yii-extension/simple-widget)
[![Build Status](https://github.com/yii-extension/simple-widget/workflows/build/badge.svg)](https://github.com/yii-extension/simple-widget/actions?query=workflow%3Abuild)
[![codecov](https://codecov.io/gh/yii-extension/simple-widget/branch/master/graph/badge.svg?token=gaUysTvoUu)](https://codecov.io/gh/yii-extension/simple-widget)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyii-extension%2Fsimple-widget%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yii-extension/simple-widget/master)
[![static analysis](https://github.com/yii-extension/simple-widget/workflows/static%20analysis/badge.svg)](https://github.com/yii-extension/simple-widget/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yii-extension/simple-widget/coverage.svg)](https://shepherd.dev/github/yii-extension/simple-widget)

## Installation

```shell
composer require simple-widget
```

## Usage

### Create a new widget without dependencies:

```php
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 = '

' . $result . '
';
}

return $result;
}
}
```

Using in view:

```php

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

Code generated:

```html

```

### Using widget in view with config factory:

```php

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

Code generated:

```html

```

### Using widget in view with config file:

Load config from file: `/config/widget-definitions.php`:

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

```php

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

Code generated:
```html

```

### Create a new widget with depedencie injection:

```php
html->renderTagAttributes($this->attributes)) . '>';
}

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

Using view:

```php
[['class' => 'test-class']]], [new Html()])->id('w0')->render();
```

Code generated:
```html

```

### Using widget load config file with `CONSTANT`:

Defined `CONSTANT`: `WIDGET_CONFIG_PATH` for example:

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

Create file `/config/widget-definitions.php`:

```php
[method() => [$value]
'Widget' => [
'attributes()' => [['class' => 'test-class']],
'id()' => ['id-tests'],
],
];
```

### Unit testing

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```shell
./vendor/bin/phpunit
```

### Mutation testing

The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

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

## Static analysis

The code is statically analyzed with [Psalm](https://psalm.dev/docs). To run static analysis:

```shell
./vendor/bin/psalm
```

### Support the project

[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective&logoColor=7eadf1&labelColor=555555)](https://opencollective.com/yiisoft)

## License

The simple-widget for Yii Packages is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Extension](https://github.com/yii-extension).