PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/rights/components/RGenerator.php

https://gitlab.com/zenfork/vektor
PHP | 211 lines | 140 code | 24 blank | 47 comment | 12 complexity | 43a33813a0f78bfee971db57e4d44499 MD5 | raw file
  1. <?php
  2. /**
  3. * Rights generator component class file.
  4. *
  5. * @author Christoffer Niska <cniska@live.com>
  6. * @copyright Copyright &copy; 2010 Christoffer Niska
  7. * @since 0.9.8
  8. */
  9. class RGenerator extends CApplicationComponent
  10. {
  11. private $_authManager;
  12. private $_items;
  13. /**
  14. * @property CDbConnection
  15. */
  16. public $db;
  17. /**
  18. * Initializes the generator.
  19. */
  20. public function init()
  21. {
  22. parent::init();
  23. $this->_authManager = Yii::app()->getAuthManager();
  24. $this->db = $this->_authManager->db;
  25. }
  26. /**
  27. * Runs the generator.
  28. * @return the items generated or false if failed.
  29. */
  30. public function run()
  31. {
  32. $authManager = $this->_authManager;
  33. $itemTable = $authManager->itemTable;
  34. // Start transaction
  35. $txn = $this->db->beginTransaction();
  36. try
  37. {
  38. $generatedItems = array();
  39. // Loop through each type of items
  40. foreach( $this->_items as $type=>$items )
  41. {
  42. // Loop through items
  43. foreach( $items as $name )
  44. {
  45. // Make sure the item does not already exist
  46. if( $authManager->getAuthItem($name)===null )
  47. {
  48. // Insert item
  49. $sql = "INSERT INTO {$itemTable} (name, type, data)
  50. VALUES (:name, :type, :data)";
  51. $command = $this->db->createCommand($sql);
  52. $command->bindValue(':name', $name);
  53. $command->bindValue(':type', $type);
  54. $command->bindValue(':data', 'N;');
  55. $command->execute();
  56. $generatedItems[] = $name;
  57. }
  58. }
  59. }
  60. // All commands executed successfully, commit
  61. $txn->commit();
  62. return $generatedItems;
  63. }
  64. catch( CDbException $e )
  65. {
  66. // Something went wrong, rollback
  67. $txn->rollback();
  68. return false;
  69. }
  70. }
  71. /**
  72. * Appends items to be generated of a specific type.
  73. * @param array $items the items to be generated.
  74. * @param integer $type the item type.
  75. */
  76. public function addItems($items, $type)
  77. {
  78. if( isset($this->_items[ $type ])===false )
  79. $this->_items[ $type ] = array();
  80. foreach( $items as $itemname )
  81. $this->_items[ $type ][] = $itemname;
  82. }
  83. /**
  84. * Returns all the controllers and their actions.
  85. * @param array $items the controllers and actions.
  86. */
  87. public function getControllerActions($items=null)
  88. {
  89. if( $items===null )
  90. $items = $this->getAllControllers();
  91. foreach( $items['controllers'] as $controllerName=>$controller )
  92. {
  93. $actions = array();
  94. $file = fopen($controller['path'], 'r');
  95. $lineNumber = 0;
  96. while( feof($file)===false )
  97. {
  98. ++$lineNumber;
  99. $line = fgets($file);
  100. preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches);
  101. if( $matches!==array() )
  102. {
  103. $name = $matches[1];
  104. $actions[ strtolower($name) ] = array(
  105. 'name'=>$name,
  106. 'line'=>$lineNumber
  107. );
  108. }
  109. }
  110. $items['controllers'][ $controllerName ]['actions'] = $actions;
  111. }
  112. foreach( $items['modules'] as $moduleName=>$module )
  113. $items['modules'][ $moduleName ] = $this->getControllerActions($module);
  114. return $items;
  115. }
  116. /**
  117. * Returns a list of all application controllers.
  118. * @return array the controllers.
  119. */
  120. protected function getAllControllers()
  121. {
  122. $basePath = Yii::app()->basePath;
  123. $items['controllers'] = $this->getControllersInPath($basePath.DIRECTORY_SEPARATOR.'controllers');
  124. $items['modules'] = $this->getControllersInModules($basePath);
  125. return $items;
  126. }
  127. /**
  128. * Returns all controllers under the specified path.
  129. * @param string $path the path.
  130. * @return array the controllers.
  131. */
  132. protected function getControllersInPath($path)
  133. {
  134. $controllers = array();
  135. if( file_exists($path)===true )
  136. {
  137. $controllerDirectory = scandir($path);
  138. foreach( $controllerDirectory as $entry )
  139. {
  140. if( $entry{0}!=='.' )
  141. {
  142. $entryPath = $path.DIRECTORY_SEPARATOR.$entry;
  143. if( strpos(strtolower($entry), 'controller')!==false )
  144. {
  145. $name = substr($entry, 0, -14);
  146. $controllers[ strtolower($name) ] = array(
  147. 'name'=>$name,
  148. 'file'=>$entry,
  149. 'path'=>$entryPath,
  150. );
  151. }
  152. if( is_dir($entryPath)===true )
  153. foreach( $this->getControllersInPath($entryPath) as $controllerName=>$controller )
  154. $controllers[ $controllerName ] = $controller;
  155. }
  156. }
  157. }
  158. return $controllers;
  159. }
  160. /**
  161. * Returns all the controllers under the specified path.
  162. * @param string $path the path.
  163. * @return array the controllers.
  164. */
  165. protected function getControllersInModules($path)
  166. {
  167. $items = array();
  168. $modulePath = $path.DIRECTORY_SEPARATOR.'modules';
  169. if( file_exists($modulePath)===true )
  170. {
  171. $moduleDirectory = scandir($modulePath);
  172. foreach( $moduleDirectory as $entry )
  173. {
  174. if( substr($entry, 0, 1)!=='.' && $entry!=='rights' )
  175. {
  176. $subModulePath = $modulePath.DIRECTORY_SEPARATOR.$entry;
  177. if( file_exists($subModulePath)===true )
  178. {
  179. $items[ $entry ]['controllers'] = $this->getControllersInPath($subModulePath.DIRECTORY_SEPARATOR.'controllers');
  180. $items[ $entry ]['modules'] = $this->getControllersInModules($subModulePath);
  181. }
  182. }
  183. }
  184. }
  185. return $items;
  186. }
  187. }