Completed
Push — master ( c3a59f...393ec2 )
by Restu
12:11
created

Application::initConfigDir()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 10
rs 9.4285
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
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
9
10
/**
11
 * Class Application
12
 * @package JayaCode\Framework\Core\Application
13
 */
14
class Application
15
{
16
    /**
17
     * @var Request
18
     */
19
    public $request;
20
21
    /**
22
     * @var Response
23
     */
24
    public $response;
25
26
    /**
27
     * @var Session
28
     */
29
    public $session;
30
31
    /**
32
     * @var RouteHandle
33
     */
34
    public $routeHandle;
35
36
    /**
37
     * Application constructor.
38
     * @param SessionStorageInterface $storage
39
     */
40
    public function __construct(SessionStorageInterface $storage = null)
41
    {
42
        $this->initialize($storage);
43
    }
44
45
    /**
46
     * Create Application object from static function
47
     * @param SessionStorageInterface $storage
48
     * @return Application
49
     */
50
    public static function create(SessionStorageInterface $storage = null)
51
    {
52
        return new static($storage);
53
    }
54
55
    /**
56
     * initialize Application
57
     * @param SessionStorageInterface $storage
58
     */
59
    public function initialize(SessionStorageInterface $storage = null)
60
    {
61
        $this->session = new Session($storage);
62
        if (!$this->session->isStarted()) {
63
            $this->session->start();
64
        }
65
66
        $this->request = Request::createFromSymfonyGlobal($this->session);
67
68
        $this->response = Response::create();
69
        $this->routeHandle = RouteHandle::create($this);
70
71
        $this->setTimeZone();
72
    }
73
74
    public function setTimeZone($timezone = 'Asia/Jakarta')
75
    {
76
        return date_default_timezone_set($timezone);
77
    }
78
79
    /**
80
     * @param $baseDir
81
     */
82
    public function initConfigDir($baseDir)
83
    {
84
        if (!defined("__APP_DIR__")) {
85
            define("__APP_DIR__", $baseDir);
86
        }
87
88
        if (!defined("__APP_DIR__")) {
89
            define("__CONFIG_DIR__", __APP_DIR__."/config");
90
        }
91
    }
92
93
    /**
94
     * Run Application
95
     */
96
    public function run()
97
    {
98
        $this->routeHandle->handle();
99
        $this->response->send();
100
101
        $this->terminate();
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107
    public function getRequest()
108
    {
109
        return $this->request;
110
    }
111
112
    /**
113
     * @param mixed $request
114
     */
115
    public function setRequest($request)
116
    {
117
        $this->request = $request;
118
    }
119
120
    /**
121
     * @return mixed
122
     */
123
    public function getResponse()
124
    {
125
        return $this->response;
126
    }
127
128
    /**
129
     * @param mixed $response
130
     */
131
    public function setResponse($response)
132
    {
133
        $this->response = $response;
134
    }
135
136
    /**
137
     * @param array $routesArr
138
     */
139
    public function setListRoute($routesArr = array())
140
    {
141
        $this->routeHandle->setRoutes($routesArr);
142
    }
143
144
    /**
145
     * terminate application
146
     */
147
    public function terminate()
148
    {
149
        $inputs = array(
150
            "post" => $this->request->post(),
151
            "query" => $this->request->query()
152
        );
153
        $this->session->setFlash("old", $inputs);
154
155
        $this->request->getSession()->terminate();
156
157
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method terminate() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
158
    }
159
}
160