-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUsniAdaptor.php
154 lines (143 loc) · 4.17 KB
/
UsniAdaptor.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* @copyright Copyright (c) 2017 Usha Singhai Neo Informatique Pvt. Ltd
* @license https://github.com/ushainformatique/yiichimp/blob/master/LICENSE.md
*/
namespace usni;
use Yii;
use usni\library\utils\StringUtil;
use usni\library\utils\ArrayUtil;
use yii\db\Expression;
use usni\library\web\Application;
/**
* UsniAdaptor class file.
*
* This class acts as an adaptor to Yii framework and returns different components for it like
* UsniAdaptor::app(), Yii::$app->request so in future if the framework changes it would not have the impact on
* the application. It also acts as the base helper class for any application build using the framework.
*
* @package usni
*/
class UsniAdaptor
{
/**
* Returns the application instance.
* @return Application
*/
public static function app()
{
return Yii::$app;
}
/**
* Gets alias path.
* Wraps around getPathOfAlias of Yii 1.1
* @param string $key
* @param bool $throwException whether to throw an exception if the given alias is invalid.
* @return void
*/
public static function getAlias($key, $throwException = true)
{
if (strncmp($key, '@', 1))
{
return Yii::getAlias('@' . $key, $throwException);
}
return Yii::getAlias($key, $throwException);
}
/**
* Wraps around Yii::t function.
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
* [[\yii\base\Application::language|application language]] will be used.
* @return string the translated message.
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t($category, $message, $params, $language);
}
/**
* Wraps create url method.
* @param string $route
* @param array $params
* @return string
*/
public static function createUrl($route, $params = [])
{
$urlParams = [$route];
$urlParams = ArrayUtil::merge($urlParams, $params);
return self::app()->urlManager->createUrl($urlParams);
}
/**
* Wraps create absoulte url method.
* @param string $route
* @param array $params
* @param string $scheme the scheme to use for the url (either `http` or `https`). If not specified
* the scheme of the current request will be used.
* @return string
*/
public static function createAbsoluteUrl($route, $params = [], $scheme = null)
{
$urlParams = [$route];
$urlParams = ArrayUtil::merge($urlParams, $params);
return self::app()->urlManager->createAbsoluteUrl($urlParams, $scheme);
}
/**
* Get request param
* @param string $param
* @return mixed
*/
public static function getRequestParam($param, $defaultValue = null)
{
return self::app()->request->get($param, $defaultValue);
}
/**
* Return object base class name without namespace.
* @param Object|String $object
* @return string
*/
public static function getObjectClassName($object)
{
if(is_string($object))
{
$qualifiedName = $object;
}
else
{
$qualifiedName = get_class($object);
}
return StringUtil::basename($qualifiedName);
}
/**
* Get yii request object
* @return \yii\web\Request|\yii\console\Request the request component.
*/
public static function getRequest()
{
return self::app()->getRequest();
}
/**
* Get db command.
* @return \Yii::$app->db
*/
public static function db()
{
return Yii::$app->db;
}
/**
* Get table prefix
* @return string
*/
public static function tablePrefix()
{
return UsniAdaptor::app()->db->tablePrefix;
}
/**
* Get now db expression
* @return Expression
*/
public static function getNow()
{
return new Expression('NOW()');
}
}