-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManualRedirectsManager.php
More file actions
105 lines (87 loc) · 2.59 KB
/
ManualRedirectsManager.php
File metadata and controls
105 lines (87 loc) · 2.59 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Statamic\Addons\Redirects;
class ManualRedirectsManager extends RedirectsManager
{
/**
* Add or update the given redirect, optionally at a specific position.
*
* @param ManualRedirect $redirect
* @param int $position Zero-based position where to insert the redirect.
*
* @return ManualRedirectsManager
*/
public function add(ManualRedirect $redirect, $position = null)
{
if ($redirect->getFrom() === $redirect->getTo()) {
return $this;
}
$data = $redirect->toArray();
unset($data['from']);
$this->redirects[$redirect->getFrom()] = $data;
if ($position !== null) {
$this->setPosition($redirect->getFrom(), $position);
}
return $this;
}
/**
* Update the position of an existing redirect given by the route.
*
* @param string $route
* @param int $position
*
* @return ManualRedirectsManager
*/
public function setPosition($route, $position)
{
if (!$this->exists($route)) {
return $this;
}
$data = $this->redirects[$route];
unset($this->redirects[$route]);
$redirects = [];
$i = 0;
foreach ($this->redirects as $redirectRoute => $redirectData) {
if ($i === $position) {
$redirects[$route] = $data;
}
$redirects[$redirectRoute] = $redirectData;
$i++;
}
if (!isset($redirects[$route])) {
$redirects[$route] = $data;
}
$this->redirects = $redirects;
return $this;
}
/**
* {@inheritdoc}
*/
public function remove($route)
{
parent::remove($route);
$this->redirectsLogger->removeManualRedirect($route);
return $this;
}
/**
* Get a redirect by route.
*
* @param string $route
*
* @return ManualRedirect
*/
public function get($route)
{
if (!$this->exists($route)) {
return null;
}
$data = $this->redirects[$route];
return (new ManualRedirect())
->setFrom($route)
->setTo($data['to'])
->setStatusCode($data['status_code'])
->setRetainQueryStrings((bool)$data['retain_query_strings'])
->setLocale($data['locale'])
->setStartDate(isset($data['start_date']) && $data['start_date'] ? new \DateTime($data['start_date']) : null)
->setEndDate(isset($data['end_date']) && $data['end_date'] ? new \DateTime($data['end_date']) : null);
}
}