2010-07-04 18:00:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Components collection is used as a registry for loaded components and handles loading
|
|
|
|
* and constructing component class objects.
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-04 18:00:23 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-04 18:00:23 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller
|
2010-07-04 18:00:23 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-02-14 03:40:19 +00:00
|
|
|
|
2010-12-04 07:15:22 +00:00
|
|
|
App::uses('ObjectCollection', 'Utility');
|
2010-12-04 07:19:44 +00:00
|
|
|
App::uses('Component', 'Controller');
|
2010-07-04 18:00:23 +00:00
|
|
|
|
2011-12-08 15:35:02 +00:00
|
|
|
/**
|
|
|
|
* Components collection is used as a registry for loaded components and handles loading
|
|
|
|
* and constructing component class objects.
|
|
|
|
*
|
|
|
|
* @package Cake.Controller
|
|
|
|
*/
|
2010-07-04 18:00:23 +00:00
|
|
|
class ComponentCollection extends ObjectCollection {
|
|
|
|
|
2010-09-15 02:52:51 +00:00
|
|
|
/**
|
|
|
|
* The controller that this collection was initialized with.
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
protected $_Controller = null;
|
|
|
|
|
2010-07-04 19:13:13 +00:00
|
|
|
/**
|
|
|
|
* Initializes all the Components for a controller.
|
|
|
|
* Attaches a reference of each component to the Controller.
|
|
|
|
*
|
2011-04-17 06:13:03 +00:00
|
|
|
* @param Controller $Controller Controller to initialize components for.
|
2010-07-04 19:13:13 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function init(Controller $Controller) {
|
|
|
|
if (empty($Controller->components)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-15 02:52:51 +00:00
|
|
|
$this->_Controller = $Controller;
|
2010-07-04 19:13:13 +00:00
|
|
|
$components = ComponentCollection::normalizeObjectArray($Controller->components);
|
|
|
|
foreach ($components as $name => $properties) {
|
2010-07-05 00:29:58 +00:00
|
|
|
$Controller->{$name} = $this->load($properties['class'], $properties['settings']);
|
2010-07-04 19:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 02:52:51 +00:00
|
|
|
/**
|
|
|
|
* Get the controller associated with the collection.
|
|
|
|
*
|
|
|
|
* @return Controller.
|
|
|
|
*/
|
|
|
|
public function getController() {
|
|
|
|
return $this->_Controller;
|
|
|
|
}
|
|
|
|
|
2010-07-04 18:00:23 +00:00
|
|
|
/**
|
|
|
|
* Loads/constructs a component. Will return the instance in the registry if it already exists.
|
2010-11-07 04:07:27 +00:00
|
|
|
* You can use `$settings['enabled'] = false` to disable callbacks on a component when loading it.
|
|
|
|
* Callbacks default to on. Disabled component methods work as normal, only callbacks are disabled.
|
2011-01-10 02:27:44 +00:00
|
|
|
*
|
2011-01-14 02:06:32 +00:00
|
|
|
* You can alias your component as an existing component by setting the 'className' key, i.e.,
|
2011-01-10 02:27:44 +00:00
|
|
|
* {{{
|
|
|
|
* public $components = array(
|
2011-01-14 02:06:32 +00:00
|
|
|
* 'Email' => array(
|
|
|
|
* 'className' => 'AliasedEmail'
|
2011-01-10 02:27:44 +00:00
|
|
|
* );
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* All calls to the `Email` component would use `AliasedEmail` instead.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-07-04 18:00:23 +00:00
|
|
|
* @param string $component Component name to load
|
|
|
|
* @param array $settings Settings for the component.
|
|
|
|
* @return Component A component object, Either the existing loaded component or a new one.
|
2011-10-15 18:06:31 +00:00
|
|
|
* @throws MissingComponentException when the component could not be found
|
2010-07-04 18:00:23 +00:00
|
|
|
*/
|
2010-11-07 03:48:27 +00:00
|
|
|
public function load($component, $settings = array()) {
|
2011-01-19 00:33:05 +00:00
|
|
|
if (is_array($settings) && isset($settings['className'])) {
|
2011-01-15 01:44:33 +00:00
|
|
|
$alias = $component;
|
|
|
|
$component = $settings['className'];
|
|
|
|
}
|
2011-01-28 15:19:53 +00:00
|
|
|
list($plugin, $name) = pluginSplit($component, true);
|
2011-01-15 01:44:33 +00:00
|
|
|
if (!isset($alias)) {
|
|
|
|
$alias = $name;
|
2011-01-10 02:27:44 +00:00
|
|
|
}
|
|
|
|
if (isset($this->_loaded[$alias])) {
|
|
|
|
return $this->_loaded[$alias];
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
|
|
|
$componentClass = $name . 'Component';
|
2011-01-28 15:19:53 +00:00
|
|
|
App::uses($componentClass, $plugin . 'Controller/Component');
|
2010-07-04 18:00:23 +00:00
|
|
|
if (!class_exists($componentClass)) {
|
2011-10-15 18:06:31 +00:00
|
|
|
throw new MissingComponentException(array(
|
2011-01-22 05:30:15 +00:00
|
|
|
'class' => $componentClass
|
|
|
|
));
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
2011-01-10 02:27:44 +00:00
|
|
|
$this->_loaded[$alias] = new $componentClass($this, $settings);
|
2010-11-07 04:07:27 +00:00
|
|
|
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
|
2011-11-06 22:00:17 +00:00
|
|
|
if ($enable) {
|
|
|
|
$this->enable($alias);
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
2011-01-10 02:27:44 +00:00
|
|
|
return $this->_loaded[$alias];
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 04:08:35 +00:00
|
|
|
}
|