ComponentCollection.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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', 'Core');
  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. * @param string $component Component name to load
  58. * @param array $settings Settings for the component.
  59. * @return Component A component object, Either the existing loaded component or a new one.
  60. * @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
  61. */
  62. public function load($component, $settings = array()) {
  63. list($plugin, $name) = pluginSplit($component);
  64. if (isset($this->_loaded[$name])) {
  65. return $this->_loaded[$name];
  66. }
  67. $componentClass = $name . 'Component';
  68. if (!class_exists($componentClass)) {
  69. if (!App::import('Component', $component)) {
  70. throw new MissingComponentFileException(array(
  71. 'file' => Inflector::underscore($component) . '.php',
  72. 'class' => $componentClass
  73. ));
  74. }
  75. if (!class_exists($componentClass)) {
  76. throw new MissingComponentFileException(array(
  77. 'file' => Inflector::underscore($component) . '.php',
  78. 'class' => $componentClass
  79. ));
  80. }
  81. }
  82. $this->_loaded[$name] = new $componentClass($this, $settings);
  83. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  84. if ($enable === true) {
  85. $this->_enabled[] = $name;
  86. }
  87. return $this->_loaded[$name];
  88. }
  89. }