1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\RedirectBundle\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Kevin Bond <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
abstract class Redirect |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $source; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $destination; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $permanent; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $count = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DateTime |
32
|
|
|
*/ |
33
|
|
|
protected $lastAccessed = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $source |
37
|
|
|
* @param string $destination |
38
|
|
|
* @param bool $permanent |
39
|
|
|
*/ |
40
|
36 |
|
public function __construct($source, $destination, $permanent = true) |
41
|
|
|
{ |
42
|
36 |
|
$this->setSource($source); |
43
|
36 |
|
$this->setDestination($destination); |
44
|
36 |
|
$this->setPermanent($permanent); |
45
|
36 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $destination |
49
|
|
|
* @param bool $permanent |
50
|
|
|
* |
51
|
|
|
* @return static |
52
|
|
|
*/ |
53
|
1 |
|
public static function createFromNotFound(NotFound $notFound, $destination, $permanent = true) |
54
|
|
|
{ |
55
|
1 |
|
return new static($notFound->getPath(), $destination, $permanent); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
24 |
|
public function getSource() |
62
|
|
|
{ |
63
|
24 |
|
return $this->source; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $source |
68
|
|
|
*/ |
69
|
36 |
|
public function setSource($source) |
70
|
|
|
{ |
71
|
36 |
|
$source = \trim($source); |
72
|
36 |
|
$source = !empty($source) ? $source : null; |
73
|
|
|
|
74
|
36 |
|
if (null !== $source) { |
75
|
34 |
|
$source = $this->createAbsoluteUri($source); |
76
|
|
|
} |
77
|
|
|
|
78
|
36 |
|
$this->source = $source; |
79
|
36 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
13 |
|
public function getDestination() |
85
|
|
|
{ |
86
|
13 |
|
return $this->destination; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $destination |
91
|
|
|
*/ |
92
|
36 |
|
public function setDestination($destination) |
93
|
|
|
{ |
94
|
36 |
|
$destination = \trim($destination); |
95
|
36 |
|
$destination = !empty($destination) ? $destination : null; |
96
|
|
|
|
97
|
36 |
|
if (null !== $destination && null === \parse_url($destination, \PHP_URL_SCHEME)) { |
98
|
27 |
|
$destination = $this->createAbsoluteUri($destination, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
36 |
|
$this->destination = $destination; |
102
|
36 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
3 |
|
public function isPermanent() |
108
|
|
|
{ |
109
|
3 |
|
return $this->permanent; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param bool $permanent |
114
|
|
|
*/ |
115
|
36 |
|
public function setPermanent($permanent) |
116
|
|
|
{ |
117
|
36 |
|
$this->permanent = $permanent; |
118
|
36 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
4 |
|
public function getCount() |
124
|
|
|
{ |
125
|
4 |
|
return $this->count; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $amount |
130
|
|
|
*/ |
131
|
4 |
|
public function increaseCount($amount = 1) |
132
|
|
|
{ |
133
|
4 |
|
$this->count += $amount; |
134
|
4 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return \DateTime |
138
|
|
|
*/ |
139
|
2 |
|
public function getLastAccessed() |
140
|
|
|
{ |
141
|
2 |
|
return $this->lastAccessed; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param \DateTime $time |
146
|
|
|
*/ |
147
|
4 |
|
public function updateLastAccessed(\DateTime $time = null) |
148
|
|
|
{ |
149
|
4 |
|
if (null === $time) { |
150
|
4 |
|
$time = new \DateTime('now'); |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
$this->lastAccessed = $time; |
154
|
4 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $path |
158
|
|
|
* @param bool $allowQueryString |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
36 |
|
protected function createAbsoluteUri($path, $allowQueryString = false) |
163
|
|
|
{ |
164
|
36 |
|
$value = '/'.\ltrim(\parse_url($path, \PHP_URL_PATH), '/'); |
165
|
|
|
|
166
|
36 |
|
if ($allowQueryString && $query = \parse_url($path, \PHP_URL_QUERY)) { |
167
|
1 |
|
$value .= '?'.$query; |
168
|
|
|
} |
169
|
|
|
|
170
|
36 |
|
return $value; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|