|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
|
4
|
|
|
|
|
5
|
|
|
use Win\Request\Url; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Controllers |
|
9
|
|
|
* |
|
10
|
|
|
* São responsáveis por processar as requisições e definir as Views |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Controller { |
|
13
|
|
|
|
|
14
|
|
|
public static $dir = '/app/controller'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Ponteiro para Aplicação Principal |
|
18
|
|
|
* @var Application |
|
19
|
|
|
*/ |
|
20
|
|
|
public $app; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $action; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
public $layout = 'main'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Variáveis para serem usadas no arquivo da View |
|
30
|
|
|
* @var mixed[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $data = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Cria o Controller, definindo o Action |
|
36
|
|
|
* @param string $action |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($action = '') { |
|
39
|
|
|
$this->app = Application::app(); |
|
40
|
|
|
$this->setAction($action); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** @param string $title */ |
|
44
|
|
|
public function setTitle($title) { |
|
45
|
|
|
$this->addData('title', $title); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Adiciona uma variável para usar na View |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param mixed $value |
|
52
|
|
|
*/ |
|
53
|
|
|
public function addData($name, $value) { |
|
54
|
|
|
$this->data[$name] = $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Retorna uma variável do Controller |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* @return mixed|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getData($name) { |
|
63
|
|
|
if (key_exists($name, $this->data)) { |
|
64
|
|
|
return $this->data[$name]; |
|
65
|
|
|
} |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Define o Action |
|
71
|
|
|
* @param string $action |
|
72
|
|
|
*/ |
|
73
|
|
|
private function setAction($action = '') { |
|
74
|
|
|
if (empty($action)) { |
|
75
|
|
|
$action = $this->app->getParam(1); |
|
76
|
|
|
} |
|
77
|
|
|
$this->action = $this->toCamelCase($action); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retorna o nome do Action em camelCase |
|
82
|
|
|
* @param string $action |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private function toCamelCase($action) { |
|
86
|
|
|
if (strpos($action, '-') !== false) { |
|
87
|
|
|
$camelCaseName = str_replace(' ', '', ucwords(str_replace('-', ' ', $action))); |
|
88
|
|
|
$camelCaseName[0] = strtolower($camelCaseName[0]); |
|
89
|
|
|
$action = $camelCaseName; |
|
90
|
|
|
} |
|
91
|
|
|
return $action; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** @return string */ |
|
95
|
|
|
public function getAction() { |
|
96
|
|
|
return $this->action; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Carrega o Controller, |
|
101
|
|
|
* executando o Action atual |
|
102
|
|
|
*/ |
|
103
|
|
|
public function load() { |
|
104
|
|
|
$this->init(); |
|
105
|
|
|
$action = $this->action; |
|
106
|
|
|
$view = $this->$action(); |
|
107
|
|
|
if ($view instanceof View && !$this->app->isErrorPage()): |
|
108
|
|
|
$this->app->view = $view; |
|
109
|
|
|
endif; |
|
110
|
|
|
|
|
111
|
|
|
if (!$this->app->isErrorPage()): |
|
112
|
|
|
$this->app->view->mergeData($this->data); |
|
113
|
|
|
$this->app->view->validate(); |
|
114
|
|
|
endif; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function reload() { |
|
118
|
|
|
$this->init(); |
|
119
|
|
|
$this->index(); |
|
120
|
|
|
$this->app->view->mergeData($this->data); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Volta para o método index da pagina atual |
|
125
|
|
|
* @codeCoverageIgnore |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function backToIndex() { |
|
128
|
|
|
if (!$this->app->isErrorPage()): |
|
129
|
|
|
Url::instance()->redirect($this->app->getPage()); |
|
130
|
|
|
endif; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Action Index |
|
135
|
|
|
*/ |
|
136
|
|
|
abstract public function index(); |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Este método é chamado sempre que o Controller é carregado |
|
140
|
|
|
*/ |
|
141
|
|
|
abstract protected function init(); |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Redireciona para a URL |
|
145
|
|
|
* @param string $url |
|
146
|
|
|
*/ |
|
147
|
|
|
public function redirect($url) { |
|
148
|
|
|
Url::instance()->redirect($url); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Atualiza a mesma página |
|
153
|
|
|
* @param string $url |
|
154
|
|
|
*/ |
|
155
|
|
|
public function refresh() { |
|
156
|
|
|
Url::instance()->redirect(Url::instance()->getUrl()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Evita chamada de um método que não exista |
|
161
|
|
|
* @param string $name |
|
162
|
|
|
* @param mixed[] $arguments |
|
163
|
|
|
* @return boolean |
|
164
|
|
|
*/ |
|
165
|
|
|
public function __call($name, $arguments) { |
|
166
|
|
|
$this->app->pageNotFound(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|