forked from artkost/yii2-qa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
executable file
·122 lines (109 loc) · 2.95 KB
/
Module.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
<?php
namespace artkost\qa;
use yii\base\InvalidCallException;
use yii\helpers\Url;
use Yii;
/**
* This is the main module class for the QA module.
*
* To use QA, include it as a module in the application configuration like the following:
*
* ~~~
* return [
* ......
* 'modules' => [
* 'qa' => ['class' => 'artkost\qa\Module'],
* ],
* ]
* ~~~
*
* With the above configuration, you will be able to access QA Module in your browser using
* the URL `http://localhost/path/to/index.php?r=qa`
*
* If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]] and you have defined
* custom URL rules or enabled [[UrlManager::enableStrictParsing], you may need to add
* the following URL rules at the beginning of your URL rule set in your application configuration
* in order to access QA:
*
* ~~~
* 'rules' => [
* 'qa' => 'qa',
* 'qa/<controller>' => 'qa/<controller>',
* 'qa/<controller>/<action>' => 'qa/<controller>/<action>',
* ...
* ],
* ~~~
*
* You can then access QA via URL: `http://localhost/path/to/index.php/qa`
*
* @author Nikolay Kostyurin <[email protected]>
* @since 2.0
*/
class Module extends \yii\base\Module
{
/**
* @inheritdoc
*/
public $controllerNamespace = '\artkost\qa\controllers';
/**
* User model class
* @var string
*/
public $userClass = '\app\models\User';
/**
* Formatter function name in user model, or callable
* @var string|callable
*/
public $userNameFormatter = 'getId';
public function init()
{
parent::init();
$i18n = Yii::$app->i18n;
if (!isset($i18n->translations['artkost\qa'])) {
$i18n->translations['artkost\qa'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@artkost/qa/messages',
];
}
}
/**
* Alias function for [[Yii::t()]]
* @param $message
* @param array $params
* @param null $language
* @return string
*/
public static function t($message, $params = [], $language = null)
{
return Yii::t('artkost\qa', $message, $params, $language);
}
/**
* Alias function for [[Url::toRoute()]]
* @param $route
* @param bool $scheme
* @return string
*/
public static function url($route, $scheme = false)
{
return Url::toRoute($route, $scheme);
}
/**
* @param \app\models\User $model
* @return string
* @throws InvalidCallException
*/
public function getUserName($model)
{
//if (method_exists($model, $this->userNameFormatter)) {
return call_user_func([$model, $this->userNameFormatter]);
//} else throw new InvalidCallException('Invalid userNameFormatter function');
}
/**
* Check if is given user unique
*/
public function isUserUnique()
{
return !Yii::$app->user->isGuest;
}
}