1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the RHDC Akamai middleware package. |
4
|
|
|
* |
5
|
|
|
* (c) Shawn Iwinski <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view |
8
|
|
|
* the LICENSE file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Rhdc\Akamai\Middleware\Request; |
11
|
|
|
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Rhdc\Akamai\Edge\Resolver\ResolverInterface; |
14
|
|
|
|
15
|
|
|
class ResolverMiddleware implements MiddlewareInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ResolverInterface */ |
18
|
|
|
protected $resolver; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $resolve; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
protected $staging; |
25
|
|
|
|
26
|
17 |
|
public function __construct( |
27
|
|
|
ResolverInterface $resolver, |
28
|
|
|
$resolve = ResolverInterface::RESOLVE_HOST, |
29
|
|
|
$staging = false |
30
|
|
|
) { |
31
|
|
|
$this |
32
|
17 |
|
->setResolver($resolver) |
33
|
17 |
|
->setResolve($resolve) |
34
|
17 |
|
->setStaging($staging); |
35
|
17 |
|
} |
36
|
|
|
|
37
|
17 |
|
public function setResolver(ResolverInterface $resolver) |
38
|
|
|
{ |
39
|
17 |
|
$this->resolver = $resolver; |
40
|
17 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public function getResolver() |
44
|
|
|
{ |
45
|
2 |
|
return $this->resolver; |
46
|
|
|
} |
47
|
|
|
|
48
|
17 |
|
public function setResolve($resolve) |
49
|
|
|
{ |
50
|
17 |
|
$this->resolve = $resolve; |
51
|
17 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function getResolve() |
55
|
|
|
{ |
56
|
3 |
|
return $this->resolve; |
57
|
|
|
} |
58
|
|
|
|
59
|
17 |
|
public function setStaging($staging) |
60
|
|
|
{ |
61
|
17 |
|
$this->staging = (bool) $staging; |
62
|
17 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function getStaging() |
66
|
|
|
{ |
67
|
3 |
|
return $this->staging; |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
public function __invoke(RequestInterface $request) |
71
|
|
|
{ |
72
|
6 |
|
$uri = $request->getUri(); |
73
|
6 |
|
$host = $uri->getHost(); |
74
|
|
|
|
75
|
6 |
|
if (empty($host) || !$this->resolver->isResolvableHost($host)) { |
76
|
2 |
|
return $request; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$resolve = $this->resolver->resolve($host, $this->resolve, $this->staging); |
80
|
4 |
|
$resolveHost = is_array($resolve) ? reset($resolve) : $resolve; |
81
|
4 |
|
$resolveUri = $uri->withHost($resolveHost); |
82
|
|
|
|
83
|
|
|
// Set resolve URI and preserve original host header |
84
|
4 |
|
return $request->withUri($resolveUri, true); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|