Completed
Push — master ( b15437...c8ccf3 )
by Wanderson
02:00
created

Controller::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
	/** @var mixed[] Array variaveis para usar na View */
29
	private $data = [];
30
31
	/**
32
	 * Cria o Controller, definindo o action
33
	 */
34
	public function __construct($action = '') {
35
		$this->app = Application::app();
36
		$this->setAction($action);
37
	}
38
39
	/**
40
	 * Adiciona uma variavel para usar na view
41
	 * @param string $name
42
	 * @param mixed $value
43
	 */
44
	public function addData($name, $value) {
45
		$this->data[$name] = $value;
46
	}
47
48
	/**
49
	 * Retorna uma variavel do controller
50
	 * @param string $name
51
	 * @return mixed|null
52
	 */
53
	public function getData($name) {
54
		if (key_exists($name, $this->data)) {
55
			return $this->data[$name];
56
		}
57
		return null;
58
	}
59
60
	/**
61
	 * Define o action
62
	 * @param string $action
63
	 */
64
	private function setAction($action = '') {
65
		if (empty($action)) {
66
			$action = $this->app->getParam(1);
67
		}
68
		$this->action = $this->toCamelCase($action);
69
	}
70
71
	/**
72
	 * Retorna o nome do action em camelCase
73
	 * @param string $action
74
	 * @return string
75
	 */
76
	private function toCamelCase($action) {
77
		if (strpos($action, '-') !== false) {
78
			$camelCaseName = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));
79
			$camelCaseName[0] = strtolower($camelCaseName[0]);
80
			$action = $camelCaseName;
81
		}
82
		return $action;
83
	}
84
85
	/** @return string */
86
	public function getAction() {
87
		return $this->action;
88
	}
89
90
	/**
91
	 * Carrega o controller,
92
	 * executando o action atual
93
	 */
94
	public function load() {
95
		$this->init();
96
		$action = $this->action;
97
		$view = $this->$action();
98
99
		if ($view instanceof View && !$this->app->isErrorPage()):
100
			$this->app->view = $view;
101
		endif;
102
103
		$this->app->view->mergeData($this->data);
104
		$this->app->view->validate();
105
	}
106
107
	public function reload() {
108
		$this->init();
109
		$this->index();
110
	}
111
112
	/**
113
	 * Volta para o método index da pagina atual
114
	 */
115
	protected function backToIndex() {
116
		if (!$this->app->isErrorPage()):
117
			Url::instance()->redirect($this->app->getPage());
118
		endif;
119
	}
120
121
	/**
122
	 * Action Index
123
	 */
124
	abstract public function index();
125
126
	/**
127
	 * Este metodo é chamado sempre que o controller é carregado
128
	 */
129
	protected function init() {
130
		
131
	}
132
133
	/**
134
	 * Evita chamada de um metodo que nao existe
135
	 * @param string $name
136
	 * @param mixed[] $arguments
137
	 * @return boolean
138
	 */
139
	public function __call($name, $arguments) {
140
		$this->app->pageNotFound();
141
	}
142
143
}
144