From fac97738ce15475ccd61713e90d87c23fc8b5d8a Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 4 Jul 2010 15:13:13 -0400 Subject: [PATCH] Making Controller use ComponentCollection. --- cake/libs/controller/component_collection.php | 17 ++++++++++++++ cake/libs/controller/controller.php | 22 +++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cake/libs/controller/component_collection.php b/cake/libs/controller/component_collection.php index 97228a8f2..f1dd6560d 100644 --- a/cake/libs/controller/component_collection.php +++ b/cake/libs/controller/component_collection.php @@ -20,6 +20,23 @@ App::import('Core', 'ObjectCollection'); 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. * diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index a82865d76..bdf736b6b 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -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 @@ -342,7 +342,7 @@ class Controller extends Object { } $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'); @@ -443,7 +443,7 @@ class Controller extends Object { */ 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'])) { @@ -471,7 +471,7 @@ class Controller extends Object { /** * 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`. @@ -480,14 +480,14 @@ class Controller extends Object { * @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. @@ -495,7 +495,7 @@ class Controller extends Object { * @return void */ public function shutdownProcess() { - $this->Component->triggerCallback('shutdown', $this); + $this->Components->trigger('shutdown', array(&$this)); $this->afterFilter(); } @@ -622,7 +622,11 @@ class Controller extends Object { 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;