/plugins/acl/controllers/components/acl_generate.php

https://github.com/makies/croogo · PHP · 120 lines · 67 code · 12 blank · 41 comment · 10 complexity · 8f03c80f6a882d4e7b220df3741bc3be MD5 · raw file

  1. <?php
  2. /**
  3. * AclGenerate Component
  4. *
  5. * PHP version 5
  6. *
  7. * @category Component
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class AclGenerateComponent extends Object {
  15. /**
  16. * @param object $controller controller
  17. * @param array $settings settings
  18. */
  19. public function initialize(&$controller, $settings = array()) {
  20. $this->controller =& $controller;
  21. App::import('Core', 'File');
  22. $this->folder = new Folder;
  23. }
  24. /**
  25. * List all controllers (including plugin controllers)
  26. *
  27. * @return array
  28. */
  29. public function listControllers() {
  30. $controllerPaths = array();
  31. // app/controllers
  32. $this->folder->path = APP.'controllers'.DS;
  33. $controllers = $this->folder->read();
  34. foreach ($controllers['1'] AS $c) {
  35. if (substr($c, strlen($c) - 4, 4) == '.php') {
  36. $cName = Inflector::camelize(str_replace('_controller.php', '', $c));
  37. $controllerPaths[$cName] = APP.'controllers'.DS.$c;
  38. }
  39. }
  40. // plugins/*/controllers/
  41. $this->folder->path = APP.'plugins'.DS;
  42. $plugins = $this->folder->read();
  43. foreach ($plugins['0'] AS $p) {
  44. if ($p != 'install') {
  45. $this->folder->path = APP.'plugins'.DS.$p.DS.'controllers'.DS;
  46. $pluginControllers = $this->folder->read();
  47. foreach ($pluginControllers['1'] AS $pc) {
  48. if (substr($pc, strlen($pc) - 4, 4) == '.php') {
  49. $pcName = Inflector::camelize(str_replace('_controller.php', '', $pc));
  50. $controllerPaths[$pcName] = APP.'plugins'.DS.$p.DS.'controllers'.DS.$pc;
  51. }
  52. }
  53. }
  54. }
  55. return $controllerPaths;
  56. }
  57. /**
  58. * List actions of a particular Controller.
  59. *
  60. * @param string $name Controller name (the name only, without having Controller at the end)
  61. * @param string $path full path to the controller file including file extension
  62. * @param boolean $all default is false. it true, private actions will be returned too.
  63. *
  64. * @return array
  65. */
  66. public function listActions($name, $path) {
  67. // base methods
  68. if (strpos($path, APP.'plugins') >= 0) {
  69. $plugin = $this->getPluginFromPath($path);
  70. $pacName = Inflector::camelize($plugin) . 'AppController'; // pac - PluginAppController
  71. $pacPath = APP.'plugins'.DS.$plugin.DS.$plugin.'_app_controller.php';
  72. App::import('Controller', $pacName, null, null, $pacPath);
  73. $baseMethods = get_class_methods($pacName);
  74. } else {
  75. $baseMethods = get_class_methods('AppController');
  76. }
  77. $controllerName = $name.'Controller';
  78. App::import('Controller', $controllerName, null, null, $path);
  79. $methods = get_class_methods($controllerName);
  80. // filter out methods
  81. foreach ($methods AS $k => $method) {
  82. if (strpos($method, '_', 0) === 0) {
  83. unset($methods[$k]);
  84. continue;
  85. }
  86. if (in_array($method, $baseMethods)) {
  87. unset($methods[$k]);
  88. continue;
  89. }
  90. }
  91. return $methods;
  92. }
  93. /**
  94. * Get plugin name from path
  95. *
  96. * @param string $path file path
  97. *
  98. * @return string
  99. */
  100. public function getPluginFromPath($path) {
  101. $pathE = explode(DS, $path);
  102. $pluginsK = array_search('plugins', $pathE);
  103. $pluginNameK = $pluginsK + 1;
  104. $plugin = $pathE[$pluginNameK];
  105. return $plugin;
  106. }
  107. }
  108. ?>