1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Application; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Http\Request; |
5
|
|
|
use JayaCode\Framework\Core\Http\Response; |
6
|
|
|
use JayaCode\Framework\Core\Route\RouteHandle; |
7
|
|
|
use JayaCode\Framework\Core\Session\Session; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Application |
11
|
|
|
* @package JayaCode\Framework\Core\Application |
12
|
|
|
*/ |
13
|
|
|
class Application |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Request |
17
|
|
|
*/ |
18
|
|
|
public $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Response |
22
|
|
|
*/ |
23
|
|
|
public $response; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Session |
27
|
|
|
*/ |
28
|
|
|
public $session; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RouteHandle |
32
|
|
|
*/ |
33
|
|
|
public $routeHandle; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Application constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
$this->initialize(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create Application object from static function |
45
|
|
|
* @return Application |
46
|
|
|
*/ |
47
|
|
|
public static function create() |
48
|
|
|
{ |
49
|
|
|
return new static(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* initialize Application |
54
|
|
|
*/ |
55
|
|
|
public function initialize() |
56
|
|
|
{ |
57
|
|
|
$this->session = new Session(); |
58
|
|
|
if (!$this->session->isStarted()) { |
59
|
|
|
$this->session->start(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->request = Request::createFromSymfonyGlobal($this->session); |
63
|
|
|
|
64
|
|
|
$this->response = Response::create(); |
65
|
|
|
$this->routeHandle = RouteHandle::create($this->request, $this->response); |
66
|
|
|
|
67
|
|
|
$this->setTimeZone(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setTimeZone($timezone = 'Asia/Jakarta') |
71
|
|
|
{ |
72
|
|
|
return date_default_timezone_set($timezone); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Run Application |
77
|
|
|
*/ |
78
|
|
|
public function run() |
79
|
|
|
{ |
80
|
|
|
$this->routeHandle->handle(); |
81
|
|
|
$this->response->send(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function getRequest() |
88
|
|
|
{ |
89
|
|
|
return $this->request; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param mixed $request |
94
|
|
|
*/ |
95
|
|
|
public function setRequest($request) |
96
|
|
|
{ |
97
|
|
|
$this->request = $request; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function getResponse() |
104
|
|
|
{ |
105
|
|
|
return $this->response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $response |
110
|
|
|
*/ |
111
|
|
|
public function setResponse($response) |
112
|
|
|
{ |
113
|
|
|
$this->response = $response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $routesArr |
118
|
|
|
*/ |
119
|
|
|
public function setListRoute($routesArr = array()) |
120
|
|
|
{ |
121
|
|
|
$this->routeHandle->setRoutes($routesArr); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* terminate application |
126
|
|
|
*/ |
127
|
|
|
public function terminate() |
128
|
|
|
{ |
129
|
|
|
$inputs = array( |
130
|
|
|
"post" => $this->request->post(), |
131
|
|
|
"query" => $this->request->query() |
132
|
|
|
); |
133
|
|
|
$this->session->setFlash("old", $inputs); |
134
|
|
|
|
135
|
|
|
$this->request->getSession()->terminate(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|