ComponentCollection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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. App::uses($componentClass, 'Controller/Component');
  69. if (!class_exists($componentClass)) {
  70. if (!class_exists($componentClass)) {
  71. throw new MissingComponentFileException(array(
  72. 'file' => Inflector::underscore($component) . '.php',
  73. 'class' => $componentClass
  74. ));
  75. }
  76. }
  77. $this->_loaded[$name] = new $componentClass($this, $settings);
  78. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  79. if ($enable === true) {
  80. $this->_enabled[] = $name;
  81. }
  82. return $this->_loaded[$name];
  83. }
  84. }