forked from k-samuel/dvelum
/
Code.php
187 lines (164 loc) · 5.02 KB
/
Code.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
class Backend_Designer_Code
{
/**
* Get controller url
* @param string $controllerName
* @return string
*/
static public function getControllerUrl($controllerName)
{
$appCfg = Registry::get('main' , 'config');
$designerConfig = Config::factory(Config::File_Array, $appCfg->get('configs').'designer.php');
$templates = $designerConfig->get('templates');
if(!class_exists($controllerName))
return '';
$reflector = new ReflectionClass($controllerName);
if(!$reflector->isSubclassOf('Backend_Controller') && !$reflector->isSubclassOf('Frontend_Controller')){
return array();
}
$url = array();
$manager = new Backend_Modules_Manager();
if($reflector->isSubclassOf('Backend_Controller'))
{
$url[] = $templates['adminpath'];
$url[] = $manager->getModuleName($controllerName);
}
elseif ($reflector->isSubclassOf('Frontend_Controller'))
{
if($appCfg['frontend_router_type'] == 'module')
{
$module = self::_moduleByClass($controllerName);
if($module!==false)
{
$urlcode = Model::factory('Page')->getCodeByModule($module);
if($urlcode!==false)
$url[] = $urlcode;
}
}
elseif ($appCfg['frontend_router_type'] == 'path')
{
$paths = explode('_',str_replace(array('Frontend_'), '', $controllerName));
$pathsCount = count($paths)-1;
if($paths[$pathsCount]==='Controller')
$paths = array_slice($paths, 0 ,$pathsCount);
$url = array_merge($url , $paths);
}
elseif($appCfg['frontend_router_type'] == 'config')
{
$urlCode = self::_moduleByClass($controllerName);
if($urlCode!==false){
$url[] = $urlCode;
}
}
}
$url[]='';
Request::setDelimiter($templates['urldelimiter']);
Request::setRoot($templates['wwwroot']);
$url = Request::url($url , false);
Request::setDelimiter($appCfg['urlDelimiter']);
Request::setRoot($appCfg['wwwroot']);
return $url;
}
/**
* Get possible actions from Controller class.
* Note! Code accelerator (eaccelerator, apc, xcache, etc ) should be disabled to get comment line.
* Method returns only public methods that ends with "Action"
* @param string $controllerName
* @return array like array(
* array(
* 'name' => action name without "Action" postfix
* 'comment'=> doc comment
* )
* )
*/
static public function getPossibleActions($controllerName)
{
$manager = new Backend_Modules_Manager();
$appCfg = Registry::get('main' , 'config');
$designerConfig = Config::factory(Config::File_Array, $appCfg->get('configs').'designer.php');
$templates = $designerConfig->get('templates');
$reflector = new ReflectionClass($controllerName);
if(!$reflector->isSubclassOf('Backend_Controller') && !$reflector->isSubclassOf('Frontend_Controller')){
return array();
}
$actions = array();
$methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
$url = array();
if($reflector->isSubclassOf('Backend_Controller'))
{
$url[] = $templates['adminpath'];
$url[] = $manager->getModuleName($controllerName);
}
elseif($reflector->isSubclassOf('Frontend_Controller'))
{
if($appCfg['frontend_router_type'] == 'module')
{
$module = self::_moduleByClass($controllerName);
if($module !== false)
{
$urlcode = Model::factory('Page')->getCodeByModule($module);
if($urlcode !== false)
$url[] = $urlcode;
}
}
elseif($appCfg['frontend_router_type'] == 'path')
{
$paths = explode('_' , str_replace(array('Frontend_') , '' , $controllerName));
$pathsCount = count($paths) - 1;
if($paths[$pathsCount] === 'Controller')
$paths = array_slice($paths , 0 , $pathsCount);
$url = array_merge($url , $paths);
}
elseif($appCfg['frontend_router_type'] == 'config')
{
$urlCode = self::_moduleByClass($controllerName);
if($urlCode!==false){
$url[] = $urlCode;
}
}
}
if(!empty($methods))
{
Request::setDelimiter($templates['urldelimiter']);
Request::setRoot($templates['wwwroot']);
foreach ($methods as $method)
{
if(substr($method->name , -6) !== 'Action')
continue;
$actionName = substr($method->name , 0 , -6);
$paths = $url;
$paths[] = $actionName;
$actions[] = array(
'name'=>$actionName,
'code'=>$method->name,
'url'=>Request::url($paths , false),
'comment'=>self::_clearDocSymbols($method->getDocComment())
);
}
Request::setDelimiter($appCfg['urlDelimiter']);
Request::setRoot($appCfg['wwwroot']);
}
return $actions;
}
static protected function _moduleByClass($class)
{
$modules = Config::factory(Config::File_Array, Registry::get('main' , 'config')->get('frontend_modules'));
if(!empty($modules)){
foreach($modules as $k=>$config){
if($config['class']===$class){
return $k;
}
}
}
return false;
}
/**
* Clear string from comment symbols
* @param string $string
*/
static protected function _clearDocSymbols($string)
{
return str_replace(array('/*','*/','*') , '' , $string);
}
}