1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
4
|
|
|
|
5
|
|
|
use Win\Helper\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
|
|
|
* Cria o Controller, definindo o action |
30
|
|
|
*/ |
31
|
|
|
public function __construct($action = '') { |
32
|
|
|
$this->app = Application::app(); |
33
|
|
|
$this->setAction($action); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Define o action |
38
|
|
|
* @param string $action |
39
|
|
|
*/ |
40
|
|
|
private function setAction($action = '') { |
41
|
|
|
if (empty($action)) { |
42
|
|
|
$action = $this->app->getParam(1); |
43
|
|
|
} |
44
|
|
|
$this->action = $this->toCamelCase($action); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retorna o nome do action em camelCase |
49
|
|
|
* @param string $action |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
private function toCamelCase($action) { |
53
|
|
|
if (strpos($action, '-') !== false) { |
54
|
|
|
$camelCaseName = str_replace(' ', '', ucwords(str_replace('-', ' ', $action))); |
55
|
|
|
$camelCaseName[0] = strtolower($camelCaseName[0]); |
56
|
|
|
$action = $camelCaseName; |
57
|
|
|
} |
58
|
|
|
return $action; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @return string */ |
62
|
|
|
public function getAction() { |
63
|
|
|
return $this->action; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Carrega o controller, |
68
|
|
|
* executando o action atual |
69
|
|
|
*/ |
70
|
|
|
public function load() { |
71
|
|
|
$this->init(); |
72
|
|
|
$action = $this->action; |
73
|
|
|
$view = $this->$action(); |
74
|
|
|
if ($view instanceof View): |
75
|
|
|
$this->app->view = $view; |
76
|
|
|
endif; |
77
|
|
|
$this->app->view->validate(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Volta para o método index da pagina atual |
82
|
|
|
*/ |
83
|
|
|
protected function backToIndex() { |
84
|
|
|
Url::instance()->redirect($this->app->getPage()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Action Index |
89
|
|
|
*/ |
90
|
|
|
abstract public function index(); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Este metodo é chamado sempre que o controller é carregado |
94
|
|
|
*/ |
95
|
|
|
protected function init() { |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Evita chamada de um metodo que nao existe |
101
|
|
|
* @param string $name |
102
|
|
|
* @param mixed[] $arguments |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function __call($name, $arguments) { |
106
|
|
|
$this->app->pageNotFound(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|