Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
//loads a component on the fly from within the controller
class ComponentLoaderComponent extends Object {
   
    var $controller = null;
   
    function initialize(&$controller) {
        // saving the controller reference for later use
        $this->controller =& $controller;
    }
 
    function load($component_name) {
       // If component already loaded/attached, then bail out
       if(isset($this->controller->{$component_name}) && is_object($this->controller->{$component_name})) {
            return;
    }
       
        // Load component and attach to controller
        App::import('Component', $component_name);
        $component2 = $component_name.'Component';
        $component =& new $component2(null);
       
        if (method_exists($component, 'initialize'))
            $component->initialize($this->controller);
 
        if (method_exists($component, 'startup'))
            $component->startup($this->controller);
       
        $this->controller->{$component_name} = &$component;
    }
}
 
?>