Search code examples
phpzend-framework

Get all modules, controllers and actions from a Zend Framework application


I want to create a Zend Controller for ACL management so my problem is: How can I get all Module names, Control names and Action names in a Zend application to build a ACL Control?

I use Zend_Navigation and if the resource don't exist in your ACL Zend_Navigation is thrown a exception. And I want to use a database to deny and allow access. So I must build the database first. And if I must do that by hand it's a pain to do that.


Solution

  • I have created a function that can get all the actions, controllers and modules from a zend application. Here it is:

    $module_dir = substr(str_replace("\\","/",$this->getFrontController()->getModuleDirectory()),0,strrpos(str_replace("\\","/",$this->getFrontController()->getModuleDirectory()),'/'));
        $temp = array_diff( scandir( $module_dir), Array( ".", "..", ".svn"));
        $modules = array();
        $controller_directorys = array();
        foreach ($temp as $module) {
            if (is_dir($module_dir . "/" . $module)) {
                array_push($modules,$module);
                array_push($controller_directorys, str_replace("\\","/",$this->getFrontController()->getControllerDirectory($module)));
            }
        }
    
        foreach ($controller_directorys as $dir) {
            foreach (scandir($dir) as $dirstructure) {
                if (is_file($dir  . "/" . $dirstructure)) {
                    if (strstr($dirstructure,"Controller.php") != false) {
                        include_once($dir . "/" . $dirstructure);
                    }
                }
    
            }
        }
    
        $default_module = $this->getFrontController()->getDefaultModule();
    
        $db_structure = array();
    
        foreach(get_declared_classes() as $c){
            if(is_subclass_of($c, 'Zend_Controller_Action')){
                $functions = array();
                foreach (get_class_methods($c) as $f) {
                    if (strstr($f,"Action") != false) {
                        array_push($functions,substr($f,0,strpos($f,"Action")));
                    }
                }
                $c = strtolower(substr($c,0,strpos($c,"Controller")));
    
                if (strstr($c,"_") != false) {
                    $db_structure[substr($c,0,strpos($c,"_"))][substr($c,strpos($c,"_") + 1)] = $functions;
                }else{
                    $db_structure[$default_module][$c] = $functions;
                }
            }
        }       
    }