-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatch.php
128 lines (106 loc) · 4.42 KB
/
Dispatch.php
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
<?php
class Dispatch_Core{
protected $controller;
public static function controller($controller)
{
$controller_file=strtolower($controller);
// Set controller class name
$controller = ucfirst($controller).'_Controller';
if(!class_exists($controller, FALSE))
{
// If the file doesn't exist, just return
if (($filepath = Kohana::find_file('controllers', $controller_file)) === FALSE)
return FALSE;
// Include the Controller file
require_once $filepath;
}
// Run system.pre_controller
Event::run('dispatch.pre_controller');
// Initialize the controller
$controller = new $controller;
// Run system.post_controller_constructor
Event::run('dispatch.post_controller_constructor');
return new Dispatch($controller);
}
public function __construct(Controller $controller)
{
$this->controller=$controller;
}
public function __get($key)
{
if($key=='controller')
{
return $this->$key;
}
else
{
return $this->controller->$key;
}
}
public function __set($key,$value)
{
$this->controller->$key=$value;
}
public function __toString()
{
return $this->render();
}
public function render()
{
return (string) $this->controller;
}
public function __call($name,$arguments=null)
{
if(method_exists($this->controller,$name))
{
return $this->method($name,$arguments);
}
return false;
}
public function method($method,$arguments=null)
{
if(!method_exists($this->controller,$method))
return false;
if (method_exists($this->controller,'_remap'))
{
// Make the arguments routed
$arguments = array($method, $arguments);
// The method becomes part of the arguments
array_unshift($arguments, $method);
// Set the method to _remap
$method = '_remap';
}
ob_start();
if(is_string($arguments))
{
$arguments=array($arguments);
}
switch(count($arguments))
{
case 1:
$result=$this->controller->$method($arguments[0]);
break;
case 2:
$result=$this->controller->$method($arguments[0], $arguments[1]);
break;
case 3:
$result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
default:
// Resort to using call_user_func_array for many segments
$result=call_user_func_array(array($this->controller, $method), $arguments);
break;
}
// Run system.post_controller
Event::run('dispatch.post_controller');
if($result!=NULL)
{
$result=ob_get_contents();
ob_end_clean();
}
return $result;
}
}