Changeset 383

Show
Ignore:
Timestamp:
10/05/07 16:42:09 (1 year ago)
Author:
bermiferrer
Message:

Implementing experimental controller "Modules".

Right now all controllers live in 'app/controllers/', which can become a messy place if the application grows.

This new functionality allows you to group, for example administration controllers into app/controllers/admin and the administration views into app/views/admin

The controller generator supports generating modular controllers doing like this:

./script/generate controller Admin::User

generated controller will be at app/controllers/admin/user_controller.php and the class name for the controller will be Admin_UserController

You'll need to edit your config/routes.php file to make your application aware of this new module by adding

$Map->connect('/admin/:controller/:action/:id', array('action' => 'index', 'module' => 'admin'));

before other routes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/config/locales/en.php

    r281 r383  
    148148$dictionary['Could not find the file /app/controllers/<i>%controller_file_name</i> for the controller %controller_class_name'] = 'Could not find the file /app/controllers/<i>%controller_file_name</i> for the controller %controller_class_name'; 
    149149 
     150$dictionary['No controller was specified.'] = 'No controller was specified.'; 
     151 
     152// 2007-10-05 23:28:22 
     153 
     154 
     155$dictionary['Please add force=true to the argument list in order to overwrite existing files.'] = 'Please add force=true to the argument list in order to overwrite existing files.'; 
     156 
     157 
    150158?> 
  • trunk/config/locales/es.php

    r281 r383  
    142142$dictionary['Could not find the file /app/controllers/<i>%controller_file_name</i> for the controller %controller_class_name'] = 'No se ha encontrado el fichero /app/controllers/<i>%controller_file_name</i> del controlador %controller_class_name'; 
    143143 
     144$dictionary['No controller was specified.'] = 'No se ha especificado ningĂșn controlador.'; 
     145 
     146// 2007-10-05 23:28:22 
     147 
     148 
     149$dictionary['Please add force=true to the argument list in order to overwrite existing files.'] = 'Please add force=true to the argument list in order to overwrite existing files.'; 
     150 
     151 
    144152?> 
  • trunk/lib/Ak.php

    r380 r383  
    17771777        $rules = array( 
    17781778        'paranoid' => '/([^A-Z^a-z^0-9^_^-^ ]+)/', 
     1779        'high' => '/([^A-Z^a-z^0-9^_^-^ ^\/^\\\^:]+)/', 
    17791780        'normal' => '/([^A-Z^a-z^0-9^_^-^ ^\.^\/^\\\]+)/' 
    17801781        ); 
  • trunk/lib/AkActionController.php

    r359 r383  
    123123    var $web_service_api; 
    124124    var $web_service_apis = array(); 
     125     
     126    var $module_name; 
    125127 
    126128    /** 
     
    208210            require_once(AK_LIB_DIR.DS.'AkActionView.php'); 
    209211            require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkPhpTemplateHandler.php'); 
    210             $this->Template =& new AkActionView(AK_APP_DIR.DS.'views'.DS.$this->Request->getController(), 
     212            $this->Template =& new AkActionView($this->_getTemplateBasePath(), 
    211213            $this->Request->getParameters(),$this->Request->getController()); 
    212214         
     
    760762    { 
    761763        $defaults = $this->defaultUrlOptions($options); 
     764        if(!empty($this->module_name)){ 
     765            $defaults['module'] = $this->getModuleName(); 
     766        } 
     767        if(!empty($options['controller']) && strstr($options['controller'], '/')){ 
     768            $defaults['module'] = substr($options['controller'], 0, strrpos($options['controller'], '/')); 
     769            $options['controller'] = substr($options['controller'], strrpos($options['controller'], '/') + 1); 
     770        } 
    762771        $options = !empty($defaults) ? array_merge($defaults, $options) : $options; 
    763772        $options['controller'] = empty($options['controller']) ? AkInflector::underscore($this->getControllerName()) : $options['controller']; 
     
    765774    } 
    766775 
    767     function &getControllerName() 
     776    function getControllerName() 
    768777    { 
    769778 
    770779        if(!isset($this->controller_name)){ 
    771780            $current_class_name = get_class($this); 
     781 
    772782            $included_controllers = $this->_getIncludedControllerNames(); 
    773783            $lowercase_included_controllers = array_filter($included_controllers, 'strtolower'); 
    774             $key = array_search(strtolower($current_class_name),$lowercase_included_controllers,true); 
    775             $found_controller = substr($included_controllers[$key],0,-10); 
     784            $key = array_search(strtolower($current_class_name), $lowercase_included_controllers, true); 
     785            $found_controller = substr($included_controllers[$key], 0, -10); 
    776786            $this->controller_name = $found_controller; 
     787            $this->_removeModuleNameFromControllerName(); 
    777788        } 
    778789 
    779790        return $this->controller_name; 
    780791    } 
    781  
    782     /** 
    783      * @todo Refactorize transversing dir instead of looking for loaded files (if faster) 
     792     
     793    function getModuleName() 
     794    { 
     795        return $this->module_name; 
     796    } 
     797 
     798    /** 
     799     * Removes the modules name from the controller if exists. 
     800     *  
     801     * Additionally it sets the default url_options for this controller and the module name scope. 
    784802     */ 
     803    function _removeModuleNameFromControllerName() 
     804    { 
     805        if(strstr($this->controller_name, '::')){ 
     806            $module_parts = substr($this->controller_name, 0, strrpos($this->controller_name, '::')); 
     807            $this->module_name = join('/', array_map(array('AkInflector','underscore'), strstr($module_parts, '::') ? explode('::', $module_parts) : array($module_parts))); 
     808            $this->controller_name = substr($this->controller_name, strrpos($this->controller_name, '::')+2); 
     809        } 
     810    } 
     811     
     812    function _getTemplateBasePath() 
     813    { 
     814        return AK_APP_DIR.DS.'views'.DS.(empty($this->_module_path)?'':$this->_module_path).$this->Request->getController(); 
     815    } 
     816     
    785817    function _getIncludedControllerNames() 
    786818    { 
  • trunk/lib/AkRequest.php

    r364 r383  
    207207        if($found = $Router->toParams($ak_request)){ 
    208208            if(!isset($found['controller'])){ 
    209                 trigger_error(Ak::t('No controller was specified.'),E_USER_WARNING); 
     209                trigger_error(Ak::t('No controller was specified.'), E_USER_WARNING); 
    210210            } 
    211211            if(!isset($found['action'])){ 
    212                 trigger_error(Ak::t('No action was specified.'),E_USER_WARNING); 
     212                trigger_error(Ak::t('No action was specified.'), E_USER_WARNING); 
    213213            } 
    214214 
     
    223223                } 
    224224            } 
    225  
     225            if(isset($found['module'])){       
     226                if($this->_addParam('module',$found['module'])){ 
     227                    $this->module = $this->_request['module'] = $found['module']; 
     228                } 
     229            } 
     230             
    226231            foreach ($found as $k=>$v){ 
    227232                if($this->_addParam($k,$v)){ 
     
    241246    { 
    242247        return $this->_validateTechName($action_name); 
     248    } 
     249 
     250    function isValidModuleName($module_name) 
     251    { 
     252        return preg_match('/^[A-Za-z]{1,}[A-Za-z0-9_\/]*$/', $module_name); 
    243253    } 
    244254 
     
    658668    { 
    659669        if($variable[0] != '_'){ 
    660             if(($variable == 'action' && !$this->isValidActionName($value)) || ( $variable == 'controller' && !$this->isValidControllerName($value))){ 
     670            if( ( $variable == 'action' && !$this->isValidActionName($value)) ||  
     671                ( $variable == 'controller' && !$this->isValidControllerName($value)) || 
     672                ( $variable == 'module' && !$this->isValidModuleName($value)) 
     673                ){ 
    661674                return false; 
    662675            } 
     
    729742         
    730743        $params = $this->getParams(); 
     744 
     745        $module_path = $module_class_peffix = ''; 
     746        if(!empty($params['module'])){ 
     747            $module_path = trim(str_replace(array('/','\\'), DS, Ak::sanitize_include($params['module'], 'high')), DS).DS; 
     748            $module_class_peffix = str_replace(' ','_',AkInflector::titleize(str_replace(DS,' ', trim($module_path, DS)))).'_'; 
     749        } 
     750         
    731751        $controller_file_name = AkInflector::underscore($params['controller']).'_controller.php'; 
    732         $controller_class_name = AkInflector::camelize($params['controller']).'Controller'; 
    733         $controller_path = AK_CONTROLLERS_DIR.DS.$controller_file_name; 
     752        $controller_class_name = $module_class_peffix.AkInflector::camelize($params['controller']).'Controller'; 
     753        $controller_path = AK_CONTROLLERS_DIR.DS.$module_path.$controller_file_name; 
    734754        include_once(AK_APP_DIR.DS.'application_controller.php'); 
    735755        if(@!include_once($controller_path)){ 
     
    744764        } 
    745765        $Controller =& new $controller_class_name(array('controller'=>true)); 
     766        $Controller->_module_path = $module_path; 
    746767        isset($_SESSION) ? $Controller->session =& $_SESSION : null; 
    747768        return $Controller; 
  • trunk/lib/utils/generators/controller/controller_generator.php

    r305 r383  
    2525    function _preloadPaths() 
    2626    { 
    27         $this->class_name = AkInflector::camelize(preg_replace('/_?controller$/i','',$this->class_name)); 
     27        if(!empty($this->class_name_arg)){ 
     28            $this->class_name = $this->class_name_arg; 
     29        } 
     30         
     31        $this->class_name = $this->controller_name = $this->class_name_arg = str_replace('::', '/', AkInflector::camelize(preg_replace('/_?controller$/i','',$this->class_name))); 
     32         
     33        $this->module_path = ''; 
     34 
     35        // Controller inside module 
     36        if(strstr($this->class_name_arg,'/')){ 
     37            $module_parts = substr($this->class_name, 0, strrpos($this->class_name_arg, '/')); 
     38            $this->module_path = join(DS, array_map(array('AkInflector','underscore'), strstr($module_parts, '/') ? explode('/', $module_parts) : array($module_parts))).DS; 
     39 
     40            $this->controller_name = substr($this->class_name_arg, strrpos($this->class_name_arg, '/') + 1); 
     41            $this->underscored_controller_name = $this->module_path.AkInflector::underscore($this->controller_name); 
     42            $this->controller_path = 'controllers'.DS.$this->underscored_controller_name.'_controller.php'; 
     43 
     44            $this->class_name = str_replace('/', '_', $this->class_name_arg); 
     45        }else{ 
     46            $this->underscored_controller_name = AkInflector::underscore($this->class_name); 
     47            $this->controller_path = 'controllers'.DS.$this->underscored_controller_name.'_controller.php'; 
     48        } 
     49 
    2850        $this->assignVarToTemplate('class_name', $this->class_name); 
    29         $this->underscored_controller_name = AkInflector::underscore($this->class_name); 
    30         $this->controller_path = 'controllers'.DS.$this->underscored_controller_name.'_controller.php'; 
     51 
    3152    } 
    32      
     53 
    3354    function hasCollisions() 
    3455    { 
     
    3657        $this->_preloadPaths(); 
    3758        $this->actions = empty($this->actions) ? array() : (array)$this->actions; 
    38          
     59 
    3960        $files = array( 
    4061        AK_APP_DIR.DS.$this->controller_path, 
     
    4667         
    4768        foreach ($this->actions as $action){ 
    48             $files[] = AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name).DS.$action.'.tpl'; 
     69            $files[] = AK_VIEWS_DIR.DS.$this->module_path.AkInflector::underscore($this->controller_name).DS.$action.'.tpl'; 
    4970        } 
    50          
     71 
    5172        foreach ($files as $file_name){ 
    5273            if(file_exists($file_name)){ 
     
    6081    { 
    6182        $this->_preloadPaths(); 
    62          
     83 
    6384        $this->save(AK_APP_DIR.DS.$this->controller_path, $this->render('controller')); 
    6485        $this->save(AK_HELPERS_DIR.DS.$this->underscored_controller_name."_helper.php", $this->render('helper')); 
     
    6687        $this->save(AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.$this->controller_path, $this->render('fixture')); 
    6788        $this->save(AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'helpers'.DS.$this->underscored_controller_name."_helper.php", $this->render('helper_fixture')); 
    68          
    69         @Ak::make_dir(AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name)); 
    70          
     89 
     90        @Ak::make_dir(AK_VIEWS_DIR.DS.$this->module_path.AkInflector::underscore($this->controller_name)); 
     91 
    7192        foreach ($this->actions as $action){ 
    7293            //$this->action = $action; 
    7394            $this->assignVarToTemplate('action',$action); 
    74             $this->assignVarToTemplate('path','AK_VIEWS_DIR.DS.\''.AkInflector::underscore($this->class_name).'/'.$action.'.tpl\''); 
    75             $this->save(AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name).DS.$action.'.tpl', $this->render('view')); 
     95            $this->assignVarToTemplate('path','AK_VIEWS_DIR.DS.\''.$this->module_path.AkInflector::underscore($this->controller_name).'/'.$action.'.tpl\''); 
     96            $this->save(AK_VIEWS_DIR.DS.$this->module_path.AkInflector::underscore($this->controller_name).DS.$action.'.tpl', $this->render('view')); 
    7697        } 
    7798    }