1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Application; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Database; |
5
|
|
|
use JayaCode\Framework\Core\Database\Model\Model; |
6
|
|
|
use JayaCode\Framework\Core\Http\Request; |
7
|
|
|
use JayaCode\Framework\Core\Http\Response; |
8
|
|
|
use JayaCode\Framework\Core\Route\Dispatcher\Dispatcher as DispatcherRoute; |
9
|
|
|
use JayaCode\Framework\Core\Route\Status; |
10
|
|
|
use JayaCode\Framework\Core\Session\Session; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Application |
14
|
|
|
* @package JayaCode\Framework\Core\Application |
15
|
|
|
*/ |
16
|
|
|
class Application |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Request |
21
|
|
|
*/ |
22
|
|
|
public $request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Response |
26
|
|
|
*/ |
27
|
|
|
public $response; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Session |
31
|
|
|
*/ |
32
|
|
|
public $session; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DispatcherRoute |
36
|
|
|
*/ |
37
|
|
|
protected $routeDispatcher; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Database |
41
|
|
|
*/ |
42
|
|
|
public $db; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Application constructor. |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param Response $response |
48
|
|
|
* @param Session $session |
49
|
|
|
* @param DispatcherRoute $routeDispatcher |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
Request $request, |
|
|
|
|
53
|
|
|
Response $response, |
54
|
|
|
Session $session, |
55
|
|
|
DispatcherRoute $routeDispatcher |
56
|
|
|
) { |
57
|
|
|
|
58
|
|
|
$this->request = $request; |
59
|
|
|
$this->response = $response; |
60
|
|
|
$this->session = $session; |
61
|
|
|
$this->routeDispatcher = $routeDispatcher; |
62
|
|
|
|
63
|
|
|
if (!$this->session->isStarted()) { |
64
|
|
|
$this->session->start(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->setTimeZone(); |
68
|
|
|
$this->initDatabase(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* initialize Database |
73
|
|
|
*/ |
74
|
|
|
protected function initDatabase() |
75
|
|
|
{ |
76
|
|
|
$dbConfig = (array) config("database"); |
77
|
|
|
|
78
|
|
|
if (count($dbConfig) > 0) { |
79
|
|
|
$this->db = new Database($dbConfig); |
80
|
|
|
Model::$db = $this->db; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $timezone |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function setTimeZone($timezone = 'Asia/Jakarta') |
89
|
|
|
{ |
90
|
|
|
return date_default_timezone_set($timezone); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Run Application |
95
|
|
|
*/ |
96
|
|
|
public function run() |
97
|
|
|
{ |
98
|
|
|
$route = $this->routeDispatcher->dispatch($this->request->method(), $this->request->path()); |
99
|
|
|
|
100
|
|
|
if ($route[0] == Status::FOUND) { |
101
|
|
|
if (is_callable($route[1])) { |
102
|
|
|
$this->response->setContent(call_user_func($route[1])); |
103
|
|
|
} elseif (is_array($route[1])) { |
104
|
|
|
$controllerName = $route[1]["controller"]; |
105
|
|
|
$actionMethod = $route[1]["method"]; |
106
|
|
|
|
107
|
|
|
$controller = new $controllerName($this); |
108
|
|
|
|
109
|
|
|
$content = $controller->$actionMethod($this->request); |
110
|
|
|
$this->response->setDataContent($content); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
$this->response->setNotFound(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->response->send(); |
117
|
|
|
|
118
|
|
|
$this->terminate(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* terminate application |
123
|
|
|
*/ |
124
|
|
|
public function terminate() |
125
|
|
|
{ |
126
|
|
|
$inputs = array( |
127
|
|
|
"post" => $this->request->post(), |
128
|
|
|
"query" => $this->request->query() |
129
|
|
|
); |
130
|
|
|
$this->session->setFlash("old", $inputs); |
131
|
|
|
|
132
|
|
|
$this->request->getSession()->terminate(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|