<?php
 
    /**
 
    * Class Manager Class.
 
    * 
 
    * @author     Brasoveanu Tudor
 
    * @version    1.0 2011-10-11 
 
    */
 
    
 
    class Manager {
 
        
 
        private $path;
 
        private $classes = array();
 
        private $data = array();
 
        
 
        /**
 
        * Initialize a Manager class
 
        * $data allowed :
 
        * folderName (string) - default : classes
 
        * loadAll (bool) - default : false
 
        * debug (bool) - default : false
 
        * 
 
        * @param array $data
 
        * @return void
 
        */
 
        
 
        public function __construct($data = array()) {
 
            $folderName = 'classes';
 
            $loadAll = false;
 
            $debug = false;
 
            
 
            if(is_array($data) && array_key_exists('folder_name',$data))
 
                $folderName = $data['folderName'];
 
            
 
            if(is_array($data) && array_key_exists('loadAll',$data))
 
                $loadAll = $data['loadAll'];
 
            
 
            if(is_array($data) && array_key_exists('debug',$data))
 
                $debug = $data['debug'];
 
            
 
            if(file_exists($folderName) && is_dir($folderName)) {
 
                $this->path = $folderName.'/';
 
            }
 
            
 
            if(!empty($this->path)) {
 
                $check = $this->MapAllClasses();
 
                if(!$check) die('Could not load classes or folder is empty from path : '.$this->path.'');
 
                
 
                if($debug) {
 
                    echo '<pre>';
 
                    print_r($this->classes);
 
                    echo '</pre>';
 
                }
 
                
 
                if($loadAll) {
 
                    foreach($this->classes AS $class) {
 
                        if(!class_exists($class['class'])) {
 
                            require_once($class['file']);
 
                            $this->data[strtolower($class['class'])] = new $class['class']();
 
                        }
 
                    }
 
                    
 
                    if($debug) {
 
                        echo '<pre>';
 
                        print_r(get_declared_classes());
 
                        echo '</pre>';
 
                    }
 
                }    
 
            }
 
        }
 
        
 
        /**
 
        * Gets the class definition object (class name,method names,class path)
 
        * 
 
        * @param string $php_code
 
        * @param bool $onlypublic
 
        * @return array
 
        */
 
        function GetPHPClasses($php_code,$onlypublic = true) {
 
            $classes = array();
 
            $methods=array();
 
            
 
            $tokens = token_get_all($php_code);
 
            $count = count($tokens);
 
            
 
            for ($i = 2; $i < $count; $i++) {
 
                if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
 
                    $class_name = $tokens[$i][1];
 
                    $methods[$class_name] = array();
 
                }
 
                
 
                if ($tokens[$i - 2][0] == T_FUNCTION && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
 
                    if ($onlypublic) {
 
                        if (!in_array($tokens[$i-4][0],array(T_PROTECTED, T_PRIVATE))) {
 
                            $method_name = $tokens[$i][1];
 
                            $methods[$class_name][] = $method_name;
 
                        }
 
                    } else {
 
                        $method_name = $tokens[$i][1];
 
                        $methods[$class_name][] = $method_name;
 
                    }
 
                }
 
            }
 
            
 
            return $methods;
 
        }
 
        
 
        /**
 
        * Gets the class definition object from a file (class name,method names,class path)
 
        * 
 
        * @param string $filepath
 
        * @param bool $onlypublic
 
        * @return array
 
        */
 
        function FileGetPHPClasses($filepath,$onlypublic=true) {
 
            $php_code = file_get_contents($filepath);
 
            $classes = $this->GetPHPClasses($php_code,$onlypublic);
 
            
 
            return $classes;
 
        }
 
        
 
        /**
 
        * Gets all the class definition objects
 
        * from the classes folder (class name,method names,class path)
 
        * 
 
        * @param string $path
 
        * @param bool $onlypublic
 
        * @return array
 
        */
 
        function MapAllClasses($path = '',$onlypublic = true) {
 
            $result = array();
 
            $return = false;
 
            
 
            if(empty($path))
 
                $path = $this->path;
 
            
 
            if(!empty($path)) {
 
                $dh = opendir($path);
 
                while (($file = readdir($dh)) !== false) {
 
                    if (substr($file,0,1) != ".") {
 
                        if (filetype($path.$file) == "file") {
 
                            $classes = $this->FileGetPHPClasses($path.$file,$onlypublic);
 
                            foreach($classes as $class=>$method) {
 
                                $result[] = array(
 
                                    "file" => $path.$file,
 
                                    "class" => $class,
 
                                    "method" => $method
 
                                );
 
                            }
 
                        } else {
 
                            $result = array_merge($result,$this->MapAllClasses($path.$file."/",$onlypublic));
 
                        }
 
                    }
 
                }
 
                closedir($dh);
 
                
 
                if(is_array($result) && count($result) > 0) {
 
                    $this->classes = $result;
 
                    $return = true;
 
                }
 
            }
 
            
 
            return $return;
 
        }
 
        
 
        /**
 
        * Initialize the wanted classes
 
        * 
 
        * @param string $name
 
        * @param bool $debug
 
        * @return void
 
        */
 
        public function startClass($name = '',$debug = false) {
 
            if(!empty($name) && !class_exists($name)) {
 
                foreach($this->classes AS $class) {
 
                    if(strcasecmp($class['class'],$name) == 0)
 
                    {
 
                        require_once($class['file']);
 
                        $this->data[strtolower($class['class'])] = new $class['class']();
 
                        
 
                        if($debug) {
 
                            echo '<pre>';
 
                            print_r(get_declared_classes());
 
                            echo '</pre>';
 
                        }
 
                        
 
                        break;
 
                    }
 
                }
 
            }
 
        }
 
        
 
        /**
 
        * Runs a specified method from a class
 
        * Example call className_MethodName(parameters)
 
        * 
 
        * @param string $function_name
 
        * @param bool $params
 
        * @return function
 
        */
 
        public function __call($function_name, $params) {
 
            $class_name = '';
 
            $function = '';
 
            
 
            if(strpos($function_name,"_")) {
 
                list($class_name,$function) = explode("_",$function_name);
 
                
 
                if (method_exists($this->data[$class_name], $function)) {
 
                    return call_user_func_array(array($this->data[$class_name], $function), $params);
 
                }    
 
            } else {
 
                foreach($this->data as $class) {
 
                    if (method_exists($class, $function_name)) {
 
                        return call_user_func_array(array($class, $function_name), $params);
 
                    }
 
                }
 
            }
 
        }
 
    }
 
?>
 
 |