So here is what I did. In my resource controller:
Enable Class / Reflection Library
use ReflectionClass; use ReflectionMethod;
To get the controllers:
public function getControllers() { $files = scandir('../src/Controller/'); $results = []; $ignoreList = [ '.', '..', 'Component', 'AppController.php', ]; foreach($files as $file){ if(!in_array($file, $ignoreList)) { $controller = explode('.', $file)[0]; array_push($results, str_replace('Controller', '', $controller)); } } return $results; }
And now for the actions:
public function getActions($controllerName) { $className = 'App\\Controller\\'.$controllerName.'Controller'; $class = new ReflectionClass($className); $actions = $class->getMethods(ReflectionMethod::IS_PUBLIC); $results = [$controllerName => []]; $ignoreList = ['beforeFilter', 'afterFilter', 'initialize']; foreach($actions as $action){ if($action->class == $className && !in_array($action->name, $ignoreList)){ array_push($results[$controllerName], $action->name); } } return $results; }
Finally, to tie them both together:
public function getResources(){ $controllers = $this->getControllers(); $resources = []; foreach($controllers as $controller){ $actions = $this->getActions($controller); array_push($resources, $actions); } return $resources; }
I hope this helps some people.
source share