public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
midcom / midcom_core / services / dispatcher / manual.php
100644 190 lines (167 sloc) 6.045 kb
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* @package midcom_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
/**
* Manual dispatcher for MidCOM 3
*
* Dispatches requested route and controller of components.
*
* @package midcom_core
*/
class midcom_core_services_dispatcher_manual implements midcom_core_services_dispatcher
{
    public $component_name = '';
    public $component_instance = false;
    private $page = null;
    protected $route_id = false;
    protected $action_arguments = array();
 
    public function __construct()
    {
    }
 
    /**
* Pull data from environment into the context.
*/
    public function populate_environment_data()
    {
        if (!$this->page)
        {
            return;
        }
        
        $page_data = array();
        $page_data['guid'] = $this->page->guid;
        $page_data['title'] = $this->page->title;
        $page_data['content'] = $this->page->content;
        $_MIDCOM->context->component = $this->page->component;
        $_MIDCOM->context->page = $page_data;
        $_MIDCOM->context->prefix = $this->get_page_prefix();
        
        $_MIDCOM->templating->append_page($this->page->id);
    }
 
    public function initialize($component)
    {
        if ($_MIDCOM->timer)
        {
            $_MIDCOM->timer->setMarker("MidCOM dispatcher::initialize::{$component}");
        }
        $this->component_name = $component;
        
        if ($this->page)
        {
            $_MIDCOM->context->component_instance = $_MIDCOM->componentloader->load($this->component_name, $this->page);
        }
        else
        {
            $_MIDCOM->context->component_instance = $_MIDCOM->componentloader->load($this->component_name);
        }
        
        $_MIDCOM->templating->append_directory($_MIDCOM->componentloader->component_to_filepath($this->component_name) . '/templates');
    }
    
    public function get_routes()
    {
        $core_routes = $_MIDCOM->configuration->get('routes');
        $component_routes = $_MIDCOM->context->component_instance->configuration->get('routes');
        
        return array_merge($core_routes, $component_routes);
    }
    
    public function set_page(midgard_page $page)
    {
        $this->page = $page;
    }
    
    private function get_page_prefix()
    {
        if (!$this->page)
        {
            throw new Exception("No page set for the manual dispatcher");
        }
    
        $prefix = "{$_MIDGARD['prefix']}/";
        $host_mc = midgard_host::new_collector('id', $_MIDGARD['host']);
        $host_mc->set_key_property('root');
        $host_mc->execute();
        $roots = $host_mc->list_keys();
        if (!$roots)
        {
            throw new Exception("Failed to load root page data for host {$_MIDGARD['host']}");
        }
        $root_id = null;
        foreach ($roots as $root => $array)
        {
            $root_id = $root;
            break;
        }
        
        if ($this->page->id == $root_id)
        {
            return $prefix;
        }
        
        $page_path = '';
        $page_id = $this->page->id;
        while ( $page_id
               && $page_id != $root_id)
        {
            $parent_mc = midgard_page::new_collector('id', $page_id);
            $parent_mc->set_key_property('up');
            $parent_mc->add_value_property('name');
            $parent_mc->execute();
            $parents = $parent_mc->list_keys();
            foreach ($parents as $parent => $array)
            {
                $page_id = $parent;
                $page_path = $parent_mc->get_subkey($parent, 'name') . "/{$page_path}";
            }
        }
        
        return $prefix . $page_path;
    }
    
    public function set_route($route_id, array $arguments)
    {
        $this->route_id = $route_id;
        $_MIDCOM->context->route_id = $this->route_id;
        $this->action_arguments = $arguments;
    }
 
    /**
* Load a component and dispatch the request to it
*/
    public function dispatch()
    {
        if ($_MIDCOM->timer)
        {
            $_MIDCOM->timer->setMarker("MidCOM dispatcher::dispatch::{$this->component_name}");
        }
        $route_definitions = $this->get_routes();
 
        $selected_route_configuration = $route_definitions[$this->route_id];
 
        $controller_class = $selected_route_configuration['controller'];
        $controller = new $controller_class($_MIDCOM->context->component_instance);
        
        // Then call the route_id
        $action_method = "action_{$selected_route_configuration['action']}";
        $data = array();
        if ($_MIDCOM->timer)
        {
            $_MIDCOM->timer->setMarker("MidCOM dispatcher::dispatch::{$this->component_name}::{$controller_class}::{$action_method}");
        }
        $controller->$action_method($this->route_id, $data, $this->action_arguments);
        $_MIDCOM->context->set_item($this->component_name, $data);
        
        // Set other context data from route
        if (isset($selected_route_configuration['mimetype']))
        {
            $_MIDCOM->context->mimetype = $selected_route_configuration['mimetype'];
        }
        if (isset($selected_route_configuration['template_entry_point']))
        {
            $_MIDCOM->context->template_entry_point = $selected_route_configuration['template_entry_point'];
        }
        if (isset($selected_route_configuration['content_entry_point']))
        {
            $_MIDCOM->context->content_entry_point = $selected_route_configuration['content_entry_point'];
        }
    }
 
    /**
* Generates an URL for given route_id with given arguments
*
* @param string $route_id the id of the route to generate a link for
* @param array $args associative arguments array
* @return string url
*/
    public function generate_url($route_id, array $args)
    {
        return '';
    }
}
?>