Welcome Guest, Not a member yet? Register   Sign In
Struct: Controller-/Method-Structure to Array
#1

[eluser]trice22[/eluser]
Hello,

there are probably better and easier ways to handle, what I’ve here, but I needed this library for a project and thought I share it and get some opinions:

What does it do?
It “collects” all controllers in a certain folder-scope and pushes them into an array. It also “scans” these controllers and pushes the methods, each controller contains into the same array. Thereby all private functions, functions, that start with a pre-defined identifier (default: “__"), the constructors as well as methods that are being inherited by the Controller-class are being left out.

Example:
Controller1:
Code:
class Controller1 extends Controller {

    function Controller1()
    {
        parent::Controller();
    }

    function function1()
    {
        //…
    }

    function function2()
    {
        //…
    }

    function __function3()
    {
        //…
    }
}

Controller2:
Code:
class Controller2 extends Controller {

    function Controller2()
    {
        parent::Controller();
    }

    function function1()
    {
        //…
    }

    private function function2()
    {
        //…
    }
}

will result in this array:
Code:
Array (
    [0] => Array (
        [controller] => controller1
        [methods] => Array (
            [0] => function1
            [1] => function2 )
        )
    [1] => Array (
        [controller] => controller2
        [methods] => Array (
            [0] => function1 )
        )
    )

How to use it?
Copy “Struct.php” into you library-folder.

In your Model or Controller:
Code:
$this->load->library('Struct');
$structure_array = $this->struct->getArray();

// Optional: Set different folder-scope (default: 'controllers/' )
$this->struct->setScope('controllers/navigation/');

// Optional: Set identifier for methods, that should not be included (default: '__' )
$this->struct-> setPrivId('exclude_this_');

Why?
I’m thinking about using it e.g for automated navigation-generation.
#2

[eluser]trice22[/eluser]
Code?
Code:
<?php
//*** Struct // PHP Class  ***
/*
    version:    0.1
    date:       09/03/08
    author:     Florian Plank - Polarblau
    email:      florian.plank[at]polarblau.com
    license:    MIT
    ---
    Description:
    Retrieve

    USAGE for CodeIgniter
   --------------------------------------------------------------
    Copy into you library-folder:
    - Struct.php
   --------------------------------------------------------------
    in your Model or Controller, e.g. into the constructor
    $this->load->library('Struct');
    $structure_array = $this->struct->getArray();
    
    // Set different scope, relative to "../system/application/"
    $this->struct->setScope('/new/scope/folder');
   --------------------------------------------------------------
  
*/

class Struct
{

    var $private_function_id     = '__';                // functions that should be left out but aren't private
    var $scope_folder            = 'controllers/';    // folder to be scanned
    var $result_array             = array();            // array for the results, which will be returned
    
    /**
    *  Constructor
    *
    *  access public
    *
    *  @return void
    */
    function Struct () {
        // Check if scope ends with a slash
        $scope_folder = (substr($this->scope_folder, -1)=='/') ? $this->scope_folder : $this->scope_folder . '/';
    }
    
    /**
    *  setScope() - changes folder to scanned
    *
    *  access public
    *  @param: $sf:String - new scope
    *
    *  @return void
    */
    public function setScope ($sf)
    {
        $this->scope_folder = $sf;
    }
    
    
    /**
    *  setPrivId() - set start-identifier for functions, which must not
    *
    *  access public
    *  @param: $$id:String - new identifier
    *
    *  @return void
    */
    public function setPrivId ($id)
    {
        $this->private_function_id = $id;
    }
    
    
    /**
    *  dirList() - push content (files) of folder in array
    *
    *  access private
    *  @param $directory:String
    *
    *  @return array
    */
    private function dirList ($directory)
    {
        $results = array();
        $handler = opendir($directory);
        while ($file = readdir($handler)) {
            if ($file != '.' && $file != '..' && preg_match('/php/',$file))
                $results[] = $file;
        }
        closedir($handler);
        return $results;
    }

    /**
    *  getArray() - main function to retrieve the result array
    *
    *  access public
    *
    *  @return array
    */
    public function getArray ()
    {
        // collect all controllers
        $all_controllers = $this->dirList(getcwd() . "/system/application/" . $this->scope_folder);
        
        // get current file
        $current_file = $_SERVER["SCRIPT_NAME"];
        $parts = explode('/', $current_file);
        $current_file = $parts[count($parts) - 1];
        
        // get methods, that have been inherited from CI's Controller-Class
        $native_controller_methods = get_class_methods('Controller');
        
        // Iterate through all controllers
        for($i=0; $i<count($all_controllers); $i++) {
        
            // include all controllers except the current one for scan
            if($all_controllers[$i] != $current_file) {
                include_once(getcwd() . "/system/application/" . $this->scope_folder . $all_controllers[$i]);
            }
        
            // Remove '.php'-from file-name and push the controllername into associative array
            $this->result_array[$i] = array(
                                        'controller' => substr($all_controllers[$i], 0, -4),
                                        'methods' => array()
                                            );
                                            
            // Collect methods
            $class_methods = get_class_methods($this->result_array[$i]['controller']);

            $j=0;
            // populate second dimension of result array
            foreach ($class_methods as $method_name) {
                
                // leave out all methods, which start with "$private_function_id"-String
                // leave out the contructors
                // leave out all native native controller-methods
                // ( private functions will as well be left out naturally )
                $hide = preg_match('/^' . $this->private_function_id . '/', $method_name);
                if(!in_array($method_name, $native_controller_methods) && !$hide && ($this->result_array[$i]['controller'] != strtolower($method_name))) {
                    $this->result_array[$i]['methods'][$j] = $method_name;
                    $j++;
                }
            }
        }
        
        // Return results
        return $this->result_array;
    }
}
?&gt;

Any comments/tests etc. would be highly appreciated.

Thanks,
—trice




Theme © iAndrew 2016 - Forum software by © MyBB