-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminModule.php
102 lines (94 loc) · 3.8 KB
/
AdminModule.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
<?php
namespace webkadabra\yii\modules\cms;
/**
* Class Module
* @author Sergii Gamaiunov <[email protected]>
* @package webkadabra\yii\modules\cms
*/
class AdminModule extends Module
{
public $allowedRoles = ['admin'];
public $availableControllerRoutes=[];
public $controllerNamespace = 'webkadabra\yii\modules\cms\adminControllers';
/**
* @var string Path to main application's view path. Should be explicitly defined for advanced applications with different backend/frontend apps
*/
public $frontendViewTemplatesPath = '@app/views/cms-templates';
public function getTemplatesViewsPath()
{
$viewPath = $this->frontendViewTemplatesPath;
$viewPath = \Yii::getAlias($viewPath);
$path = realpath($viewPath);
return $path;
}
/**
* Scan templates folder and return templates and parse their configs (PhpDoc-styled)
* @return array
*/
public function templateListWithConfigs()
{
$templates = array();
$viewPath = $this->getTemplatesViewsPath();
$fileLists = \yii\helpers\FileHelper::findFiles($viewPath,['only'=>[
'*.php',
]]);
foreach ($fileLists as $value) {
// For each file we are trying to read first comments block for template configuration
$tokens = token_get_all(file_get_contents($value));
$viewFilename = basename($value);
if (!$tokens) {
continue;
}
$view_id = str_ireplace($viewPath, '', $value);
$view_id = str_ireplace(DIRECTORY_SEPARATOR, '/', $view_id);
$view_id = trim($view_id, DIRECTORY_SEPARATOR);
$view_id = str_replace('.php', '', $view_id);
$view_id = ltrim($view_id, '/');
if ($viewFilename AND strpos($viewFilename, '.php')) {
$template = array();
foreach ($tokens as $token) {
// Template's first PhpDoc comment block is considered template config
if ($token[0] == T_DOC_COMMENT) {
// Fetch template label
preg_match("/(?<=\*)\sTemplate:([A-Za-z0-9_\s]+)(?=\n)/", $token[1], $nameMatch);
if (!empty($nameMatch[1])) {
$template['label'] = trim($nameMatch[1]);
}
// Fetch template block tokens
preg_match("/(?<=\*)\sBlocks:([A-Za-z0-9_\s,\(\):]+)(?=\n)/", $token[1], $cellsMatch);
if (!empty($cellsMatch[1])) {
if ($cells = explode(',', $cellsMatch[1])) {
foreach ($cells as $cell) {
$cell = trim($cell);
if (strstr($cell, ':')) {
$c = explode(':', $cell);
$cell = ['id' => $c[0], 'hint' => $c[1]];
}
$template['blocks'][] = $cell;
}
}
}
break; // We are not looking for more configs in one template
}
}
if ($template) {
$templates[$view_id] = $template;
}
}
}
return $templates;
}
public function init()
{
\yii\base\Module::init();
$this->registerTranslations();
}
public function registerTranslations()
{
\Yii::$app->i18n->translations['cms*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@vendor/webkadabra/yii2-cms-module/messages',
];
}
}