Changeset 1140

Show
Ignore:
Timestamp:
09/21/08 19:44:24 (2 months ago)
Author:
bermiferrer
Message:

Moving helper loading code into a HelperLoader? class.

This will help on adding limited helper support to Mailers and Console apps.

It will also help on testing and refactoring existing smelly helper loading code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/AkActionController.php

    r647 r1140  
    230230    function instantiateHelpers() 
    231231    { 
    232         $helpers = $this->getDefaultHelpers(); 
    233         $helpers = array_merge($helpers, $this->getApplicationHelpers()); 
    234         $helpers = array_merge($helpers, $this->getPluginHelpers()); 
    235         $helpers = array_merge($helpers, $this->getModuleHelper()); 
    236         $helpers = array_merge($helpers, $this->getCurrentControllerHelper()); 
    237  
    238         require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 
    239  
    240         $available_helpers = array(); 
    241         foreach ($helpers as $file=>$helper){ 
    242             $helper_class_name = AkInflector::camelize(AkInflector::demodulize(strstr($helper, 'Helper') ? $helper : $helper.'Helper')); 
    243             $full_path = preg_match('/[\\\\\/]+/',$file); 
    244             $file_path = $full_path ? $file : AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$file; 
    245             include_once($file_path); 
    246  
    247             if(class_exists($helper_class_name)){ 
    248                 $attribute_name = $full_path ? AkInflector::underscore($helper_class_name) : substr($file,0,-4); 
    249                 $available_helpers[] = $attribute_name; 
    250                 $this->$attribute_name =& new $helper_class_name(&$this); 
    251                 if(method_exists($this->$attribute_name,'setController')){ 
    252                     $this->$attribute_name->setController(&$this); 
    253                 } 
    254                 if(method_exists($this->$attribute_name,'init')){ 
    255                     $this->$attribute_name->init(); 
    256                 } 
    257             } 
    258         } 
    259         defined('AK_ACTION_CONTROLLER_AVAILABLE_HELPERS') ? null : define('AK_ACTION_CONTROLLER_AVAILABLE_HELPERS', join(',',$available_helpers)); 
     232        require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php'); 
     233        $HelperLoader = new AkHelperLoader(); 
     234        $HelperLoader->setController(&$this); 
     235        $HelperLoader->instantiateHelpers(); 
    260236    } 
    261237 
     
    271247        return array(); 
    272248    } 
    273  
     249         
    274250    function getModuleHelper() 
    275251    { 
     
    283259        return array(); 
    284260    } 
    285  
    286     function getDefaultHelpers() 
    287     { 
    288         if($this->helpers == 'default'){ 
    289             $available_helpers = Ak::dir(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers',array('dirs'=>false)); 
    290             $helper_names = array(); 
    291             foreach ($available_helpers as $available_helper){ 
    292                 $helper_names[$available_helper] = AkInflector::classify(substr($available_helper,0,-10)); 
    293             } 
    294             return $helper_names; 
    295         }else{ 
    296             $this->helpers = Ak::toArray($this->helpers); 
    297         } 
    298         return $this->helpers; 
    299     } 
    300  
    301     function getApplicationHelpers() 
    302     { 
    303         $helper_names = array(); 
    304         if ($this->app_helpers == 'all'){ 
    305             $available_helpers = Ak::dir(AK_HELPERS_DIR,array('dirs'=>false)); 
    306             $helper_names = array(); 
    307             foreach ($available_helpers as $available_helper){ 
    308                 $helper_names[AK_HELPERS_DIR.DS.$available_helper] = AkInflector::classify(substr($available_helper,0,-10)); 
    309             } 
    310  
    311         } elseif (!empty($this->app_helpers)){ 
    312             foreach (Ak::toArray($this->app_helpers) as $helper_name){ 
    313                 $helper_names[AK_HELPERS_DIR.DS.AkInflector::underscore($helper_name).'_helper.php'] = AkInflector::camelize($helper_name); 
    314             } 
    315         } 
    316         return $helper_names; 
    317     } 
    318  
    319     function getPluginHelpers() 
    320     { 
    321         $helper_names = AkActionController::addPluginHelper(false); // Trick for getting helper names set by AkPlugin::addHelper 
    322         if(empty($helper_names)){ 
    323             return array(); 
    324         }elseif ($this->plugin_helpers == 'all'){ 
    325             return $helper_names; 
    326         }else { 
    327             $selected_helper_names = array(); 
    328             foreach (Ak::toArray($this->plugin_helpers) as $helper_name){ 
    329                 $helper_name = AkInflector::camelize($helper_name); 
    330                 if($path = array_shift(array_keys($helper_names, AkInflector::camelize($helper_name)))){ 
    331                     $selected_helper_names[$path] = $helper_names[$path]; 
    332                 } 
    333             } 
    334             return $selected_helper_names; 
    335         } 
    336     } 
    337  
    338     /** 
    339      * Used for adding helpers to the base class like those added by the plugins engine. 
    340      * 
    341      * @param string $helper_name Helper class name like CalendarHelper 
    342      * @param array $options - path: Path to the helper class, defaults to AK_PLUGINS_DIR/helper_name/lib/helper_name.php 
    343      */ 
    344     function addPluginHelper($helper_name, $options = array()) 
    345     { 
    346         static $helpers = array(); 
    347         if($helper_name === false){ 
    348             return $helpers; 
    349         } 
    350         $underscored_helper_name = AkInflector::underscore($helper_name); 
    351         $default_options = array( 
    352         'path' => AK_PLUGINS_DIR.DS.$underscored_helper_name.DS.'lib'.DS.$underscored_helper_name.'.php' 
    353         ); 
    354         $options = array_merge($default_options, $options); 
    355         $helpers[$options['path']] = $helper_name; 
    356     } 
    357  
     261     
     262     
    358263    function _validateGeneratedXhtml() 
    359264    { 
     
    394299        $models = array_unique(array_merge(Ak::import($this->model), Ak::import($this->models), Ak::import($models), (empty($this->app_models)?array(): Ak::import($this->app_models)))); 
    395300 
     301        unset($this->model, $this->models); 
     302                 
    396303        foreach ($models as $model){ 
    397304            $this->instantiateModelClass($model, (empty($this->finder_options[$model])?array():$this->finder_options[$model])); 
  • trunk/lib/AkPlugin.php

    r398 r1140  
    8686    function addHelper($helper_name, $helper_path = null) 
    8787    { 
     88        require_once(AK_LIB_DIR.DS.'AkActionView'.DS.'AkHelperLoader.php'); 
    8889        $helper_name = AkInflector::camelize($helper_name); 
    8990        $helper_path = empty($helper_path) ? $this->getPath().DS.'lib'.DS.AkInflector::underscore($helper_name).'.php' : $helper_path; 
    90         AkActionController::addPluginHelper($helper_name, array('path' => $helper_path)); 
     91        AkHelperLoader::addPluginHelper($helper_name, array('path' => $helper_path)); 
    9192    } 
    9293