ComponentCollection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Components collection is used as a registry for loaded components and handles loading
  4. * and constructing component 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.libs.controller
  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. App::uses('Component', 'Controller');
  20. class ComponentCollection extends ObjectCollection {
  21. /**
  22. * The controller that this collection was initialized with.
  23. *
  24. * @var Controller
  25. */
  26. protected $_Controller = null;
  27. /**
  28. * Initializes all the Components for a controller.
  29. * Attaches a reference of each component to the Controller.
  30. *
  31. * @param Controller $Controller Controller to initialize components for.
  32. * @return void
  33. */
  34. public function init(Controller $Controller) {
  35. if (empty($Controller->components)) {
  36. return;
  37. }
  38. $this->_Controller = $Controller;
  39. $components = ComponentCollection::normalizeObjectArray($Controller->components);
  40. foreach ($components as $name => $properties) {
  41. $Controller->{$name} = $this->load($properties['class'], $properties['settings']);
  42. }
  43. }
  44. /**
  45. * Get the controller associated with the collection.
  46. *
  47. * @return Controller.
  48. */
  49. public function getController() {
  50. return $this->_Controller;
  51. }
  52. /**
  53. * Loads/constructs a component. Will return the instance in the registry if it already exists.
  54. * You can use `$settings['enabled'] = false` to disable callbacks on a component when loading it.
  55. * Callbacks default to on. Disabled component methods work as normal, only callbacks are disabled.
  56. *
  57. * You can alias your component as an existing component by setting the 'className' key, i.e.,
  58. * {{{
  59. * public $components = array(
  60. * 'Email' => array(
  61. * 'className' => 'AliasedEmail'
  62. * );
  63. * );
  64. * }}}
  65. * All calls to the `Email` component would use `AliasedEmail` instead.
  66. *
  67. * @param string $component Component name to load
  68. * @param array $settings Settings for the component.
  69. * @return Component A component object, Either the existing loaded component or a new one.
  70. * @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
  71. */
  72. public function load($component, $settings = array()) {
  73. if (is_array($settings) && isset($settings['className'])) {
  74. $alias = $component;
  75. $component = $settings['className'];
  76. }
  77. list($plugin, $name) = pluginSplit($component, true);
  78. if (!isset($alias)) {
  79. $alias = $name;
  80. }
  81. if (isset($this->_loaded[$alias])) {
  82. return $this->_loaded[$alias];
  83. }
  84. $componentClass = $name . 'Component';
  85. App::uses($componentClass, $plugin . 'Controller/Component');
  86. if (!class_exists($componentClass)) {
  87. throw new MissingComponentClassException(array(
  88. 'file' => Inflector::underscore($componentClass) . '.php',
  89. 'class' => $componentClass
  90. ));
  91. }
  92. $this->_loaded[$alias] = new $componentClass($this, $settings);
  93. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  94. if ($enable === true) {
  95. $this->_enabled[] = $alias;
  96. }
  97. return $this->_loaded[$alias];
  98. }
  99. }