1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
4
|
|
|
|
5
|
|
|
use Win\Helper\Url; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Application (WinPHP Framework) |
9
|
|
|
* |
10
|
|
|
* Framework em PHP baseado em MVC |
11
|
|
|
* Esta classe é responsável por incluir as páginas de acordo com a URL e gerenciar a estrutura MVC |
12
|
|
|
* @author winPHP Framework <http://github.com/winframework/winphp/> |
13
|
|
|
* @version 1.2.5 |
14
|
|
|
*/ |
15
|
|
|
class Application { |
16
|
|
|
|
17
|
|
|
protected static $app = null; |
18
|
|
|
private $name; |
19
|
|
|
private $page; |
20
|
|
|
private $homePage = 'index'; |
21
|
|
|
private $paramList; |
22
|
|
|
|
23
|
|
|
/** @var Controller */ |
24
|
|
|
public $controller; |
25
|
|
|
|
26
|
|
|
/** @var View */ |
27
|
|
|
public $view; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cria a aplicação principal |
31
|
|
|
* @param mixed[] $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct($config = []) { |
34
|
|
|
static::$app = $this; |
35
|
|
|
Config::init($config); |
36
|
|
|
$this->name = Config::get('name', ''); |
37
|
|
|
|
38
|
|
|
$this->setParamList(Url::instance()->getFragments()); |
39
|
|
|
$this->controller = ControllerFactory::create($this->getParam(0), $this->getParam(1)); |
40
|
|
|
|
41
|
|
|
if (Router::instance()->run()): |
42
|
|
|
$this->setParamList(Router::instance()->getCustomUrl()); |
43
|
|
|
$this->controller = Router::instance()->createController(); |
44
|
|
|
endif; |
45
|
|
|
|
46
|
|
|
$this->setPage($this->getParam(0)); |
47
|
|
|
$this->view = ViewFactory::create($this->getParam(0), $this->paramList); |
48
|
|
|
|
49
|
|
|
ErrorPage::validate(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retorna o ponteiro para a aplicação principal |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public static function app() { |
57
|
|
|
return static::$app; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Roda a aplicação |
62
|
|
|
* Executando o Controller e criando o Layout que contem a View |
63
|
|
|
*/ |
64
|
|
|
public function run() { |
65
|
|
|
$this->controller->load(); |
66
|
|
|
$layout = new Layout($this->controller->layout); |
67
|
|
|
$layout->load(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @return string */ |
71
|
|
|
public function getName() { |
72
|
|
|
return $this->name; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @return string */ |
76
|
|
|
public function getFullUrl() { |
77
|
|
|
return Url::instance()->getBaseUrl() . Url::instance()->getUrl(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @return string */ |
81
|
|
|
public function getBaseUrl() { |
82
|
|
|
return Url::instance()->getBaseUrl(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retorna a URL Atual |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getUrl() { |
90
|
|
|
return Url::instance()->format(implode('/', $this->getParamList())); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retorna a página atual |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getPage() { |
98
|
|
|
return $this->page; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @param string $page */ |
102
|
|
|
public function setPage($page) { |
103
|
|
|
$this->page = $page; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retorna TRUE se está na página inicial |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function isHomePage() { |
111
|
|
|
return (boolean) ($this->page === $this->homePage); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retorna TRUE se está em alguma página de erro (404, 403, 503, etc) |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
public function isErrorPage() { |
119
|
|
|
return ErrorPage::isErrorPage(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retorna um todos os parâmetros da URL |
124
|
|
|
* @return string[] |
125
|
|
|
*/ |
126
|
|
|
protected function getParamList() { |
127
|
|
|
return $this->paramList; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Define os parâmetros. |
132
|
|
|
* Se estiver vazio, utiliza os parâmetros padrão. |
133
|
|
|
* @param string[] $paramList |
134
|
|
|
*/ |
135
|
|
|
private function setParamList($paramList) { |
136
|
|
|
$paramDefaulf = [$this->homePage, 'index']; |
137
|
|
|
$this->paramList = array_replace($paramDefaulf, array_filter($paramList)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Retorna uma parte da URL |
142
|
|
|
* @param int $position Parte escolhida |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getParam($position) { |
146
|
|
|
return (key_exists($position, $this->paramList)) ? $this->paramList[$position] : ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Redireciona para a URL |
151
|
|
|
* @param string $url |
152
|
|
|
*/ |
153
|
|
|
public function redirect($url = '') { |
154
|
|
|
Url::instance()->redirect($url); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Atualiza a mesma página |
159
|
|
|
* @param string $url |
160
|
|
|
*/ |
161
|
|
|
public function refresh() { |
162
|
|
|
Url::instance()->redirect($this->getUrl()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** Define a página como 404 */ |
166
|
|
|
public function pageNotFound() { |
167
|
|
|
$this->errorPage(404); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Define a página atual como algum erro |
172
|
|
|
* @param int $errorCode [401, 404, 500, etc] |
173
|
|
|
*/ |
174
|
|
|
public function errorPage($errorCode) { |
175
|
|
|
ErrorPage::setError($errorCode); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|