|
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\RequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Application |
|
12
|
|
|
{ |
|
13
|
|
|
private $routerConfig = []; |
|
14
|
|
|
private $siteConfig = []; |
|
15
|
|
|
private $errorReporterConfig = []; |
|
16
|
|
|
private $viewEngineConfig = []; |
|
17
|
|
|
private $dbConfig = []; |
|
18
|
|
|
|
|
19
|
|
|
private $eventListeners = []; |
|
20
|
|
|
private $viewResolvers = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Singleton: private constructor |
|
24
|
|
|
*/ |
|
25
|
|
|
private function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return Application |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function getInstance() |
|
33
|
|
|
{ |
|
34
|
|
|
static $instance = null; |
|
35
|
|
|
|
|
36
|
|
|
if ($instance === null) { |
|
37
|
|
|
$instance = new self(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $instance; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setSiteConfig(array $siteConfig) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->siteConfig = $siteConfig; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function setErrorReporterConfig(array $errorReporterConfig) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->errorReporterConfig = $errorReporterConfig; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function setDbConfig(array $dbConfig) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->dbConfig = $dbConfig; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setViewEngineConfig(array $viewEngineConfig) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->viewEngineConfig = $viewEngineConfig; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function setEventListener(array $eventListeners) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->eventListeners = $eventListeners; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setViewResolver(array $viewResolvers) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->viewResolvers = $viewResolvers; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setDisplayError($isDisplay) |
|
74
|
|
|
{ |
|
75
|
|
|
error_reporting(E_ALL); |
|
76
|
|
|
ini_set('display_errors', $isDisplay); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function registerErrorReporters() |
|
80
|
|
|
{ |
|
81
|
|
|
$register = new ErrorReporterRegister(); |
|
82
|
|
|
$register->setErrorReporterConfig($this->errorReporterConfig); |
|
83
|
|
|
$register->setEnabledReporters($this->siteConfig['errorReporters']); |
|
84
|
|
|
$register->register(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setRouterConfig(array $routerConfig) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->routerConfig = $routerConfig; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function registerEventListener() |
|
93
|
|
|
{ |
|
94
|
|
|
$register = new EventListenerRegister(); |
|
95
|
|
|
$register->setEventListener($this->eventListeners); |
|
96
|
|
|
$register->register(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public function dispatch(RequestInterface $request) |
|
101
|
|
|
{ |
|
102
|
|
|
$dispatcher = new Dispatcher(); |
|
103
|
|
|
|
|
104
|
|
|
$dispatcher->setRequest($request); |
|
105
|
|
|
$dispatcher->setRouterConfig($this->routerConfig); |
|
106
|
|
|
$dispatcher->setViewEngineConfig($this->viewEngineConfig); |
|
107
|
|
|
$dispatcher->setViewResolver($this->viewResolvers); |
|
108
|
|
|
|
|
109
|
|
|
$dispatcher->dispatch(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return url path with site url |
|
115
|
|
|
* @param string $url sub url |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function url($url = '') |
|
119
|
|
|
{ |
|
120
|
|
|
$url = $url === '/' ? '' : $url; |
|
121
|
|
|
return $this->siteConfig['url'] . $url; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return EntityManager |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getEntityManager() |
|
129
|
|
|
{ |
|
130
|
|
|
$config = Setup::createAnnotationMetadataConfiguration( |
|
131
|
|
|
$this->dbConfig['entityPath'], |
|
132
|
|
|
$this->dbConfig['devMode'] |
|
133
|
|
|
); |
|
134
|
|
|
return EntityManager::create($this->dbConfig[$this->siteConfig['database']], $config); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return EventManager |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getEventManager() |
|
141
|
|
|
{ |
|
142
|
|
|
static $instance = null; |
|
143
|
|
|
|
|
144
|
|
|
if ($instance === null) { |
|
145
|
|
|
$instance = new EventManager(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $instance; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|