Completed
Push — master ( c9a5b8...803065 )
by Restu
11:14
created

Application::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 3
b 1
f 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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\Dispatcher\Dispatcher as DispatcherRoute;
9
use JayaCode\Framework\Core\Route\Status;
10
use JayaCode\Framework\Core\Session\Session;
11
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
12
13
/**
14
 * Class Application
15
 * @package JayaCode\Framework\Core\Application
16
 */
17
class Application
18
{
19
    /**
20
     * @var Application
21
     */
22
    public static $app;
23
24
    /**
25
     * @var Request
26
     */
27
    public $request;
28
29
    /**
30
     * @var Response
31
     */
32
    public $response;
33
34
    /**
35
     * @var Session
36
     */
37
    public $session;
38
39
    /**
40
     * @var DispatcherRoute
41
     */
42
    protected $routeDispatcher;
43
44
    /**
45
     * @var Database
46
     */
47
    public $db;
48
49
    /**
50
     * Application constructor.
51
     * @param SessionStorageInterface $storage
52
     */
53 6
    public function __construct(SessionStorageInterface $storage = null)
54
    {
55 6
        $this->initialize($storage);
56 6
    }
57
58
    /**
59
     * Create Application object from static function
60
     * @param SessionStorageInterface $storage
61
     * @return Application
62
     */
63
    public static function create(SessionStorageInterface $storage = null)
64
    {
65
        return new static($storage);
66
    }
67
68
    /**
69
     * initialize Application
70
     * @param SessionStorageInterface $storage
71
     */
72 6
    public function initialize(SessionStorageInterface $storage = null)
73
    {
74 6
        $this->session = new Session($storage);
75 6
        if (!$this->session->isStarted()) {
76 6
            $this->session->start();
77 6
        }
78
79 6
        $this->request = Request::createFromSymfonyGlobal($this->session);
80
81 6
        $this->response = Response::create();
82
83 6
        static::$app = $this;
84
85 6
        $this->setTimeZone();
86
87 6
        $this->initDatabase();
88 6
    }
89
90
    /**
91
     * initialize Database
92
     */
93 6
    protected function initDatabase()
94
    {
95 6
        $dbConfig = (array) config("database");
96
97 6
        if (count($dbConfig) > 0) {
98
            $this->db = new Database($dbConfig);
99
            Model::$db = $this->db;
100
        }
101 6
    }
102
103
    /**
104
     * @param string $timezone
105
     * @return bool
106
     */
107 6
    public function setTimeZone($timezone = 'Asia/Jakarta')
108
    {
109 6
        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
        $route = $this->routeDispatcher->dispatch($this->request->method(), $this->request->path());
132
133
        if ($route[0] == Status::FOUND) {
134
            if (is_callable($route[1])) {
135
                $this->response->setContent(call_user_func($route[1]));
136
            } elseif (is_array($route[1])) {
137
                $controllerName = $route[1]["controller"];
138
                $actionMethod = $route[1]["method"];
139
140
                $controller = new $controllerName($this);
141
142
                $content = $controller->$actionMethod($this->request);
143
                $this->response->setDataContent($content);
144
            }
145
        } else {
146
            $this->response->setNotFound();
147
        }
148
149
        $this->response->send();
150
151
        $this->terminate();
152
    }
153
154
    /**
155
     * @return mixed
156
     */
157
    public function getRequest()
158
    {
159
        return $this->request;
160
    }
161
162
    /**
163
     * @param mixed $request
164
     */
165
    public function setRequest($request)
166
    {
167
        $this->request = $request;
168
    }
169
170
    /**
171
     * @return mixed
172
     */
173
    public function getResponse()
174
    {
175
        return $this->response;
176
    }
177
178
    /**
179
     * @param mixed $response
180
     */
181
    public function setResponse($response)
182
    {
183
        $this->response = $response;
184
    }
185
186
    /**
187
     * @param callable $definitionCollection
188
     */
189
    public function setDataRoute($definitionCollection)
190
    {
191
        $this->routeDispatcher = \JayaCode\Framework\Core\Route\dispatcherBasic($definitionCollection);
192
    }
193
194
    /**
195
     * terminate application
196
     */
197
    public function terminate()
198
    {
199
        $inputs = array(
200
            "post" => $this->request->post(),
201
            "query" => $this->request->query()
202
        );
203
        $this->session->setFlash("old", $inputs);
204
205
        $this->request->getSession()->terminate();
206
    }
207
}
208