PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/action_pack/helper_loader.php

https://github.com/bermi/akelos
PHP | 281 lines | 198 code | 35 blank | 48 comment | 36 complexity | 6cfce1a8e88fff0819f721b0c4f8e565 MD5 | raw file
  1. <?php
  2. # This file is part of the Akelos Framework
  3. # (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
  4. # See LICENSE and CREDITS for details
  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
  13. {
  14. public $_Controller;
  15. public $_HelperInstances = array();
  16. public $_Handler;
  17. public function __construct() {
  18. $this->_Handler = new stdClass();
  19. }
  20. public function setController(&$ControllerInstance) {
  21. $this->_Controller = $ControllerInstance;
  22. $this->setHandler($this->_Controller);
  23. }
  24. /**
  25. * $HandlerInstance is the object where all the helpers will be instantiated as attributes.
  26. *
  27. * Like setController but for Mailers and Testing
  28. */
  29. public function setHandler(&$HandlerInstance) {
  30. $this->_Handler = $HandlerInstance;
  31. }
  32. /**
  33. * Creates an instance of each available helper and links it into into current handler.
  34. *
  35. * For example, if a helper TextHelper is located into the file text_helper.php.
  36. * An instance is created on current controller
  37. * at $this->text_helper. This instance is also available on the view by calling $text_helper.
  38. *
  39. * Helpers can be found at lib/AkActionView/helpers (this might change in a future)
  40. *
  41. * Retuns an array with helper_name => HerlperInstace
  42. */
  43. public function &instantiateHelpers() {
  44. $this->instantiateHelpersAsHandlerAttributes($this->getHelperNames());
  45. $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances));
  46. return $this->_HelperInstances;
  47. }
  48. public function instantiateHelpersAsHandlerAttributes($helpers = array()) {
  49. $helpers_dir = AkConfig::getDir('helpers');
  50. foreach ($helpers as $file=>$helper){
  51. if (empty($helper)) continue;
  52. $helper_class_name = AkInflector::camelize(AkInflector::demodulize(strstr($helper, 'Helper') ? $helper : $helper.'Helper'));
  53. $helper_file_name = AkInflector::underscore($helper_class_name);
  54. if(is_int($file)){
  55. $file = $helpers_dir.DS.$helper_file_name.'.php';
  56. }
  57. $full_path = preg_match('/[\\\\\/]+/',$file);
  58. $file_path = $full_path ? $file : AK_ACTION_PACK_DIR.DS.'helpers'.DS.$file;
  59. if(is_file($file_path)){
  60. include_once($file_path);
  61. }
  62. if(class_exists($helper_class_name)){
  63. $attribute_name = $full_path ? $helper_file_name : substr($file,0,-4);
  64. $this->_Handler->$attribute_name = new $helper_class_name($this->_Handler);
  65. if(method_exists($this->_Handler->$attribute_name,'setController')){
  66. $this->_Handler->$attribute_name->setController($this->_Handler);
  67. }elseif(method_exists($this->_Handler->$attribute_name,'setMailer')){
  68. $this->_Handler->$attribute_name->setMailer($this->_Handler);
  69. }
  70. if(method_exists($this->_Handler->$attribute_name,'init')){
  71. $this->_Handler->$attribute_name->init();
  72. }
  73. $this->_HelperInstances[$attribute_name] = $this->_Handler->$attribute_name;
  74. }
  75. }
  76. }
  77. /**
  78. * Creates an instance of each available helper and links it into into current mailer.
  79. *
  80. * Mailer helpers work as Controller helpers but without the Request context
  81. */
  82. public function getHelpersForMailer() {
  83. $helper_names = $this->getHelperNames();
  84. $this->instantiateHelpersAsHandlerAttributes($helper_names);
  85. $this->_storeInstantiatedHelperNames(array_keys($this->_HelperInstances));
  86. return $this->_HelperInstances;
  87. }
  88. /**
  89. * In order to help rendering engines to know which helpers are available
  90. * we need to persit them as a static var.
  91. */
  92. public function _storeInstantiatedHelperNames($helpers) {
  93. Ak::setStaticVar('AkActionView::instantiated_helper_names', $helpers);
  94. }
  95. /**
  96. * Returns an array of helper names like:
  97. *
  98. * array('url_helper', 'prototype_helper')
  99. */
  100. static function getInstantiatedHelperNames() {
  101. return Ak::getStaticVar('AkActionView::instantiated_helper_names');
  102. }
  103. public function getHelperNames() {
  104. //$helpers = $this->getDefaultHandlerHelperNames();
  105. $helpers = array_merge($this->getDefaultHandlerHelperNames(), $this->getApplicationHelperNames(), $this->getPluginHelperNames());
  106. //$helpers = array_merge($helpers, $this->getPluginHelperNames());
  107. if(!empty($this->_Controller) && ($this->_Controller instanceof AkActionController)){
  108. $helpers = array_merge($helpers, $this->_Controller->getModuleHelper(), $this->_Controller->getCurrentControllerHelper());
  109. }
  110. return $helpers;
  111. }
  112. public function getDefaultHandlerHelperNames() {
  113. $handler = $this->_Handler;
  114. $handler->helpers = !isset($handler->helpers) ? 'default' : $handler->helpers;
  115. if($handler->helpers == 'default'){
  116. $available_helpers = AkFileSystem::dir(AK_ACTION_PACK_DIR.DS.'helpers',array('dirs'=>false));
  117. $helper_names = array();
  118. foreach ($available_helpers as $available_helper){
  119. $helper_names[$available_helper] = AkInflector::classify(substr($available_helper,0,-10));
  120. }
  121. return $helper_names;
  122. }else{
  123. $handler->helpers = Ak::toArray($handler->helpers);
  124. }
  125. return $handler->helpers;
  126. }
  127. public function getApplicationHelperNames() {
  128. $handler = $this->_Handler;
  129. $handler->app_helpers = !isset($handler->app_helpers) ? 'all' : $handler->app_helpers;
  130. $helpers_dir = AkConfig::getDir('helpers');
  131. $helper_names = array();
  132. if ($handler->app_helpers == 'all'){
  133. $available_helpers = AkFileSystem::dir($helpers_dir, array('dirs'=>false));
  134. $helper_names = array();
  135. foreach ($available_helpers as $available_helper){
  136. $helper_names[$helpers_dir.DS.$available_helper] = AkInflector::classify(substr($available_helper,0,-10));
  137. }
  138. } elseif (!empty($handler->app_helpers)){
  139. foreach (Ak::toArray($handler->app_helpers) as $helper_name){
  140. $helper_names[$helpers_dir.DS.AkInflector::underscore($helper_name).'_helper.php'] = AkInflector::camelize($helper_name);
  141. }
  142. }
  143. return $helper_names;
  144. }
  145. public function getPluginHelperNames() {
  146. $handler = $this->_Handler;
  147. $handler->plugin_helpers = !isset($handler->plugin_helpers) ? 'all' : $handler->plugin_helpers;
  148. $helper_names = AkHelperLoader::addPluginHelper(false); // Trick for getting helper names set by AkPlugin::addHelper
  149. if(empty($helper_names)){
  150. return array();
  151. }elseif ($handler->plugin_helpers == 'all'){
  152. return $helper_names;
  153. }else {
  154. $selected_helper_names = array();
  155. foreach (Ak::toArray($handler->plugin_helpers) as $helper_name){
  156. $helper_name = AkInflector::camelize($helper_name);
  157. if($path = Ak::first(array_keys($helper_names, AkInflector::camelize($helper_name)))){
  158. $selected_helper_names[$path] = $helper_names[$path];
  159. }
  160. }
  161. return $selected_helper_names;
  162. }
  163. }
  164. /**
  165. * Used for adding helpers to the base class like those added by the plugins engine.
  166. *
  167. * @param string $helper_name Helper class name like CalendarHelper
  168. * @param array $options - path: Path to the helper class, defaults to AK_PLUGINS_DIR/helper_name/lib/helper_name.php
  169. */
  170. public function addPluginHelper($helper_name, $options = array()) {
  171. static $helpers = array();
  172. if($helper_name === false){
  173. return $helpers;
  174. }
  175. $underscored_helper_name = AkInflector::underscore($helper_name);
  176. $default_options = array(
  177. 'path' => AkConfig::getDir('plugins').DS.$underscored_helper_name.DS.'lib'.DS.$underscored_helper_name.'.php'
  178. );
  179. $options = array_merge($default_options, $options);
  180. $helpers[$options['path']] = $helper_name;
  181. }
  182. public function getHelperFileName($file_name){
  183. if(is_file($file_name)){
  184. return $file_name;
  185. }
  186. $file_name = AkInflector::underscore($file_name);
  187. if(!strstr($file_name, '.php')){
  188. $file_name = $file_name.'.php';
  189. }
  190. foreach ($this->getHeperBasePaths() as $base_path){
  191. if(!strstr($file_name, $base_path)){
  192. if(AK_ACTION_PACK_DIR.DS.'helpers' == $base_path && substr($file_name, 0 , 3) != 'ak_'){
  193. if(is_file($base_path.DS.'ak_'.$file_name)){
  194. return $base_path.DS.'ak_'.$file_name;
  195. }
  196. }elseif(is_file($base_path.DS.$file_name)){
  197. return $base_path.DS.$file_name;
  198. }
  199. }
  200. }
  201. return false;
  202. }
  203. public function getHeperBasePaths(){
  204. $paths = array(
  205. AkConfig::getDir('helpers'),
  206. AK_ACTION_PACK_DIR.DS.'helpers'
  207. );
  208. if(!empty($this->_Controller) && ($this->_Controller instanceof AkActionController)){
  209. $module_name = $this->_Controller->getModuleName();
  210. if(!empty($module_name)){
  211. $paths[] = AkConfig::getDir('helpers').DS.AkInflector::underscore($module_name);
  212. }
  213. }
  214. return $paths;
  215. }
  216. public function instantiateHelperAsHandlerAttribute($helper, $file){
  217. $helper_class_name = AkInflector::camelize(strstr($helper, 'Helper') ? $helper : $helper.'Helper');
  218. if(!$file_path = $this->getHelperFileName($file)){
  219. return false;
  220. }
  221. include_once($file_path);
  222. if(strstr($file_path, AK_ACTION_PACK_DIR.DS.'helpers') && substr($helper_class_name, 0 , 2) != 'Ak'){
  223. $helper_class_name = 'Ak'.$helper_class_name;
  224. $using_core_helper_alias = true;
  225. }
  226. if(class_exists($helper_class_name)){
  227. $attribute_name = Ak::last(explode(DS, substr($file_path,0,-4)));
  228. $this->_Handler->$attribute_name = new $helper_class_name($this->_Handler);
  229. if(isset($using_core_helper_alias)){
  230. $attribute_name_alias = substr($attribute_name,3);
  231. if(!isset($this->_Handler->$attribute_name_alias)){
  232. $this->_Handler->$attribute_name_alias = $this->_Handler->$attribute_name;
  233. }
  234. }
  235. if(method_exists($this->_Handler->$attribute_name,'setController')){
  236. $this->_Handler->$attribute_name->setController($this->_Handler);
  237. }elseif(method_exists($this->_Handler->$attribute_name,'setMailer')){
  238. $this->_Handler->$attribute_name->setMailer($this->_Handler);
  239. }
  240. if(method_exists($this->_Handler->$attribute_name,'init')){
  241. $this->_Handler->$attribute_name->init();
  242. }
  243. $this->_HelperInstances[$attribute_name] = $this->_Handler->$attribute_name;
  244. }
  245. }
  246. }