Greetings!
How can I get list of all controllers with their actions in all modules of the application (with routes)?
Tonight I wrote following script …
public function actionGrab(){
$aControllers = [];
$path = APP_PATH . 'protected/';
$ctrls = function($path) use (&$ctrls, &$aControllers){
$oIterator = new DirectoryIterator($path);
foreach($oIterator as $oFile){
if(!$oFile->isDot()
&& (false !== strpos($oFile->getPathname(), 'protected/controllers')
|| false !== strpos($oFile->getPathname(), 'protected/modules')
)
){
if($oFile->isDir()){
$ctrls($oFile->getPathname());
}else{
if(strpos($oFile->getBasename(), 'Controller.php')){
$content = file_get_contents($oFile->getPathname());
$controllerName = $oFile->getBasename('.php');
$route = explode('protected/', $oFile->getPathname());
$route = str_ireplace(array('modules', 'controllers', 'protected', 'Controller.php'), '', $route[1]);
$route = preg_replace("/(\/){2,}/", "/", $route);
$aControllers[$controllerName] = [
'filepath' => $oFile->getPathname(),
'route' => mb_strtolower($route),
'actions' => [],
];
preg_match_all('#function action(.*)\(#ui', $content, $aData);
$acts = function($aData) use (&$aControllers, &$controllerName){
if(!empty($aData) && isset($aData[1]) && !empty($aData[1])){
$aControllers[$controllerName]['actions'] = array_map(
function($actionName){ return mb_strtolower(trim($actionName, '{\\.*()')); },
$aData[1]
);
}
};
$acts($aData);
}
}
}
}
};
$ctrls($path);
echo '<pre>';
print_r($aControllers);
}
and it is shown something like this…
Array
(
[NewsController] => Array
(
[filepath] => /var/www/***/protected/modules/admin/modules/static/controllers/NewsController.php
[route] => /admin/static/news
[actions] => Array
(
[0] => list
[1] => add
[2] => edit
[3] => delete
)
)
[PageController] => Array
(
[filepath] => /var/www/***/protected/controllers/PageController.php
[route] => /page
[actions] => Array
(
[0] => view
[1] => section
)
)
)
I think that script looks really crazy…
Is there more simple way?