1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright Copyright (C) 2019 Sergio coderius <coderius> |
4
|
|
|
* @license This program is free software: the MIT License (MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace coderius\hitCounter; |
8
|
|
|
|
9
|
|
|
use Yii; |
10
|
|
|
use yii\web\GroupUrlRule; |
11
|
|
|
use \yii\base\Application; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
|
14
|
|
|
class Module extends \yii\base\Module |
15
|
|
|
{ |
16
|
|
|
/** @var string module name */ |
17
|
|
|
public static $moduleName = 'hitCounter'; |
18
|
|
|
|
19
|
|
|
/** @var string|null */ |
20
|
|
|
public $userIdentityClass = null; |
21
|
|
|
|
22
|
|
|
public $controllerNamespace = 'coderius\hitCounter\controllers'; |
23
|
|
|
|
24
|
27 |
|
public function init() |
25
|
|
|
{ |
26
|
27 |
|
parent::init(); |
27
|
|
|
|
28
|
27 |
|
\Yii::configure($this, require __DIR__ . '/config/main.php'); |
29
|
|
|
|
30
|
|
|
//иначе ломает консольные комманды |
31
|
27 |
|
if (Yii::$app instanceof yii\web\Application) { |
32
|
27 |
|
if ($this->userIdentityClass === null) { |
33
|
15 |
|
$this->userIdentityClass = \Yii::$app->getUser()->identityClass; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// var_dump(Module::t('messages', 'No comments yet.'));die; |
38
|
27 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds UrlManager rules. |
42
|
|
|
* |
43
|
|
|
* @param Application $app the application currently running |
44
|
|
|
*/ |
45
|
|
|
public function addUrlManagerRules(Application $app) |
46
|
|
|
{ |
47
|
|
|
$app->urlManager->addRules([new GroupUrlRule([ |
48
|
|
|
'prefix' => $this->id, |
49
|
|
|
'rules' => require __DIR__ . '/config/_routes.php', |
50
|
|
|
])], true); |
51
|
|
|
|
52
|
|
|
// var_dump(\Yii::$app->urlManager); |
53
|
|
|
// var_dump($this->id.'s'); |
54
|
|
|
// die; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
9 |
|
public static function selfInstance() |
61
|
|
|
{ |
62
|
9 |
|
return \Yii::$app->getModule(static::$moduleName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get default model classes. |
67
|
|
|
*/ |
68
|
6 |
|
public function getDefaultModels() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
6 |
|
'HitCounterModel' => \coderius\hitCounter\models\HitCounterModel::class, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public function model(string $name) |
76
|
|
|
{ |
77
|
3 |
|
$models = $this->getDefaultModels(); |
78
|
3 |
|
if (array_key_exists($name, $models)) { |
79
|
3 |
|
return $models[$name]; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|