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)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.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)
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param Controller $controller Controller to initialize components for.
|
|
|
|
* @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.
|
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.
|
|
|
|
* @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
|
|
|
|
*/
|
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-01-22 05:30:15 +00:00
|
|
|
throw new MissingComponentClassException(array(
|
|
|
|
'file' => Inflector::underscore($componentClass) . '.php',
|
|
|
|
'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;
|
2010-07-04 18:00:23 +00:00
|
|
|
if ($enable === true) {
|
2011-01-10 02:27:44 +00:00
|
|
|
$this->_enabled[] = $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
|
|
|
}
|