Completed
Push — master ( e6b3d9...94ffcd )
by Park Jong-Hun
03:09
created

Application::getEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
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
9
class Application
10
{
11
    private $routerConfig = [];
12
    private $siteConfig = [];
13
    private $errorReporterConfig = [];
14
    private $viewEngineConfig = [];
15
    private $dbConfig = [];
16
17
    /**
18
     * Singleton: private constructor
19
     */
20
    private function __construct()
21
    {
22
    }
23
24
    /**
25
     * @return Application
26
     */
27
    public static function getInstance()
28
    {
29
        static $instance = null;
30
31
        if ($instance === null) {
32
            $instance = new self();
33
        }
34
35
        return $instance;
36
    }
37
38
    public function setSiteConfig(array $siteConfig)
39
    {
40
        $this->siteConfig = $siteConfig;
41
    }
42
43
    public function setErrorReporterConfig(array $errorReporterConfig)
44
    {
45
        $this->errorReporterConfig = $errorReporterConfig;
46
    }
47
48
    public function setDbConfig(array $dbConfig)
49
    {
50
        $this->dbConfig = $dbConfig;
51
    }
52
53
    public function setViewEngineConfig(array $viewEngineConfig)
54
    {
55
        $this->viewEngineConfig = $viewEngineConfig;
56
    }
57
58
    public function setDisplayError($isDisplay)
59
    {
60
        error_reporting(E_ALL);
61
        ini_set('display_errors', $isDisplay);
62
    }
63
64
    public function registerErrorReporters()
65
    {
66
        $register = new ErrorReporterRegister();
67
        $register->setErrorReporterConfig($this->errorReporterConfig);
68
        $register->setEnabledReporters($this->siteConfig['errorReporters']);
69
        $register->regist();
70
    }
71
72
    public function setRouterConfig(array $routerConfig)
73
    {
74
        $this->routerConfig = $routerConfig;
75
    }
76
77
    public function dispatch(Request $request)
78
    {
79
        $dispatcher = new AppDispatcher();
80
        $dispatcher->setRouterConfig($this->routerConfig);
81
        $dispatcher->setViewEngineConfig($this->viewEngineConfig[$this->siteConfig['viewEngine']]);
82
        $dispatcher->dispatch($request);
83
    }
84
85
86
    /**
87
     * Return url path with site url
88
     * @param  string $url sub url
89
     * @return string
90
     */
91
    public function url($url = '')
92
    {
93
        return $this->siteConfig['url'] . $url;
94
    }
95
96
97
    /**
98
     * @return EntityManager
99
     */
100
    public function getEntityManager()
101
    {
102
        $config = Setup::createAnnotationMetadataConfiguration(
103
                    $this->dbConfig['entityPath'],
104
                    $this->dbConfig['devMode']
105
                    );
106
        return EntityManager::create($this->dbConfig[$this->siteConfig['database']], $config);
107
    }
108
}
109