Completed
Push — master ( c2bacd...81a0c2 )
by Park Jong-Hun
08:02
created

Application::setDbConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Core;
4
5
use Doctrine\ORM\Tools\Setup;
6
use Doctrine\ORM\EntityManager;
7
use JBZoo\Event\EventManager;
8
use Core\ControllerDispatcher\Dispatcher;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class Application
12
{
13
    private $routerConfig = [];
14
    private $siteConfig = [];
15
    private $errorReporterConfig = [];
16
    private $viewEngineConfig = [];
17
18
    private $eventListeners = [];
19
    private $viewResolvers = [];
20
21
    /**
22
     * Singleton: private constructor
23
     */
24
    private function __construct()
25
    {
26
    }
27
28
    /**
29
     * @return Application
30
     */
31
    public static function getInstance()
32
    {
33
        static $instance = null;
34
35
        if ($instance === null) {
36
            $instance = new self();
37
        }
38
39
        return $instance;
40
    }
41
42
    public function setSiteConfig(array $siteConfig)
43
    {
44
        $this->siteConfig = $siteConfig;
45
    }
46
47
    public function setErrorReporterConfig(array $errorReporterConfig)
48
    {
49
        $this->errorReporterConfig = $errorReporterConfig;
50
    }
51
52
    public function setViewEngineConfig(array $viewEngineConfig)
53
    {
54
        $this->viewEngineConfig = $viewEngineConfig;
55
    }
56
57
    public function setEventListener(array $eventListeners)
58
    {
59
        $this->eventListeners = $eventListeners;
60
    }
61
62
    public function setViewResolver(array $viewResolvers)
63
    {
64
        $this->viewResolvers = $viewResolvers;
65
    }
66
67
    public function setDisplayError($isDisplay)
68
    {
69
        error_reporting(E_ALL);
70
        ini_set('display_errors', $isDisplay);
71
    }
72
73
    public function registerErrorReporters()
74
    {
75
        $register = new ErrorReporterRegister();
76
        $register->setErrorReporterConfig($this->errorReporterConfig);
77
        $register->setEnabledReporters($this->siteConfig['errorReporters']);
78
        $register->register();
79
    }
80
81
    public function setRouterConfig(array $routerConfig)
82
    {
83
        $this->routerConfig = $routerConfig;
84
    }
85
86
    public function registerEventListener()
87
    {
88
        $register = new EventListenerRegister();
89
        $register->setEventListener($this->eventListeners);
90
        $register->register();
91
    }
92
93
94
    public function dispatch(ServerRequestInterface $request)
95
    {
96
        $dispatcher = new Dispatcher();
97
98
        $dispatcher->setRequest($request);
99
        $dispatcher->setRouterConfig($this->routerConfig);
100
        $dispatcher->setViewEngineConfig($this->viewEngineConfig);
101
        $dispatcher->setViewResolver($this->viewResolvers);
102
103
        $dispatcher->dispatch();
104
    }
105
106
107
    /**
108
     * Return url path with site url
109
     * @param  string $url sub url
110
     * @return string
111
     */
112
    public function url($url = '')
113
    {
114
        $url = $url === '/' ? '' : $url;
115
        return $this->siteConfig['url'] . $url;
116
    }
117
118
119
    /**
120
     * @return EventManager
121
     */
122
    public function getEventManager()
123
    {
124
        static $instance = null;
125
126
        if ($instance === null) {
127
            $instance = new EventManager();
128
        }
129
130
        return $instance;
131
    }
132
}
133