HelperCollection.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * @return void
  31. */
  32. public function __construct(View $view) {
  33. $this->_View = $view;
  34. }
  35. /**
  36. * Loads/constructs a helper. Will return the instance in the registry if it already exists.
  37. * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
  38. * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
  39. * declaring $helpers arrays you can disable callbacks on helpers.
  40. *
  41. * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
  42. * {{{
  43. * public $helpers = array(
  44. * 'Html' => array(
  45. * 'className' => 'AliasedHtml'
  46. * );
  47. * );
  48. * }}}
  49. * All calls to the `Html` helper would use `AliasedHtml` instead.
  50. *
  51. * @param string $helper Helper name to load
  52. * @param array $settings Settings for the helper.
  53. * @return Helper A helper object, Either the existing loaded helper or a new one.
  54. * @throws MissingHelperFileException, MissingHelperClassException when the helper could not be found
  55. */
  56. public function load($helper, $settings = array()) {
  57. if (is_array($settings) && isset($settings['className'])) {
  58. $alias = $helper;
  59. $helper = $settings['className'];
  60. }
  61. list($plugin, $name) = pluginSplit($helper, true);
  62. if (!isset($alias)) {
  63. $alias = $name;
  64. }
  65. if (isset($this->_loaded[$alias])) {
  66. return $this->_loaded[$alias];
  67. }
  68. $helperClass = $name . 'Helper';
  69. App::uses($helperClass, $plugin . 'View/Helper');
  70. if (!class_exists($helperClass)) {
  71. throw new MissingHelperClassException(array(
  72. 'class' => $helperClass,
  73. 'file' => $helperClass . '.php'
  74. ));
  75. }
  76. $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
  77. $vars = array('request', 'theme', 'plugin');
  78. foreach ($vars as $var) {
  79. $this->_loaded[$alias]->{$var} = $this->_View->{$var};
  80. }
  81. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  82. if ($enable === true) {
  83. $this->_enabled[] = $alias;
  84. }
  85. return $this->_loaded[$alias];
  86. }
  87. }