Skip to content

Instantly share code, notes, and snippets.

@pentium10
Created October 7, 2011 09:46
Show Gist options
  • Select an option

  • Save pentium10/1269919 to your computer and use it in GitHub Desktop.

Select an option

Save pentium10/1269919 to your computer and use it in GitHub Desktop.
Implemented support for method 'actions' of controllers
<?php
// SEE
// http://code.google.com/p/yii-rights/issues/detail?id=41&q=actions
/**
* Returns all the controllers and their actions.
* @param array $items the controllers and actions.
*/
public function getControllerActions($items=null) {
if ($items === null)
$items = $this->getAllControllers();
foreach ($items['controllers'] as $controllerName => $controller) {
$actions = array();
$pi = pathinfo($controller['path']);
$className = $pi['filename'];
// check if this class is loaded
if (!class_exists($className, $autoload = false)) {
include($controller['path']);
}
try {
if (class_exists($className)) {
$o = new $className($id = time(), $module = '');
$objActions = $o->actions();
if (!empty($objActions)) {
foreach ($objActions as $name=>$class) {
$actions[strtolower($name)] = array(
'name' => $name,
'line' => 0
);
}
}
}
} catch (Exception $e) {
// ignore any exception that might happened
}
$file = fopen($controller['path'], 'r');
$lineNumber = 0;
while (feof($file) === false) {
++$lineNumber;
$line = fgets($file);
preg_match('/public[ \t]+function[ \t]+action([A-Z]{1}[a-zA-Z0-9]+)[ \t]*\(/', $line, $matches);
if ($matches !== array()) {
$name = $matches[1];
$actions[strtolower($name)] = array(
'name' => $name,
'line' => $lineNumber
);
}
}
$items['controllers'][$controllerName]['actions'] = $actions;
}
foreach ($items['modules'] as $moduleName => $module)
$items['modules'][$moduleName] = $this->getControllerActions($module);
return $items;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment