| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 4 5 /** 6 * Helpers are normally loaded in the context of a controller call, but some 7 * times they might be useful in Mailers, Comand line tools or for unit testing 8 * 9 * Some helpers might require information available only on a conroller context 10 * such as current URL, Request and Response information among others. 11 */ 12 class AkHelperLoader extends AkObject 13 { 14 var $_Controller; 15 var $_HelperInstances; 16 var $_Handler; 17 18 function __construct() 19 { 20 $this->_Handler = new stdClass(); 21 } 22 23 function setController(&$ControllerInstance) 24 { 25 $this->_Controller =& $ControllerInstance; 26 $this->setHandler($this->_Controller); 27 } 28 29 /** 30 * $HandlerInstance is the object where all the helpers will be instantiated as attributes. 31 * 32 * Like setController but for Mailers and Testing 33 */ 34 function setHandler(&$HandlerInstance) 35 { 36 $this->_Handler =& $HandlerInstance; 37 } 38 39 /** 40 * Creates an instance of each available helper and links it into into current handler. 41 * 42 * For example, if a helper TextHelper is located into the file text_helper.php. 43 * An instance is created on current controller 44 * at $this->text_helper. This instance is also available on the view by calling $text_helper. 45 * 46 * Helpers can be found at lib/AkActionView/helpers (this might change in a future) 47 * 48 * Retuns an array with helper_name => HerlperInstace 49 */ 50 function &instantiateHelpers() 51 { 52 $this->instantiateHelpersAsHandlerAttributes($this->getHelperNames()); 53 $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances)); 54 return $this->_HelperInstances; 55 } 56 57 function instantiateHelpersAsHandlerAttributes($helpers = array()) 58 { 59 foreach ($helpers as $file=>$helper){ 60 $helper_class_name = AkInflector::camelize(AkInflector::demodulize(strstr($helper, 'Helper') ? $helper : $helper.'Helper')); 61 $helper_file_name = AkInflector::underscore($helper_class_name); 62 if(is_int($file)){ 63 $file = AK_HELPERS_DIR.DS.$helper_file_name.'.php'; 64 } 65 66 $full_path = preg_match('/[\\\\\/]+/',$file); 67 $file_path = $full_path ? $file : AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.$file; 68 if(is_file($file_path)){ 69 include_once($file_path); 70 } 71 72 if(class_exists($helper_class_name)){ 73 $attribute_name = $full_path ? $helper_file_name : substr($file,0,-4); 74 $this->_Handler->$attribute_name =& new $helper_class_name(&$this->_Handler); 75 if(method_exists($this->_Handler->$attribute_name,'setController')){ 76 $this->_Handler->$attribute_name->setController(&$this->_Handler); 77 }elseif(method_exists($this->_Handler->$attribute_name,'setMailer')){ 78 $this->_Handler->$attribute_name->setMailer(&$this->_Handler); 79 } 80 if(method_exists($this->_Handler->$attribute_name,'init')){ 81 $this->_Handler->$attribute_name->init(); 82 } 83 $this->_HelperInstances[$attribute_name] =& $this->_Handler->$attribute_name; 84 } 85 } 86 } 87 88 /** 89 * Creates an instance of each available helper and links it into into current mailer. 90 * 91 * Mailer helpers work as Controller helpers but without the Request context 92 */ 93 function getHelpersForMailer() 94 { 95 $helper_names = $this->getHelperNames(); 96 $this->instantiateHelpersAsHandlerAttributes($helper_names); 97 $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances)); 98 return $this->_HelperInstances; 99 } 100 101 /** 102 * In order to help rendering engines to know which helpers are available 103 * we need to persit them as a static var. 104 */ 105 function _storeInstantiatedHelperNames($helpers) 106 { 107 Ak::setStaticVar('AkActionView::instantiated_helper_names', $helpers); 108 } 109 110 /** 111 * Returns an array of helper names like: 112 * 113 * array('url_helper', 'prototype_helper') 114 */ 115 function getInstantiatedHelperNames() 116 { 117 return Ak::getStaticVar('AkActionView::instantiated_helper_names'); 118 } 119 120 121 function getHelperNames() 122 { 123 //$helpers = $this->getDefaultHandlerHelperNames(); 124 $helpers = array_merge($this->getDefaultHandlerHelperNames(), $this->getApplicationHelperNames(), $this->getPluginHelperNames()); 125 //$helpers = array_merge($helpers, $this->getPluginHelperNames()); 126 127 if(!empty($this->_Controller)){ 128 $helpers = array_merge($helpers, $this->_Controller->getModuleHelper(), $this->_Controller->getCurrentControllerHelper()); 129 //$helpers = array_merge($helpers, $this->_Controller->getCurrentControllerHelper()); 130 } 131 132 return $helpers; 133 } 134 135 136 function getDefaultHandlerHelperNames() 137 { 138 $handler =& $this->_Handler; 139 $handler->helpers = !isset($handler->helpers) ? 'default' : $handler->helpers; 140 141 if($handler->helpers == 'default'){ 142 $available_helpers = Ak::dir(AK_LIB_DIR.DS.'AkActionView'.DS.'helpers',array('dirs'=>false)); 143 $helper_names = array(); 144 foreach ($available_helpers as $available_helper){ 145 $helper_names[$available_helper] = AkInflector::classify(substr($available_helper,0,-10)); 146 } 147 return $helper_names; 148 }else{ 149 $handler->helpers = Ak::toArray($handler->helpers); 150 } 151 152 return $handler->helpers; 153 } 154 155 function getApplicationHelperNames() 156 { 157 $handler =& $this->_Handler; 158 $handler->app_helpers = !isset($handler->app_helpers) ? null : $handler->app_helpers; 159 160 $helper_names = array(); 161 if ($handler->app_helpers == 'all'){ 162 $available_helpers = Ak::dir(AK_HELPERS_DIR,array('dirs'=>false)); 163 $helper_names = array(); 164 foreach ($available_helpers as $available_helper){ 165 $helper_names[AK_HELPERS_DIR.DS.$available_helper] = AkInflector::classify(substr($available_helper,0,-10)); 166 } 167 168 } elseif (!empty($handler->app_helpers)){ 169 foreach (Ak::toArray($handler->app_helpers) as $helper_name){ 170 $helper_names[AK_HELPERS_DIR.DS.AkInflector::underscore($helper_name).'_helper.php'] = AkInflector::camelize($helper_name); 171 } 172 } 173 return $helper_names; 174 } 175 176 function getPluginHelperNames() 177 { 178 $handler =& $this->_Handler; 179 $handler->plugin_helpers = !isset($handler->plugin_helpers) ? 'all' : $handler->plugin_helpers; 180 181 $helper_names = AkHelperLoader::addPluginHelper(false); // Trick for getting helper names set by AkPlugin::addHelper 182 if(empty($helper_names)){ 183 return array(); 184 }elseif ($handler->plugin_helpers == 'all'){ 185 return $helper_names; 186 }else { 187 $selected_helper_names = array(); 188 foreach (Ak::toArray($handler->plugin_helpers) as $helper_name){ 189 $helper_name = AkInflector::camelize($helper_name); 190 if($path = array_shift(array_keys($helper_names, AkInflector::camelize($helper_name)))){ 191 $selected_helper_names[$path] = $helper_names[$path]; 192 } 193 } 194 return $selected_helper_names; 195 } 196 } 197 198 /** 199 * Used for adding helpers to the base class like those added by the plugins engine. 200 * 201 * @param string $helper_name Helper class name like CalendarHelper 202 * @param array $options - path: Path to the helper class, defaults to AK_PLUGINS_DIR/helper_name/lib/helper_name.php 203 */ 204 function addPluginHelper($helper_name, $options = array()) 205 { 206 static $helpers = array(); 207 if($helper_name === false){ 208 return $helpers; 209 } 210 $underscored_helper_name = AkInflector::underscore($helper_name); 211 $default_options = array( 212 'path' => AK_PLUGINS_DIR.DS.$underscored_helper_name.DS.'lib'.DS.$underscored_helper_name.'.php' 213 ); 214 $options = array_merge($default_options, $options); 215 $helpers[$options['path']] = $helper_name; 216 } 217 } 218 219 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |