1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Route |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Route\Extension; |
16
|
|
|
|
17
|
|
|
use Phossa2\Route\Status; |
18
|
|
|
use Phossa2\Route\Dispatcher; |
19
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
20
|
|
|
use Phossa2\Route\Interfaces\ResultInterface; |
21
|
|
|
use Phossa2\Event\EventableExtensionAbstract; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RedirectToHttps |
25
|
|
|
* |
26
|
|
|
* If connection is not secure (not https), set the result status to |
27
|
|
|
* Status::MOVED_PERMANENTLY and return FALSE |
28
|
|
|
* |
29
|
|
|
* @package Phossa2\Route |
30
|
|
|
* @author Hong Zhang <[email protected]> |
31
|
|
|
* @see EventableExtensionAbstract |
32
|
|
|
* @see ResultInterface |
33
|
|
|
* @version 2.0.0 |
34
|
|
|
* @since 2.0.0 added |
35
|
|
|
*/ |
36
|
|
|
class RedirectToHttps extends EventableExtensionAbstract |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public function methodsAvailable()/*# : array */ |
42
|
|
|
{ |
43
|
|
|
return ['redirectIt']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extension methods takes an event as input. |
48
|
|
|
* |
49
|
|
|
* Event params has `Phossa2\Route\Result` set as 'result' |
50
|
|
|
* |
51
|
|
|
* MUST RETURN A BOOLEAN VALUE !!! |
52
|
|
|
* |
53
|
|
|
* @param EventInterface $event |
54
|
|
|
* @return bool |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
public function redirectIt(EventInterface $event)/*# : bool */ |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
/* @var ResultInterface $result */ |
60
|
|
|
$result = $event->getParam('result'); |
61
|
|
|
|
62
|
|
|
if (!isset($_SERVER['HTTPS'])) { |
63
|
|
|
$result->setStatus(Status::MOVED_PERMANENTLY); |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return event handlers of this extension handling |
71
|
|
|
* |
72
|
|
|
* ```php |
73
|
|
|
* protected function extensionHandles() |
74
|
|
|
* { |
75
|
|
|
* return [ |
76
|
|
|
* ['event' => 'cache.*', 'handler' => ['byPassCache', 100]], |
77
|
|
|
* ]; |
78
|
|
|
* } |
79
|
|
|
* ``` |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
* @access protected |
83
|
|
|
*/ |
84
|
|
|
protected function extensionHandles()/*# : array */ |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
[ |
88
|
|
|
'event' => Dispatcher::EVENT_BEFORE_MATCH, |
89
|
|
|
'handler' => 'redirectIt' |
90
|
|
|
] |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: