Completed
Push — master ( a34111...b893df )
by Park Jong-Hun
14:20
created

Application::getEventManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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