1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Request Component. |
9
|
|
|
* |
10
|
|
|
* Extending the {{yii\web\Request}} class by predefine values and add ability to verify whether the request is in admin context or not. |
11
|
|
|
* |
12
|
|
|
* @property boolean $isAdmin Whether the request is admin or not. |
13
|
|
|
* |
14
|
|
|
* @author Basil Suter <[email protected]> |
15
|
|
|
* @since 1.0.0 |
16
|
|
|
*/ |
17
|
|
|
class Request extends \yii\web\Request |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var boolean Force web request to enable unit tests with simulated web requests |
21
|
|
|
*/ |
22
|
|
|
public $forceWebRequest = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string The validation cookie for cookies, should be overwritten in your configuration. |
26
|
|
|
* |
27
|
|
|
* The cookie validation key is generated randomly or by any new release but should be overriten in your config. |
28
|
|
|
* |
29
|
|
|
* http://randomkeygen.com using a 504-bit WPA Key |
30
|
|
|
*/ |
31
|
|
|
public $cookieValidationKey = '(`1gq(|TI2Zxx7zZH<Zk052a9a$@l2EtD9wT`lkTO@7uy{cPaJt4y70mxh4q(3'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array A list of default available parsers. |
35
|
|
|
*/ |
36
|
|
|
public $parsers = [ |
37
|
|
|
'application/json' => 'yii\web\JsonParser', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private $_isAdmin; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Setter method to force isAdmin request. |
44
|
|
|
* |
45
|
|
|
* @param boolean $state Whether its an admin request or not |
46
|
|
|
*/ |
47
|
|
|
public function setIsAdmin($state) |
48
|
|
|
{ |
49
|
|
|
$this->_isAdmin = $state; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Getter method resolves the current url request and check if admin context. |
54
|
|
|
* |
55
|
|
|
* This is mostly used in order to bootstrap more modules and application logic in admin context. |
56
|
|
|
* |
57
|
|
|
* @return boolean If the current request is in admin context return value is true, otherwise false. |
58
|
|
|
*/ |
59
|
|
|
public function getIsAdmin() |
60
|
|
|
{ |
61
|
|
|
if ($this->_isAdmin === null) { |
62
|
|
|
if ($this->getIsConsoleRequest() && !$this->forceWebRequest) { |
63
|
|
|
$this->_isAdmin = false; |
64
|
|
|
} else { |
65
|
|
|
$resolver = Yii::$app->composition->getResolvedPathInfo($this); |
66
|
|
|
$parts = explode('/', $resolver->resolvedPath); |
67
|
|
|
$first = reset($parts); |
68
|
|
|
|
69
|
|
|
if (preg_match('/admin/i', $first, $results)) { |
70
|
|
|
$this->_isAdmin = true; |
71
|
|
|
} else { |
72
|
|
|
$this->_isAdmin = false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->_isAdmin; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the user client language. |
82
|
|
|
* |
83
|
|
|
* @param string $defaultValue Return if not set. |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getClientLanguage($defaultValue) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $defaultValue; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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: