ComponentCollection.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  15. * @subpackage cake.libs.controller
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ObjectCollection', 'Utility');
  20. App::uses('Component', 'Controller');
  21. class ComponentCollection extends ObjectCollection {
  22. /**
  23. * The controller that this collection was initialized with.
  24. *
  25. * @var Controller
  26. */
  27. protected $_Controller = null;
  28. /**
  29. * Initializes all the Components for a controller.
  30. * Attaches a reference of each component to the Controller.
  31. *
  32. * @param Controller $controller Controller to initialize components for.
  33. * @return void
  34. */
  35. public function init(Controller $Controller) {
  36. if (empty($Controller->components)) {
  37. return;
  38. }
  39. $this->_Controller = $Controller;
  40. $components = ComponentCollection::normalizeObjectArray($Controller->components);
  41. foreach ($components as $name => $properties) {
  42. $Controller->{$name} = $this->load($properties['class'], $properties['settings']);
  43. }
  44. }
  45. /**
  46. * Get the controller associated with the collection.
  47. *
  48. * @return Controller.
  49. */
  50. public function getController() {
  51. return $this->_Controller;
  52. }
  53. /**
  54. * Loads/constructs a component. Will return the instance in the registry if it already exists.
  55. * You can use `$settings['enabled'] = false` to disable callbacks on a component when loading it.
  56. * Callbacks default to on. Disabled component methods work as normal, only callbacks are disabled.
  57. *
  58. * @param string $component Component name to load
  59. * @param array $settings Settings for the component.
  60. * @return Component A component object, Either the existing loaded component or a new one.
  61. * @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
  62. */
  63. public function load($component, $settings = array()) {
  64. list($plugin, $name) = pluginSplit($component);
  65. if (isset($this->_loaded[$name])) {
  66. return $this->_loaded[$name];
  67. }
  68. $componentClass = $name . 'Component';
  69. App::uses($componentClass, 'Controller/Component');
  70. if (!class_exists($componentClass)) {
  71. if (!class_exists($componentClass)) {
  72. throw new MissingComponentFileException(array(
  73. 'file' => Inflector::underscore($component) . '.php',
  74. 'class' => $componentClass
  75. ));
  76. }
  77. }
  78. $this->_loaded[$name] = new $componentClass($this, $settings);
  79. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  80. if ($enable === true) {
  81. $this->_enabled[] = $name;
  82. }
  83. return $this->_loaded[$name];
  84. }
  85. }