HelperCollection.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Helpers collection is used as a registry for loaded helpers and handles loading
  4. * and constructing helper class objects.
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.View
  15. * @since CakePHP(tm) v 2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('ObjectCollection', 'Utility');
  19. class HelperCollection extends ObjectCollection {
  20. /**
  21. * View object to use when making helpers.
  22. *
  23. * @var View
  24. */
  25. protected $_View;
  26. /**
  27. * Constructor
  28. *
  29. * @param View $view
  30. */
  31. public function __construct(View $view) {
  32. $this->_View = $view;
  33. }
  34. /**
  35. * Loads/constructs a helper. Will return the instance in the registry if it already exists.
  36. * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
  37. * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
  38. * declaring $helpers arrays you can disable callbacks on helpers.
  39. *
  40. * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
  41. * {{{
  42. * public $helpers = array(
  43. * 'Html' => array(
  44. * 'className' => 'AliasedHtml'
  45. * );
  46. * );
  47. * }}}
  48. * All calls to the `Html` helper would use `AliasedHtml` instead.
  49. *
  50. * @param string $helper Helper name to load
  51. * @param array $settings Settings for the helper.
  52. * @return Helper A helper object, Either the existing loaded helper or a new one.
  53. * @throws MissingHelperException when the helper could not be found
  54. */
  55. public function load($helper, $settings = array()) {
  56. if (is_array($settings) && isset($settings['className'])) {
  57. $alias = $helper;
  58. $helper = $settings['className'];
  59. }
  60. list($plugin, $name) = pluginSplit($helper, true);
  61. if (!isset($alias)) {
  62. $alias = $name;
  63. }
  64. if (isset($this->_loaded[$alias])) {
  65. return $this->_loaded[$alias];
  66. }
  67. $helperClass = $name . 'Helper';
  68. App::uses($helperClass, $plugin . 'View/Helper');
  69. if (!class_exists($helperClass)) {
  70. throw new MissingHelperException(array(
  71. 'class' => $helperClass,
  72. 'plugin' => substr($plugin, 0, -1)
  73. ));
  74. }
  75. $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
  76. $vars = array('request', 'theme', 'plugin');
  77. foreach ($vars as $var) {
  78. $this->_loaded[$alias]->{$var} = $this->_View->{$var};
  79. }
  80. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  81. if ($enable === true) {
  82. $this->_enabled[] = $alias;
  83. }
  84. return $this->_loaded[$alias];
  85. }
  86. }