1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* janitor (http://juliangut.com/janitor). |
5
|
|
|
* Effortless maintenance management. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/janitor |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Janitor\Handler; |
13
|
|
|
|
14
|
|
|
use Janitor\Watcher\ScheduledWatcherInterface; |
15
|
|
|
use Janitor\Watcher\WatcherInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Redirect maintenance handler. |
21
|
|
|
*/ |
22
|
|
|
class Redirect implements HandlerInterface |
23
|
|
|
{ |
24
|
|
|
private $location; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Redirect constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $location |
30
|
|
|
*/ |
31
|
|
|
public function __construct($location) |
32
|
|
|
{ |
33
|
|
|
$this->location = (string) $location; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, WatcherInterface $watcher) |
40
|
|
|
{ |
41
|
|
|
if ($watcher instanceof ScheduledWatcherInterface) { |
42
|
|
|
$response = $response->withHeader('Expires', $watcher->getEnd()->format('D, d M Y H:i:s e')); |
43
|
|
|
} else { |
44
|
|
|
$response = $response->withHeader('Cache-Control', 'max-age=0') |
45
|
|
|
->withHeader('Cache-Control', 'no-cache, must-revalidate') |
46
|
|
|
->withHeader('Pragma', 'no-cache'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$location = $this->location; |
50
|
|
|
if (!preg_match('/^https?:\/\//', $location)) { |
51
|
|
|
$location = (string) $request->getUri()->withPath($location); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $response->withStatus(302) |
55
|
|
|
->withHeader('Location', $location); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|