HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
RGenerator.php
Go to the documentation of this file.
1 <?php
10 class RGenerator extends CApplicationComponent
11 {
12  private $_authManager;
13  private $_items;
14 
18  public $db;
19 
23  public function init()
24  {
25  parent::init();
26 
27  $this->_authManager = Yii::app()->getAuthManager();
28  $this->db = $this->_authManager->db;
29  }
30 
36  public function run()
37  {
38  $authManager = $this->_authManager;
39  $itemTable = $authManager->itemTable;
40 
41  // Start transaction
42  $txn = $this->db->beginTransaction();
43 
44  try {
45  $generatedItems = array();
46 
47  // Loop through each type of items
48  foreach ($this->_items as $type => $items) {
49  // Loop through items
50  foreach ($items as $name) {
51  // Make sure the item does not already exist
52  if ($authManager->getAuthItem($name) === null) {
53  // Insert item
54  $sql = "INSERT INTO {$itemTable} (name, type, data)
55  VALUES (:name, :type, :data)";
56  $command = $this->db->createCommand($sql);
57  $command->bindValue(':name', $name);
58  $command->bindValue(':type', $type);
59  $command->bindValue(':data', 'N;');
60  $command->execute();
61 
62  $generatedItems[] = $name;
63  }
64  }
65  }
66 
67  // All commands executed successfully, commit
68  $txn->commit();
69 
70  return $generatedItems;
71  } catch (CDbException $e) {
72  // Something went wrong, rollback
73  $txn->rollback();
74 
75  return false;
76  }
77  }
78 
85  public function addItems($items, $type)
86  {
87  if (isset($this->_items[ $type ]) === false) {
88  $this->_items[ $type ] = array();
89  }
90 
91  foreach ($items as $itemname) {
92  $this->_items[ $type ][] = $itemname;
93  }
94  }
95 
101  public function getControllerActions($items = null)
102  {
103  if ($items === null) {
104  $items = $this->getAllControllers();
105  }
106 
107  foreach ($items['controllers'] as $controllerName => $controller) {
108  $actions = array();
109  $file = fopen($controller['path'], 'r');
110  $lineNumber = 0;
111  while (feof($file) === false) {
112  ++$lineNumber;
113  $line = fgets($file);
114  preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches);
115  if ($matches !== array()) {
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 
131  return $items;
132  }
133 
139  protected function getAllControllers()
140  {
141  $basePath = Yii::app()->basePath;
142  $items['controllers'] = $this->getControllersInPath($basePath.DIRECTORY_SEPARATOR.'controllers');
143  $items['modules'] = $this->getControllersInModules($basePath);
144 
145  return $items;
146  }
147 
155  protected function getControllersInPath($path)
156  {
157  $controllers = array();
158 
159  if (file_exists($path) === true) {
160  $controllerDirectory = scandir($path);
161  foreach ($controllerDirectory as $entry) {
162  if ($entry{0} !== '.') {
163  $entryPath = $path.DIRECTORY_SEPARATOR.$entry;
164  if (strpos(strtolower($entry), 'controller') !== false) {
165  $name = substr($entry, 0, -14);
166  $controllers[ strtolower($name) ] = array(
167  'name' => $name,
168  'file' => $entry,
169  'path' => $entryPath,
170  );
171  }
172 
173  if (is_dir($entryPath) === true) {
174  foreach ($this->getControllersInPath($entryPath) as $controllerName => $controller) {
175  $controllers[ $controllerName ] = $controller;
176  }
177  }
178  }
179  }
180  }
181 
182  return $controllers;
183  }
184 
192  protected function getControllersInModules($path)
193  {
194  $items = array();
195 
196  $modulePath = $path.DIRECTORY_SEPARATOR.'modules';
197  if (file_exists($modulePath) === true) {
198  $moduleDirectory = scandir($modulePath);
199  foreach ($moduleDirectory as $entry) {
200  if (substr($entry, 0, 1) !== '.' && $entry !== 'rights') {
201  $subModulePath = $modulePath.DIRECTORY_SEPARATOR.$entry;
202  if (file_exists($subModulePath) === true) {
203  $items[ $entry ]['controllers'] = $this->getControllersInPath($subModulePath.DIRECTORY_SEPARATOR.'controllers');
204  $items[ $entry ]['modules'] = $this->getControllersInModules($subModulePath);
205  }
206  }
207  }
208  }
209 
210  return $items;
211  }
212 }