-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedirectEndpoint.php
More file actions
42 lines (35 loc) · 1.13 KB
/
RedirectEndpoint.php
File metadata and controls
42 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Routing package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Polymorphine\Routing\Route\Endpoint;
use Polymorphine\Routing\Route\Endpoint;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Endpoint returning redirect directive with uri string
* produced by given callback (usually referring to router).
*/
class RedirectEndpoint extends Endpoint
{
private $uriCallback;
private int $statusCode;
/**
* @param callable $uriCallback fn() => string
* @param int $statusCode
*/
public function __construct(callable $uriCallback, int $statusCode = 301)
{
$this->uriCallback = $uriCallback;
$this->statusCode = $statusCode;
}
protected function execute(ServerRequestInterface $request, ResponseInterface $prototype): ResponseInterface
{
return $prototype->withStatus($this->statusCode)->withHeader('Location', ($this->uriCallback)());
}
}