|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
9
|
|
|
use Yiisoft\Aliases\Aliases; |
|
10
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
11
|
|
|
use Yiisoft\Yii\Web\Exception\BadUriPrefixException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This middleware supports routing when webroot is not the same folder as public |
|
15
|
|
|
*/ |
|
16
|
|
|
final class SubFolder implements MiddlewareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public ?string $prefix = null; |
|
19
|
|
|
private UrlGeneratorInterface $uriGenerator; |
|
20
|
|
|
private Aliases $aliases; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(UrlGeneratorInterface $uriGenerator, Aliases $aliases) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->uriGenerator = $uriGenerator; |
|
25
|
|
|
$this->aliases = $aliases; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
29
|
|
|
{ |
|
30
|
|
|
$uri = $request->getUri(); |
|
31
|
|
|
$path = $uri->getPath(); |
|
32
|
|
|
$auto = $this->prefix === null; |
|
33
|
|
|
$length = $auto ? 0 : strlen($this->prefix); |
|
34
|
|
|
|
|
35
|
|
|
if ($auto) { |
|
36
|
|
|
// automatically check that the project is in a subfolder |
|
37
|
|
|
// and uri contain a prefix |
|
38
|
|
|
$scriptName = $request->getServerParams()['SCRIPT_NAME']; |
|
39
|
|
|
if (strpos($scriptName, '/', 1) !== false) { |
|
40
|
|
|
$prefix = substr($scriptName, 0, strrpos($scriptName, '/')); |
|
41
|
|
|
if (strpos($path, $prefix) === 0) { |
|
42
|
|
|
$this->prefix = $prefix; |
|
43
|
|
|
$length = strlen($this->prefix); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} elseif ($length > 0) { |
|
47
|
|
|
if ($this->prefix[-1] === '/') { |
|
48
|
|
|
throw new BadUriPrefixException('Wrong URI prefix value'); |
|
49
|
|
|
} |
|
50
|
|
|
if (strpos($path, $this->prefix) !== 0) { |
|
51
|
|
|
throw new BadUriPrefixException('URI prefix does not match'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($length > 0) { |
|
56
|
|
|
$newPath = substr($path, $length); |
|
57
|
|
|
if ($newPath === '') { |
|
58
|
|
|
$newPath = '/'; |
|
59
|
|
|
} |
|
60
|
|
|
if ($newPath[0] !== '/') { |
|
61
|
|
|
if (!$auto) { |
|
62
|
|
|
throw new BadUriPrefixException('URI prefix does not match completely'); |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
|
|
$request = $request->withUri($uri->withPath($newPath)); |
|
66
|
|
|
$this->uriGenerator->setUriPrefix($this->prefix); |
|
|
|
|
|
|
67
|
|
|
// rewrite alias |
|
68
|
|
|
$this->aliases->set('@web', $this->prefix . '/'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $handler->handle($request); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|