Skip to content

Commit

Permalink
Initital commit. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Apr 18, 2023
1 parent 2d81dda commit 8c746a2
Show file tree
Hide file tree
Showing 18 changed files with 837 additions and 99 deletions.
53 changes: 15 additions & 38 deletions README.md
@@ -1,59 +1,36 @@
<p align="center">
<a href="https://github.com/yii-tools/template" target="_blank">
<a href="https://github.com/yii-tools/middleware" target="_blank">
<img src="https://avatars.githubusercontent.com/u/121752654?s=200&v=4" height="100px">
<img src="https://github.com/yii-tools/middleware/blob/initial-commit/docs/middleware.png?raw=true" height="100px">
</a>
<h1 align="center">Template.</h1>
<h1 align="center">Collection of Middlewares for YiiFramework v.3.0.</h1>
<br>
</p>

## Install

For use create a new repository from this template. For more information see [Creating a repository from a template](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template).

Also you can git clone this repository. For more information see [Cloning a repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository).

## Checking dependencies

This package uses [composer-require-checker](https://github.com/maglnet/ComposerRequireChecker) to check if all dependencies are correctly defined in `composer.json`.

To run the checker, execute the following command:

```shell
composer run check-dependencies
```
## Requirements

## Mutation testing
The minimun version of PHP required by this package is PHP 8.1.

Mutation testing is checked with [Infection](https://infection.github.io/). To run it:
For install this package, you need [composer](https://getcomposer.org/) and `mbstring` extension for PHP.

```shell
composer run mutation
```

## Static analysis

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

```shell
composer run psalm
composer require yii-tools/forms
```

## Testing

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

```
composer run test
```
[Check the documentation testing](/docs/testing.md) to learn about testing.

## CI status

[![build](https://github.com/yii-tools/template/actions/workflows/build.yml/badge.svg)](https://github.com/yii-tools/template/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/yii-tools/template/branch/main/graph/badge.svg?token=MF0XUGVLYC)](https://codecov.io/gh/yii-tools/template)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyii-tools%2Ftemplate%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/yii-tools/template/main)
[![static analysis](https://github.com/yii-tools/template/actions/workflows/static.yml/badge.svg)](https://github.com/yii-tools/template/actions/workflows/static.yml)
[![type-coverage](https://shepherd.dev/github/yii-tools/template/coverage.svg)](https://shepherd.dev/github/yii-tools/template)
[![StyleCI](https://github.styleci.io/repos/494495136/shield?branch=main)](https://github.styleci.io/repos/494495136?branch=main)
[![build](https://github.com/yii-tools/middleware/actions/workflows/build.yml/badge.svg)](https://github.com/yii-tools/middleware/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/yii-tools/middleware/branch/main/graph/badge.svg?token=MF0XUGVLYC)](https://codecov.io/gh/yii-tools/middleware)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyii-tools%2Fmiddleware%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/yii-tools/middleware/main)
[![static analysis](https://github.com/yii-tools/middleware/actions/workflows/static.yml/badge.svg)](https://github.com/yii-tools/middleware/actions/workflows/static.yml)
[![type-coverage](https://shepherd.dev/github/yii-tools/middleware/coverage.svg)](https://shepherd.dev/github/yii-tools/middleware)
[![StyleCI](https://github.styleci.io/repos/620894851/shield?branch=main)](https://github.styleci.io/repos/620894851?branch=main)

## License

Expand Down
53 changes: 43 additions & 10 deletions composer.json
@@ -1,41 +1,74 @@
{
"name": "yii-tools/template",
"name": "yii-tools/middleware",
"type": "library",
"description": "_____",
"description": "Middleware for YiiFramework v.3.0.",
"keywords": [
"_____"
"yii-tools",
"middleware",
"yii3"
],
"license": "mit",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.1"
"php": "^8.1",
"psr/http-factory": "^1.0.2",
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"yiisoft/http": "^1.2",
"yiisoft/router-fastroute": "^3.0",
"yiisoft/router": "^3.0",
"yiisoft/translator": "^3.0"
},
"require-dev": {
"httpsoft/http-message": "^1.0.5",
"maglnet/composer-require-checker": "^4.3",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.28",
"vimeo/psalm": "^5.6"
"phpunit/phpunit": "^10.1",
"roave/infection-static-analysis-plugin": "^1.30",
"vimeo/psalm": "^5.8",
"yiisoft/config": "^1.3",
"yiisoft/di": "^1.2",
"yiisoft/yii-event": "^2.0",
"yiisoft/user": "^2.0"
},
"autoload": {
"psr-4": {
"Template\\": "src"
"Yii\\Middleware\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Template\\Tests\\": "tests"
"Yii\\Middleware\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.0.x-dev"
},
"config-plugin-options": {
"source-directory": "config"
},
"config-plugin": {
"di-web": ["web/*.php"],
"params": "params.php"
},
"config-plugin-environments": {
"tests": {
"di": ["common/*.php"],
"di-web": [
"$di",
"web/*.php"
],
"events-web": []
}
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true
"infection/extension-installer": true,
"yiisoft/config": true
}
},
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions config/params.php
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

return [
'yii-tools/middleware' => [
'locale' => [
'languages' => [],
'ignoredUrls' => [],
],
],
];
15 changes: 15 additions & 0 deletions config/web/Guest.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Yii\Middleware\Guest;
use Yiisoft\Definitions\DynamicReference;
use Yiisoft\User\CurrentUser;

return [
Guest::class => [
'__construct()' => [
'isGuest' => DynamicReference::to(static fn (CurrentUser $currentUser): bool => $currentUser->isGuest()),
],
],
];
16 changes: 16 additions & 0 deletions config/web/Locale.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Yii\Middleware\Locale;

/** @var array $params */

return [
Locale::class => [
'__construct()' => [
'languages' => $params['yii-tools/middleware']['locale']['languages'],
'ignoredUrls' => $params['yii-tools/middleware']['locale']['ignoredUrls'],
],
],
];
Binary file added docs/middleware.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions docs/testing.md
@@ -0,0 +1,35 @@
# Testing

## Checking dependencies

This package uses [composer-require-checker](https://github.com/maglnet/ComposerRequireChecker) to check if all dependencies are correctly defined in `composer.json`.

To run the checker, execute the following command:

```shell
composer run check-dependencies
```

## Mutation testing

Mutation testing is checked with [Infection](https://infection.github.io/). To run it:

```shell
composer run mutation
```

## Static analysis

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

```shell
composer run psalm
```

## Unit tests

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

```
composer run test
```
30 changes: 10 additions & 20 deletions phpunit.xml.dist
@@ -1,30 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>

<phpunit
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<ini name="error_reporting" value="-1" />
<ini name="error_reporting" value="-1"/>
</php>

<coverage>
<coverage/>
<testsuites>
<testsuite name="Template">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
</coverage>

<testsuites>
<testsuite name="Template">
<directory>./tests</directory>
</testsuite>
</testsuites>
</source>
</phpunit>
13 changes: 0 additions & 13 deletions src/Example.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/Guest.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Yii\Middleware;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Http\Header;
use Yiisoft\Http\Status;

/**
* Middleware that checks if the user is a guest.
*/
final class Guest implements MiddlewareInterface
{
public function __construct(
private readonly bool $isGuest,
private readonly ResponseFactoryInterface $responseFactory
) {
}

/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->isGuest === false) {
return $this->responseFactory->createResponse(Status::FOUND)->withHeader(Header::LOCATION, '/');
}

return $handler->handle($request);
}
}

0 comments on commit 8c746a2

Please sign in to comment.