-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControllerListComponent.php
180 lines (171 loc) · 4.97 KB
/
ControllerListComponent.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
<?php
/**
* Controller List component class.
*
* @link http://github.com/cdburgess/SUM-Cake
* @package cake
* @subpackage app.controllers.components
* @license http://creativecommons.org/licenses/by-sa/3.0/
*/
App::uses('Component', 'Controller');
class ControllerListComponent extends Component {
/**
* Initialize
*
* @param object $controller Reference to the calling controller
* @return void
* @access public
*/
public function initialize(Controller $controller) {
$this->controller = $controller;
}
/**
* shutdown
*
* @return void
* @access public
* @author Chuck Burgess
**/
public function shutdown(Controller $controller) {
}
/**
* Startup
*
* called after Controller::beforeFilter()
*
* @param object $controller Reference to the calling controller
* @return void
* @access public
*/
public function startup(Controller $controller) {
}
/**
* Get
*
* Returns the array of the controllers and their methods
*
* @return array List of all controllers and their methods in this system
* @access public
*/
public function get() {
return $this->getControllers();
}
/**
* Methods
*
* List of all methods in each controller. This will include plugin controllers. All plugin
* controllers will be prepended with the plugin name: Plugin.Controller:method
*
* @return array $methods All of the methods
* @access public
*/
public function methods() {
$all = $this->get();
$plugins = App::objects('plugin');
if (!empty($plugins)) {
$all = array_merge($all, $this->_getPluginControllerMethods());
}
foreach ($all as $key => $value) {
$methods[$key.':*'] = $key.':*';
foreach ($value as $number => $action) {
$methods[$key.':'.$action] = $key.':'.$action;
}
}
sort($methods);
foreach ($methods as $value) {
$data[$value] = $value;
}
return $data;
}
/**
* Get Controllers
*
* Get a list of controllers as objects
*
* @return array $controllers List of all controllers in this system
* @access public
*/
public function getControllers() {
$controllerList = App::objects('controller', App::path('controllers'), false); // clear and rebuild the cache
$controllers = array();
foreach ($controllerList as $controller) {
if ($controller !== 'AppController') {
$controllerName = preg_replace('/Controller$/', '', $controller);
$controllers[$controllerName] = $this->getControllerMethods($controller);
}
}
return $controllers;
}
/**
* Get Controller Methods
*
* Get all of the methods from each of the controllers. This will also include the controllers
* found in paths included by App::build. Use the reflection class and get only the names of
* the public methods in the controller class, sort them, and return the array.
*
* @param string $controllerName The name of the controller to query
* @return array $classMethodsCleaned All of the methods from the class
* @access public
*/
public function getControllerMethods($controllerName) {
$classMethodsCleaned = array();
App::uses($controllerName, 'Controller');
$controller = new ReflectionClass($controllerName);
$methods = array();
foreach ($controller->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class == $controllerName) {
$methods[] = $method->name;
}
}
$subClassMethods = get_class_methods($controllerName);
$parentClass = get_parent_class($controllerName);
$parentClassMethods = get_class_methods($parentClass);
$classMethods = array_diff($subClassMethods, $parentClassMethods);
$parentClassMethods = get_class_methods('Controller');
$classMethods = array_diff($classMethods, $parentClassMethods);
foreach ($classMethods as $method) {
if ($method{0} <> "_") {
$classMethodsCleaned[] = $method;
}
}
sort($classMethodsCleaned);
return $classMethodsCleaned;
}
/**
* Get Plugin Controller Methods
*
* Get all a list of plugin controllers as objects and their methods
*
* @return array $controllers List of all plugin controllers and their methods
* @access protected
*/
protected function _getPluginControllerMethods() {
$plugins = App::objects('plugin');
$controllers = array();
if (!empty($plugins)) {
CakePlugin::loadAll();
$parentClassMethods = get_class_methods('Controller');
foreach ($plugins as $plugin) {
$controllerList = App::objects($plugin.'.Controller');
foreach ($controllerList as $controller) {
$controllerName = $plugin.'.'. $controller;
$controllerName = preg_replace('/Controller$/', '', $controller);
App::uses($controller, $plugin.'.Controller');
if ($controller !== $plugin.'AppController') {
$subClassMethods = get_class_methods($controller);
if (is_array($subClassMethods)) {
$classMethods = array_diff($subClassMethods, $parentClassMethods);
foreach ($classMethods as $method) {
if ($method{0} <> "_") {
$classMethodsCleaned[] = $method;
}
}
$controllers[$controllerName] = $classMethods;
}
}
}
}
}
return $controllers;
}
}