HelperCollection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package cake
  15. * @subpackage cake.cake.libs.view
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ObjectCollection', 'Core');
  20. class HelperCollection extends ObjectCollection {
  21. /**
  22. * View object to use when making helpers.
  23. *
  24. * @var View
  25. */
  26. protected $_View;
  27. /**
  28. * Constructor
  29. *
  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. * @param string $helper Helper name to load
  42. * @param array $settings Settings for the helper.
  43. * @return Helper A helper object, Either the existing loaded helper or a new one.
  44. * @throws MissingHelperFileException, MissingHelperClassException when the helper could not be found
  45. */
  46. public function load($helper, $settings = array()) {
  47. list($plugin, $name) = pluginSplit($helper, true);
  48. if (isset($this->_loaded[$name])) {
  49. return $this->_loaded[$name];
  50. }
  51. $helperClass = $name . 'Helper';
  52. if (!class_exists($helperClass)) {
  53. if (!App::import('Helper', $helper)) {
  54. throw new MissingHelperFileException(array(
  55. 'class' => $helperClass,
  56. 'file' => Inflector::underscore($name) . '.php'
  57. ));
  58. }
  59. if (!class_exists($helperClass)) {
  60. throw new MissingHelperClassException(array(
  61. 'class' => $helperClass,
  62. 'file' => Inflector::underscore($name) . '.php'
  63. ));
  64. }
  65. }
  66. $this->_loaded[$name] = new $helperClass($this->_View, $settings);
  67. $vars = array('request', 'theme', 'plugin');
  68. foreach ($vars as $var) {
  69. $this->_loaded[$name]->{$var} = $this->_View->{$var};
  70. }
  71. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  72. if ($enable === true) {
  73. $this->_enabled[] = $name;
  74. }
  75. return $this->_loaded[$name];
  76. }
  77. }