1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21
22 class Metadata extends CApplicationComponent
23 {
24
25 26 27 28 29
30 public function getAll()
31 {
32
33 $meta = array(
34 'models' => $this->getModels(),
35 'controllers' => $this->getControllers(),
36 'modules' => $this->getModules(),
37 );
38 foreach ($meta['controllers'] as &$controller) {
39 $controller = array(
40 'name' => $controller,
41 'actions' => $this->getActions($controller)
42 );
43 }
44
45 foreach ($meta['modules'] as &$module) {
46
47 $controllers = $this->getControllers($module);
48
49 foreach ($controllers as &$controller) {
50 $controller = array(
51 'name' => $controller,
52 'actions' => $this->getActions($controller, $module)
53 );
54 }
55
56
57 $module = array(
58 'name' => $module,
59 'controllers' => $controllers,
60 'models' => $this->getModels($module),
61 );
62
63 }
64
65 return $meta;
66
67 }
68
69 70 71 72 73 74 75
76 public function getActions($controller, $module = null)
77 {
78 if ($module != null) {
79 $path = join(DIRECTORY_SEPARATOR, array(Yii::app()->getModule($module)->basePath, 'controllers'));
80 $this->setModuleIncludePaths($module);
81 }
82 else {
83 $path = Yii::getPathOfAlias('application.controllers');
84 }
85
86 $actions = array();
87 $file = fopen($path . DIRECTORY_SEPARATOR . $controller . '.php', 'r');
88 $lineNumber = 0;
89 while (feof($file) === false) {
90 ++$lineNumber;
91 $line = fgets($file);
92 preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches);
93 if ($matches !== array()) {
94 $name = $matches[1];
95 $actions[] = $matches[1];
96 }
97 }
98
99 return $actions;
100
101 }
102
103 104 105 106 107
108 private function setModuleIncludePaths($module)
109 {
110 set_include_path(join(PATH_SEPARATOR, array(
111 get_include_path(),
112
113 join(DIRECTORY_SEPARATOR, array(Yii::app()->modulePath, $module,
114 'components')),
115 join(DIRECTORY_SEPARATOR, array(Yii::app()->modulePath, $module,
116 'models')),
117 join(DIRECTORY_SEPARATOR, array(Yii::app()->modulePath, $module,
118 'vendors')),
119 )));
120 }
121
122 123 124 125 126 127
128 function getControllersActions($module = null)
129 {
130 $c = $this->getControllers($module);
131 foreach ($c as &$controller) {
132 $controller = array(
133 'name' => $controller,
134 'actions' => $this->getActions($controller, $module)
135 );
136 }
137 return $c;
138 }
139
140 141 142 143 144 145 146
147 public function getControllers($module = null)
148 {
149
150 if ($module != null) {
151 $path = join(DIRECTORY_SEPARATOR, array(Yii::app()->getModule($module)->basePath, 'controllers'));
152 }
153 else {
154 $path = Yii::getPathOfAlias('application.controllers');
155 }
156 $controllers = array_filter(scandir($path), array($this, 'isController'));
157 foreach ($controllers as &$c) {
158 $c = str_ireplace('.php', '', $c);
159 }
160 return $controllers;
161 }
162
163 164 165 166 167 168 169
170 public function getModels($module = null, $include_classes = false)
171 {
172
173 if ($module != null) {
174 $path = join(DIRECTORY_SEPARATOR, array(Yii::app()->getModule($module)->basePath, 'models'));
175 }
176 else {
177 $path = Yii::getPathOfAlias('application.models');
178 }
179
180 $models = array();
181 if (is_dir($path)) {
182 $files = scandir($path);
183 foreach ($files as $f) {
184 if (stripos($f, '.php') !== false) {
185 $models[] = str_ireplace('.php', '', $f);
186 if ($include_classes) {
187 include_once($path . DIRECTORY_SEPARATOR . $f);
188 }
189
190 }
191 }
192 }
193 return $models;
194
195 }
196
197 198 199 200 201
202 private function isController($a)
203 {
204 return stripos($a, 'Controller.php') !== false;
205 }
206
207
208 209 210 211
212 public function getModules()
213 {
214 return array_keys(Yii::app()->modules);
215 }
216
217 218 219 220 221
222 private function isModule($a)
223 {
224 return $a != '.' and $a != '..' and is_dir(Yii::app()->modulePath . DIRECTORY_SEPARATOR . $a);
225 }
226
227 }
228