|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fastpress; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
Class Application implements \ArrayAccess{ |
|
7
|
|
|
protected $container = array(); |
|
8
|
|
|
function __construct($conf){ |
|
|
|
|
|
|
9
|
|
|
$this->container = $conf; |
|
10
|
|
|
} |
|
11
|
|
|
public function toJson(array $array){ |
|
12
|
|
|
return json_encode($array); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function offsetGet($offset){ |
|
16
|
|
|
if(array_key_exists($offset, $this->container) && |
|
17
|
|
|
is_callable($this->container[$offset])){ |
|
18
|
|
|
return $this->container[$offset](); |
|
19
|
|
|
} |
|
20
|
|
|
return $this->container[$offset]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function offsetExists($offset){ |
|
24
|
|
|
return array_key_exists($offset, $this->container); |
|
25
|
|
|
} |
|
26
|
|
|
public function offsetSet($offset, $value){ |
|
27
|
|
|
if(strpos($offset, ':')){ |
|
28
|
|
|
list($index, $subset) = explode(':', $offset, 2); |
|
29
|
|
|
$this->container[$index][$subset] = $value; |
|
30
|
|
|
} |
|
31
|
|
|
$this->container[$offset] = $value; |
|
32
|
|
|
} |
|
33
|
|
|
public function store(Callable $callable){ |
|
34
|
|
|
return function () use ($callable){ |
|
35
|
|
|
static $object; |
|
36
|
|
|
if(null == $object){ |
|
37
|
|
|
$object = $callable($this->container); |
|
38
|
|
|
} |
|
39
|
|
|
return $object; |
|
40
|
|
|
}; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function escape($text){ |
|
44
|
|
|
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function server($var, $filter = NULL){ |
|
48
|
|
|
return $this['request']->server($var, $filter); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isGet(){ |
|
52
|
|
|
return $this['request']->isGet(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function isPost(){ |
|
56
|
|
|
return $this['request']->isPost(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function isPut(){ |
|
60
|
|
|
return $this['request']->isPut(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function isDelete(){ |
|
64
|
|
|
return $this['request']->isDelete(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function app($key, $value = null){ |
|
68
|
|
|
if(null === $value){ |
|
69
|
|
|
return $this->offsetGet($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->offsetSet($key, $value); |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function setResponse($header = 200, $statusText = null){ |
|
77
|
|
|
$this['response']->setResponse($header, $statusText); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getVar($value, $filter = null){ |
|
81
|
|
|
return $this['request']->get($value, $filter); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function postVar($value, $filter = null){ |
|
85
|
|
|
return $this['request']->post($value, $filter); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function any($path, $resource){ |
|
89
|
|
|
return $this['route']->any($path, $resource); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function get($index, $modifier){ |
|
93
|
|
|
if(!is_string($modifier) && !is_callable($modifier)){ |
|
94
|
|
|
return $this['request']->get($index, $modifier); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this['route']->get($index, $modifier); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function post($index, $modifier){ |
|
101
|
|
|
if(!is_string($modifier) && !is_callable($modifier)){ |
|
102
|
|
|
return $this['request']->post($index, $modifier); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this['route']->post($index, $modifier); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function put($path, $resource){ |
|
109
|
|
|
return $this['route']->put($path, $resource); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function delete($path, $resource){ |
|
113
|
|
|
return $this['route']->delete($path, $resource); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function controllerDipatcher($resource){ |
|
117
|
|
|
$controller = $resource['controller']; |
|
118
|
|
|
$method = $resource['method']; |
|
119
|
|
|
$args = $resource['args']; |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
$controller = $resource['controller']; |
|
123
|
|
|
|
|
124
|
|
|
if(!class_exists($controller)){ |
|
125
|
|
|
throw new \Exception("controller $controller does not exist"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$controller = new $controller; |
|
129
|
|
|
if(!method_exists($controller, $method)){ |
|
130
|
|
|
throw new \Exception("method $method does not exist in $controller"); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
(new $controller)->$method($args, $this); |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function view($block, array $variables = []){ |
|
138
|
|
|
$this['view']->view($block, $variables); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function layout($layout, array $variables = []){ |
|
142
|
|
|
$this['view']->layout($layout, $variables); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function run(){ |
|
146
|
|
|
$input = $this['request']->requestGlobals(); |
|
147
|
|
|
$resource = $this['route']->match($input['server'], $input['post']); |
|
148
|
|
|
|
|
149
|
|
|
if(is_array($resource) && !empty($resource)){ |
|
150
|
|
|
$dispatch = $this->controllerDipatcher($resource); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function offsetUnset($offset){} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.