/standard/tags/release-1.0.0RC1/library/Zend/Controller/Action/HelperBroker.php

https://github.com/bhaumik25/zend-framework · PHP · 336 lines · 150 code · 43 blank · 143 comment · 13 complexity · 373633a2d324a0672a7fea19e86064af MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Controller_Action_Exception */
  21. require_once 'Zend/Controller/Action/Exception.php';
  22. /** Zend_Controller_Action_Helper_Abstract */
  23. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  24. /** Zend_Loader **/
  25. require_once 'Zend/Loader.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Controller
  29. * @subpackage Zend_Controller_Action
  30. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Controller_Action_HelperBroker
  34. {
  35. /**
  36. * $_helpers - Helper array
  37. *
  38. * @var Zend_Controller_Action_Helper_Abstract[]
  39. */
  40. static protected $_helpers = array();
  41. /**
  42. * $_paths - paths to Action_Helpers
  43. *
  44. * @var array
  45. */
  46. static protected $_paths = array(array(
  47. 'dir' => 'Zend/Controller/Action/Helper/',
  48. 'prefix' => 'Zend_Controller_Action_Helper_'
  49. ));
  50. /**
  51. * $_actionController - ActionController reference
  52. *
  53. * @var Zend_Controller_Action
  54. */
  55. protected $_actionController;
  56. /**
  57. * addHelper() - Add helper objects
  58. *
  59. * @param Zend_Controller_Action_Helper_Abstract $helper
  60. * @return void
  61. */
  62. static public function addHelper(Zend_Controller_Action_Helper_Abstract $helper)
  63. {
  64. $helper_name = $helper->getName();
  65. self::$_helpers[$helper_name] = $helper;
  66. return;
  67. }
  68. /**
  69. * addPrefix() - Add repository of helpers by prefix
  70. *
  71. * @param string $prefix
  72. */
  73. static public function addPrefix($prefix)
  74. {
  75. $prefix = rtrim($prefix, '_');
  76. $path = str_replace('_', DIRECTORY_SEPARATOR, $prefix);
  77. self::addPath($path, $prefix);
  78. return;
  79. }
  80. /**
  81. * resetHelpers()
  82. *
  83. * @return void
  84. */
  85. static public function resetHelpers()
  86. {
  87. self::$_helpers = array();
  88. return;
  89. }
  90. /**
  91. * addPath() - Add path to repositories where Action_Helpers could be found.
  92. *
  93. * @param string $path
  94. * @param string $prefix Optional; defaults to 'Zend_Controller_Action_Helper'
  95. * @return void
  96. */
  97. static public function addPath($path, $prefix = 'Zend_Controller_Action_Helper')
  98. {
  99. // make sure it ends in a PATH_SEPARATOR
  100. if (substr($path, -1, 1) != DIRECTORY_SEPARATOR) {
  101. $path .= DIRECTORY_SEPARATOR;
  102. }
  103. // make sure it ends in a PATH_SEPARATOR
  104. $prefix = rtrim($prefix, '_') . '_';
  105. $info['dir'] = $path;
  106. $info['prefix'] = $prefix;
  107. self::$_paths[] = $info;
  108. return;
  109. }
  110. /**
  111. * __construct() -
  112. *
  113. * @param Zend_Controller_Action $actionController
  114. * @return void
  115. */
  116. public function __construct(Zend_Controller_Action $actionController)
  117. {
  118. $this->_actionController = $actionController;
  119. foreach (self::$_helpers as $helper) {
  120. $helper->setActionController($actionController);
  121. $helper->init();
  122. }
  123. }
  124. /**
  125. * notifyPreDispatch() - called by action controller dispatch method
  126. *
  127. * @return void
  128. */
  129. public function notifyPreDispatch()
  130. {
  131. foreach (self::$_helpers as $helper) {
  132. $helper->preDispatch();
  133. }
  134. }
  135. /**
  136. * notifyPostDispatch() - called by action controller dispatch method
  137. *
  138. * @return void
  139. */
  140. public function notifyPostDispatch()
  141. {
  142. foreach (self::$_helpers as $helper) {
  143. $helper->postDispatch();
  144. }
  145. }
  146. /**
  147. * Normalize helper name for lookups
  148. *
  149. * @param string $name
  150. * @return string
  151. */
  152. protected static function _normalizeHelperName($name)
  153. {
  154. if (strpos($name, '_') !== false) {
  155. $name = str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
  156. }
  157. return ucfirst($name);
  158. }
  159. /**
  160. * getHelper() - get helper by name
  161. *
  162. * @param string $name
  163. * @return Zend_Controller_Action_Helper_Abstract
  164. */
  165. public function getHelper($name)
  166. {
  167. $name = self::_normalizeHelperName($name);
  168. if (!array_key_exists($name, self::$_helpers)) {
  169. self::_loadHelper($name);
  170. }
  171. $helper = self::$_helpers[$name];
  172. $initialize = false;
  173. if (null === ($actionController = $helper->getActionController())) {
  174. $initialize = true;
  175. } elseif ($actionController !== $this->_actionController) {
  176. $initialize = true;
  177. }
  178. if ($initialize) {
  179. $helper->setActionController($this->_actionController)
  180. ->init();
  181. }
  182. return $helper;
  183. }
  184. /**
  185. * getExistingHelper() - get helper by name
  186. *
  187. * Static method to retrieve helper object. Only retrieves helpers already
  188. * initialized with the broker (either via addHelper() or on-demand loading
  189. * via getHelper()).
  190. *
  191. * Throws an exception if the referenced helper does not exist in the
  192. * stack; use {@link hasHelper()} to check if the helper is registered
  193. * prior to retrieving it.
  194. *
  195. * @param string $name
  196. * @return Zend_Controller_Action_Helper_Abstract
  197. * @throws Zend_Controller_Action_Exception
  198. */
  199. public static function getExistingHelper($name)
  200. {
  201. $name = self::_normalizeHelperName($name);
  202. if (array_key_exists($name, self::$_helpers)) {
  203. return self::$_helpers[$name];
  204. }
  205. throw new Zend_Controller_Action_Exception('Action helper "' . $name . '" has not been registered with the helper broker');
  206. }
  207. /**
  208. * Is a particular helper loaded in the broker?
  209. *
  210. * @param string $name
  211. * @return boolean
  212. */
  213. public static function hasHelper($name)
  214. {
  215. $name = self::_normalizeHelperName($name);
  216. return array_key_exists($name, self::$_helpers);
  217. }
  218. /**
  219. * Remove a particular helper from the broker
  220. *
  221. * @param string $name
  222. * @return boolean
  223. */
  224. public static function removeHelper($name)
  225. {
  226. $name = self::_normalizeHelperName($name);
  227. if (array_key_exists($name, self::$_helpers)) {
  228. unset(self::$_helpers[$name]);
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * _loadHelper()
  235. *
  236. * @param string $name
  237. * @return void
  238. */
  239. protected static function _loadHelper($name)
  240. {
  241. $file = $name . '.php';
  242. foreach (self::$_paths as $info) {
  243. $dir = $info['dir'];
  244. $prefix = $info['prefix'];
  245. $class = $prefix . $name;
  246. if (class_exists($class, false)) {
  247. $helper = new $class();
  248. if (!$helper instanceof Zend_Controller_Action_Helper_Abstract) {
  249. throw new Zend_Controller_Action_Exception('Helper name ' . $name . ' -> class ' . $class . ' is not of type Zend_Controller_Action_Helper_Abstract');
  250. }
  251. self::$_helpers[$helper->getName()] = $helper;
  252. return;
  253. } elseif (Zend_Loader::isReadable($dir . $file)) {
  254. include_once $dir . $file;
  255. if (class_exists($class, false)) {
  256. $helper = new $class();
  257. if (!$helper instanceof Zend_Controller_Action_Helper_Abstract) {
  258. throw new Zend_Controller_Action_Exception('Helper name ' . $name . ' -> class ' . $class . ' is not of type Zend_Controller_Action_Helper_Abstract');
  259. }
  260. self::$_helpers[$helper->getName()] = $helper;
  261. return;
  262. }
  263. }
  264. }
  265. throw new Zend_Controller_Action_Exception('Action Helper by name ' . $name . ' not found.');
  266. }
  267. /**
  268. * __call()
  269. *
  270. * @param string $method
  271. * @param array $args
  272. * @return mixed
  273. * @throws Zend_Controller_Action_Exception if helper does not have a direct() method
  274. */
  275. public function __call($method, $args)
  276. {
  277. $helper = $this->getHelper($method);
  278. if (method_exists($helper, 'direct')) {
  279. return call_user_func_array(array($helper, 'direct'), $args);
  280. }
  281. throw new Zend_Controller_Action_Exception('Helper "' . $method . '" does not support overloading via direct()');
  282. }
  283. /**
  284. * __get()
  285. *
  286. * @param string $name
  287. * @return Zend_Controller_Action_Helper_Abstract
  288. */
  289. public function __get($name)
  290. {
  291. return $this->getHelper($name);
  292. }
  293. }