PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Controller/ComponentCollection.php

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