Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial transfer #1

Merged
merged 15 commits into from
Feb 29, 2024
2 changes: 1 addition & 1 deletion .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.2']
['8.3']
secrets:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
32 changes: 24 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "yiisoft/request-provider",
"type": "library",
"description": "_____",
"description": "PSR-7 request provider",
"keywords": [
"_____"
"yii3",
arogachev marked this conversation as resolved.
Show resolved Hide resolved
"psr",
"request",
"http",
"provider"
],
"homepage": "https://www.yiiframework.com/",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/_____/issues?state=open",
"issues": "https://github.com/yiisoft/request-provider/issues?state=open",
"forum": "https://www.yiiframework.com/forum/",
"wiki": "https://www.yiiframework.com/wiki/",
"irc": "ircs://irc.libera.chat:6697/yii",
"chat": "https://t.me/yii3en",
"source": "https://github.com/yiisoft/_____"
"source": "https://github.com/yiisoft/request-provider"
},
"funding": [
{
Expand All @@ -28,24 +32,36 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.1"
"php": "^8.1",
"psr/http-message": "^1.0|^2.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0.1",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.20"
"vimeo/psalm": "^5.20",
"yiisoft/di": "^1.2"
},
"autoload": {
"psr-4": {
"Yiisoft\\_____\\": "src"
"Yiisoft\\RequestProvider\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Yiisoft\\_____\\Tests\\": "tests"
"Yiisoft\\RequestProvider\\Tests\\": "tests"
}
},
"extra": {
"config-plugin-options": {
"source-directory": "config"
},
"config-plugin": {
"di-web": "di-web.php"
}
},
"config": {
Expand Down
18 changes: 18 additions & 0 deletions config/di-web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Yiisoft\RequestProvider\RequestProvider;
use Yiisoft\RequestProvider\RequestProviderInterface;

/** @var array $params */

return [
RequestProviderInterface::class => [
'class' => RequestProvider::class,
'reset' => function () {
/** @var RequestProvider $this */
$this->request = null;
},
],
];
31 changes: 31 additions & 0 deletions src/RequestCatcherMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Stores request into {@see RequestProviderInterface}.
* You need to add this into your application middleware stack.
*/
final class RequestCatcherMiddleware implements MiddlewareInterface
{
/**
* @param RequestProviderInterface $provider The request provider.
*/
public function __construct(
private RequestProviderInterface $provider,
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->provider->set($request);
return $handler->handle($request);
}
}
19 changes: 19 additions & 0 deletions src/RequestNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider;

use LogicException;
use Throwable;

/**
* Thrown when request isn't set before.
*/
final class RequestNotSetException extends LogicException
{
public function __construct(int $code = 0, ?Throwable $previous = null)
{
parent::__construct('Request is not set.', $code, $previous);
}
}
32 changes: 32 additions & 0 deletions src/RequestProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider;

use Psr\Http\Message\ServerRequestInterface;

/**
* Stores request for further consumption by attribute handlers.
*/
final class RequestProvider implements RequestProviderInterface
{
/**
* @var ServerRequestInterface|null The request.
*/
private ?ServerRequestInterface $request = null;

public function set(ServerRequestInterface $request): void
{
$this->request = $request;
}

public function get(): ServerRequestInterface
{
if ($this->request === null) {
throw new RequestNotSetException();
}

return $this->request;
}
}
27 changes: 27 additions & 0 deletions src/RequestProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider;

use Psr\Http\Message\ServerRequestInterface;

/**
* `RequestProviderInterface` provides a way to set the current request and then get it in attribute handlers.
*/
interface RequestProviderInterface
{
/**
* Set the current request.
*
* @param ServerRequestInterface $request The request to set.
*/
public function set(ServerRequestInterface $request): void;

/**
* Get the current request.
*
* @throws RequestNotSetException
*/
public function get(): ServerRequestInterface;
}
51 changes: 51 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Di\StateResetter;
use Yiisoft\RequestProvider\RequestNotSetException;
use Yiisoft\RequestProvider\RequestProvider;
use Yiisoft\RequestProvider\RequestProviderInterface;

use function dirname;

final class ConfigTest extends TestCase
{
public function testDi(): void
{
$container = $this->createContainer();

$this->assertInstanceOf(RequestProvider::class, $container->get(RequestProviderInterface::class));
}

public function testResetRequestProvider(): void
{
$container = $this->createContainer();

$requestProvider = $container->get(RequestProviderInterface::class);
$requestProvider->set($this->createMock(ServerRequestInterface::class));

$container->get(StateResetter::class)->reset();

$this->expectException(RequestNotSetException::class);
$requestProvider->get();
}

private function createContainer(): Container
{
return new Container(
ContainerConfig::create()->withDefinitions($this->getContainerDefinitions())
);
}

private function getContainerDefinitions(): array
{
return require dirname(__DIR__) . '/config/di-web.php';
}
}
26 changes: 26 additions & 0 deletions tests/RequestCatcherMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\RequestProvider\RequestCatcherMiddleware;
use Yiisoft\RequestProvider\RequestProvider;

final class RequestCatcherMiddlewareTest extends TestCase
{
public function testBase(): void
{
$requestProvider = new RequestProvider();
$middleware = new RequestCatcherMiddleware($requestProvider);
$request = $this->createMock(ServerRequestInterface::class);
$handler = $this->createMock(RequestHandlerInterface::class);

$middleware->process($request, $handler);

$this->assertSame($request, $requestProvider->get());
}
}
20 changes: 20 additions & 0 deletions tests/RequestNotSetExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\RequestProvider\RequestNotSetException;

final class RequestNotSetExceptionTest extends TestCase
{
public function testBase(): void
{
$exception = new RequestNotSetException();

$this->assertSame('Request is not set.', $exception->getMessage());
$this->assertSame(0, $exception->getCode());
$this->assertNull($exception->getPrevious());
}
}
32 changes: 32 additions & 0 deletions tests/RequestProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\RequestProvider\RequestNotSetException;
use Yiisoft\RequestProvider\RequestProvider;

final class RequestProviderTest extends TestCase
{
public function testBase(): void
{
$requestProvider = new RequestProvider();
$request = $this->createMock(ServerRequestInterface::class);

$requestProvider->set($request);

$this->assertSame($request, $requestProvider->get());
}

public function testRequestNotSet(): void
{
$requestProvider = new RequestProvider();

$this->expectException(RequestNotSetException::class);
$this->expectExceptionMessage('Request is not set.');
$requestProvider->get();
}
}
Loading