-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathModule.php
executable file
·135 lines (125 loc) · 3.83 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
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace artkost\qa;
use Yii;
use yii\base\InvalidCallException;
use yii\helpers\Url;
/**
* 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/ask' => 'default/ask',
* 'qa/my' => 'default/my',
* 'qa/favorite' => 'default/favorite',
* 'qa' => 'default/index',
* 'qa/tag/<tags>' => 'default/tags',
* 'qa/tag-suggest' => 'default/tag-suggest',
* 'qa/<alias>-<id>' => 'default/view'
* ...
* ],
* ~~~
*
* 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
{
/**
* Translation category for module
*/
const TRANSLATION = 'artkost/qa/';
/**
* Formatter function for name in user model, or callable
* @var string|callable
*/
public $userNameFormatter = 'getId';
/**
* Formatter function for date in answer and question models, or callable
* @var string|callable
*/
public $dateFormatter;
/**
* Alias function for [[Yii::t()]]
* @param $category
* @param $message
* @param array $params
* @param null $language
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t(self::TRANSLATION . $category, $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
* @param string $attribute
* @return string
* @throws InvalidCallException
*/
public function getUserName($model, $attribute)
{
return $this->callConfigFunction($model, 'userNameFormatter', $attribute, function($modelInstance) {
return $modelInstance->id;
});
}
/**
* @param $model
* @param string $attribute
* @return string
*/
public function getDate($model, $attribute)
{
return $this->callConfigFunction($model, 'dateFormatter', $attribute, function($modelInstance) use($attribute) {
return Yii::$app->formatter->asRelativeTime($modelInstance->{$attribute});
});
}
/**
* @param \app\models\User $model
* @param string $functionName
* @param string $attribute
* @param \Closure $defaultFunction
* @return string
* @throws InvalidCallException
*/
protected function callConfigFunction($model, $functionName, $attribute, $defaultFunction = null)
{
if (is_callable($this->{$functionName})) {
return call_user_func($this->{$functionName}, $model, $attribute);
} else if (method_exists($model, $this->{$functionName})) {
return call_user_func([$model, $this->{$functionName}], $model, $attribute);
} else if ($defaultFunction instanceof \Closure) {
return $defaultFunction($model, $attribute);
} else throw new InvalidCallException("Invalid {$functionName} function");
}
}