Skip to content

Commit

Permalink
Making Controller use ComponentCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 256532b commit fac9773
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 17 additions & 0 deletions cake/libs/controller/component_collection.php
Expand Up @@ -20,6 +20,23 @@

class ComponentCollection extends ObjectCollection {

/**
* 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;
}
$components = ComponentCollection::normalizeObjectArray($Controller->components);
foreach ($components as $name => $properties) {
$Controller->{$name} = $this->load($properites['class'], $properties['settings']);
}
}

/**
* Loads/constructs a component. Will return the instance in the registry if it already exists.
*
Expand Down
22 changes: 13 additions & 9 deletions cake/libs/controller/controller.php
Expand Up @@ -192,7 +192,7 @@ class Controller extends Object {
*
* @var string
*/
public $Component = null;
public $Components = null;

/**
* Array containing the names of components this controller uses. Component names
Expand Down Expand Up @@ -342,7 +342,7 @@ public function __construct() {
}
$this->modelClass = Inflector::classify($this->name);
$this->modelKey = Inflector::underscore($this->modelClass);
$this->Component = new Component();
$this->Components = new ComponentCollection();

$childMethods = get_class_methods($this);
$parentMethods = get_class_methods('Controller');
Expand Down Expand Up @@ -443,7 +443,7 @@ protected function __mergeVars() {
*/
public function constructClasses() {
$this->__mergeVars();
$this->Component->init($this);
$this->Components->init($this);

if ($this->uses !== null || ($this->uses !== array())) {
if (empty($this->passedArgs) || !isset($this->passedArgs['0'])) {
Expand Down Expand Up @@ -471,7 +471,7 @@ public function constructClasses() {

/**
* Perform the startup process for this controller.
* Fire the Component and Controller callbacks in the correct order.
* Fire the Components and Controller callbacks in the correct order.
*
* - Initializes components, which fires their `initialize` callback
* - Calls the controller `beforeFilter`.
Expand All @@ -480,22 +480,22 @@ public function constructClasses() {
* @return void
*/
public function startupProcess() {
$this->Component->initialize($this);
$this->Components->trigger('initialize', array(&$this));
$this->beforeFilter();
$this->Component->triggerCallback('startup', $this);
$this->Components->trigger('startup', array(&$this));
}

/**
* Perform the various shutdown processes for this controller.
* Fire the Component and Controller callbacks in the correct order.
* Fire the Components and Controller callbacks in the correct order.
*
* - triggers the component `shutdown` callback.
* - calls the Controller's `afterFilter` method.
*
* @return void
*/
public function shutdownProcess() {
$this->Component->triggerCallback('shutdown', $this);
$this->Components->trigger('shutdown', array(&$this));
$this->afterFilter();
}

Expand Down Expand Up @@ -622,7 +622,11 @@ public function redirect($url, $status = null, $exit = true) {
if (is_array($status)) {
extract($status, EXTR_OVERWRITE);
}
$response = $this->Component->beforeRedirect($this, $url, $status, $exit);
$response = $this->Components->trigger(
'beforeRedirect',
array(&$this, $url, $status, $exit),
array('break' => true, 'breakOn' => false)
);

if ($response === false) {
return;
Expand Down

0 comments on commit fac9773

Please sign in to comment.