1 <?php
2 3 4 5 6 7 8
9 class RGenerator extends CApplicationComponent
10 {
11 private $_authManager;
12 private $_items;
13
14 15 16
17 public $db;
18
19 20 21
22 public function init()
23 {
24 parent::init();
25
26 $this->_authManager = Yii::app()->getAuthManager();
27 $this->db = $this->_authManager->db;
28 }
29
30 31 32 33
34 public function run()
35 {
36 $authManager = $this->_authManager;
37 $itemTable = $authManager->itemTable;
38
39
40 $txn = $this->db->beginTransaction();
41
42 try
43 {
44 $generatedItems = array();
45
46
47 foreach( $this->_items as $type=>$items )
48 {
49
50 foreach( $items as $name )
51 {
52
53 if( $authManager->getAuthItem($name)===null )
54 {
55
56 $sql = "INSERT INTO {$itemTable} (name, type, data)
57 VALUES (:name, :type, :data)";
58 $command = $this->db->createCommand($sql);
59 $command->bindValue(':name', $name);
60 $command->bindValue(':type', $type);
61 $command->bindValue(':data', 'N;');
62 $command->execute();
63
64 $generatedItems[] = $name;
65 }
66 }
67 }
68
69
70 $txn->commit();
71 return $generatedItems;
72 }
73 catch( CDbException $e )
74 {
75
76 $txn->rollback();
77 return false;
78 }
79 }
80
81 82 83 84 85
86 public function addItems($items, $type)
87 {
88 if( isset($this->_items[ $type ])===false )
89 $this->_items[ $type ] = array();
90
91 foreach( $items as $itemname )
92 $this->_items[ $type ][] = $itemname;
93 }
94
95 96 97 98
99 public function getControllerActions($items=null)
100 {
101 if( $items===null )
102 $items = $this->getAllControllers();
103
104 foreach( $items['controllers'] as $controllerName=>$controller )
105 {
106 $actions = array();
107 $file = fopen($controller['path'], 'r');
108 $lineNumber = 0;
109 while( feof($file)===false )
110 {
111 ++$lineNumber;
112 $line = fgets($file);
113 preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches);
114 if( $matches!==array() )
115 {
116 $name = $matches[1];
117 $actions[ strtolower($name) ] = array(
118 'name'=>$name,
119 'line'=>$lineNumber
120 );
121 }
122 }
123
124 $items['controllers'][ $controllerName ]['actions'] = $actions;
125 }
126
127 foreach( $items['modules'] as $moduleName=>$module )
128 $items['modules'][ $moduleName ] = $this->getControllerActions($module);
129
130 return $items;
131 }
132
133 134 135 136
137 protected function getAllControllers()
138 {
139 $basePath = Yii::app()->basePath;
140 $items['controllers'] = $this->getControllersInPath($basePath.DIRECTORY_SEPARATOR.'controllers');
141 $items['modules'] = $this->getControllersInModules($basePath);
142 return $items;
143 }
144
145 146 147 148 149
150 protected function getControllersInPath($path)
151 {
152 $controllers = array();
153
154 if( file_exists($path)===true )
155 {
156 $controllerDirectory = scandir($path);
157 foreach( $controllerDirectory as $entry )
158 {
159 if( $entry{0}!=='.' )
160 {
161 $entryPath = $path.DIRECTORY_SEPARATOR.$entry;
162 if( strpos(strtolower($entry), 'controller')!==false )
163 {
164 $name = substr($entry, 0, -14);
165 $controllers[ strtolower($name) ] = array(
166 'name'=>$name,
167 'file'=>$entry,
168 'path'=>$entryPath,
169 );
170 }
171
172 if( is_dir($entryPath)===true )
173 foreach( $this->getControllersInPath($entryPath) as $controllerName=>$controller )
174 $controllers[ $controllerName ] = $controller;
175 }
176 }
177 }
178
179 return $controllers;
180 }
181
182 183 184 185 186
187 protected function getControllersInModules($path)
188 {
189 $items = array();
190
191 $modulePath = $path.DIRECTORY_SEPARATOR.'modules';
192 if( file_exists($modulePath)===true )
193 {
194 $moduleDirectory = scandir($modulePath);
195 foreach( $moduleDirectory as $entry )
196 {
197 if( substr($entry, 0, 1)!=='.' && $entry!=='rights' )
198 {
199 $subModulePath = $modulePath.DIRECTORY_SEPARATOR.$entry;
200 if( file_exists($subModulePath)===true )
201 {
202 $items[ $entry ]['controllers'] = $this->getControllersInPath($subModulePath.DIRECTORY_SEPARATOR.'controllers');
203 $items[ $entry ]['modules'] = $this->getControllersInModules($subModulePath);
204 }
205 }
206 }
207 }
208
209 return $items;
210 }
211 }
212