Completed
Push — master ( 719686...70b5eb )
by Restu
19:10 queued 04:12
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\RouteHandle;
9
use JayaCode\Framework\Core\Session\Session;
10
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
11
12
/**
13
 * Class Application
14
 * @package JayaCode\Framework\Core\Application
15
 */
16
class Application
17
{
18
    /**
19
     * @var Application
20
     */
21
    public static $app;
22
23
    /**
24
     * @var Request
25
     */
26
    public $request;
27
28
    /**
29
     * @var Response
30
     */
31
    public $response;
32
33
    /**
34
     * @var Session
35
     */
36
    public $session;
37
38
    /**
39
     * @var RouteHandle
40
     */
41
    public $routeHandle;
42
43
    /**
44
     * @var Database
45
     */
46
    public $db;
47
48
    /**
49
     * Application constructor.
50
     * @param SessionStorageInterface $storage
51
     */
52
    public function __construct(SessionStorageInterface $storage = null)
53
    {
54
        $this->initialize($storage);
55
    }
56
57
    /**
58
     * Create Application object from static function
59
     * @param SessionStorageInterface $storage
60
     * @return Application
61
     */
62
    public static function create(SessionStorageInterface $storage = null)
63
    {
64
        return new static($storage);
65
    }
66
67
    /**
68
     * initialize Application
69
     * @param SessionStorageInterface $storage
70
     */
71
    public function initialize(SessionStorageInterface $storage = null)
72
    {
73
        $this->session = new Session($storage);
74
        if (!$this->session->isStarted()) {
75
            $this->session->start();
76
        }
77
78
        $this->request = Request::createFromSymfonyGlobal($this->session);
79
80
        $this->response = Response::create();
81
        $this->routeHandle = RouteHandle::create($this);
82
83
        static::$app = $this;
84
85
        $this->setTimeZone();
86
87
        $this->initDatabase();
88
    }
89
90
    /**
91
     * initialize Database
92
     */
93
    protected function initDatabase()
94
    {
95
        $dbConfig = (array) config("database");
96
97
        if (count($dbConfig) > 0) {
98
            $this->db = new Database($dbConfig);
99
            Model::$db = $dbConfig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbConfig of type array is incompatible with the declared type object<JayaCode\Framework\Core\Database\Database> of property $db.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
        }
101
    }
102
103
    /**
104
     * @param string $timezone
105
     * @return bool
106
     */
107
    public function setTimeZone($timezone = 'Asia/Jakarta')
108
    {
109
        return date_default_timezone_set($timezone);
110
    }
111
112
    /**
113
     * @param $baseDir
114
     */
115
    public function initConfigDir($baseDir)
116
    {
117
        if (!defined("__APP_DIR__")) {
118
            define("__APP_DIR__", $baseDir);
119
        }
120
121
        if (!defined("__CONFIG_DIR__")) {
122
            define("__CONFIG_DIR__", __APP_DIR__."/config");
123
        }
124
    }
125
126
    /**
127
     * Run Application
128
     */
129
    public function run()
130
    {
131
        $this->routeHandle->handle();
132
        $this->response->send();
133
134
        $this->terminate();
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140
    public function getRequest()
141
    {
142
        return $this->request;
143
    }
144
145
    /**
146
     * @param mixed $request
147
     */
148
    public function setRequest($request)
149
    {
150
        $this->request = $request;
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function getResponse()
157
    {
158
        return $this->response;
159
    }
160
161
    /**
162
     * @param mixed $response
163
     */
164
    public function setResponse($response)
165
    {
166
        $this->response = $response;
167
    }
168
169
    /**
170
     * @param array $routesArr
171
     */
172
    public function setListRoute($routesArr = array())
173
    {
174
        $this->routeHandle->setRoutes($routesArr);
175
    }
176
177
    /**
178
     * terminate application
179
     */
180
    public function terminate()
181
    {
182
        $inputs = array(
183
            "post" => $this->request->post(),
184
            "query" => $this->request->query()
185
        );
186
        $this->session->setFlash("old", $inputs);
187
188
        $this->request->getSession()->terminate();
189
    }
190
}
191