Passed
Push — master ( ee8b1c...c1d291 )
by Jean-Christophe
02:45
created

Controller::finalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 1
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Ubiquity framework
5
 *
6
 */
7
namespace Ubiquity\controllers;
8
9
use Ubiquity\views\View;
10
use Ubiquity\exceptions\RouterException;
11
12
/**
13
 * Base class for controllers
14
 *
15
 * @author jcheron
16
 * @version 1.0.3
17
 */
18
abstract class Controller {
19
	/**
20
	 * The view
21
	 *
22
	 * @var View
23
	 */
24
	protected $view;
25
26
	/**
27
	 * Default action
28
	 */
29
	abstract public function index();
30
31
	/**
32
	 * Constructor
33
	 * initialize $view variable
34
	 */
35 6
	public function __construct() {
36 6
		$this->view = new View ();
37 6
	}
38
39
	/**
40
	 * Method called before each action
41
	 * Can be override in derived class
42
	 */
43 5
	public function initialize() {
44 5
	}
45
46
	/**
47
	 * Method called after each action
48
	 * Can be override in derived class
49
	 */
50 5
	public function finalize() {
51 5
	}
52
53
	/**
54
	 * Loads the view $viewName possibly passing the variables $pdata
55
	 *
56
	 * @param string $viewName
57
	 *        	view name to load
58
	 * @param mixed $pData
59
	 *        	Variable or associative array to pass to the view <br> If a variable is passed, it will have the name <b> $ data </ b> in the view, <br>
60
	 *        	If an associative array is passed, the view retrieves variables from the table's key names
61
	 * @param boolean $asString
62
	 *        	If true, the view is not displayed but returned as a string (usable in a variable)
63
	 * @throws \Exception
64
	 * @return string
65
	 */
66
	public function loadView($viewName, $pData = NULL, $asString = false) {
67
		if (isset ( $pData ))
68
			$this->view->setVars ( $pData );
69
		return $this->view->render ( $viewName, $asString );
70
	}
71
72
	/**
73
	 * Loads the default view (controllerName/actionName) possibly passing the variables $pdata
74
	 *
75
	 * @param mixed $pData
76
	 *        	Variable or associative array to pass to the view <br> If a variable is passed, it will have the name <b> $ data </ b> in the view, <br>
77
	 *        	If an associative array is passed, the view retrieves variables from the table's key names
78
	 * @param boolean $asString
79
	 *        	If true, the view is not displayed but returned as a string (usable in a variable)
80
	 * @throws \Exception
81
	 * @return string
82
	 */
83
	public function loadDefaultView($pData = NULL, $asString = false) {
84
		return $this->loadView ( $this->getDefaultViewName (), $pData, $asString );
85
	}
86
87
	/**
88
	 * Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName
89
	 *
90
	 * @return string the default view name
91
	 */
92
	public function getDefaultViewName() {
93
		return Startup::getControllerSimpleName () . "/" . Startup::getAction () . "." . Startup::getViewNameFileExtension ();
94
	}
95
96
	/**
97
	 * Returns True if access to the controller is allowed
98
	 * To be override in sub classes
99
	 *
100
	 * @param string $action
101
	 * @return boolean
102
	 */
103 5
	public function isValid($action) {
104 5
		return true;
105
	}
106
107
	/**
108
	 * Called if isValid () returns false <br>
109
	 * To be override in sub classes
110
	 */
111 4
	public function onInvalidControl() {
112 4
		if (! headers_sent ())
113 4
			\header ( 'HTTP/1.1 401 Unauthorized', true, 401 );
114 4
	}
115
116
	/**
117
	 * Loads the controller $controller and calls its $action method by passing the parameters $params
118
	 *
119
	 * @param string $controller
120
	 *        	The Controller
121
	 * @param string $action
122
	 *        	The action to call
123
	 * @param mixed $params
124
	 *        	Parameters passed to the $action method
125
	 * @param boolean $initialize
126
	 *        	If true, the controller's initialize method is called before $action
127
	 * @param boolean $finalize
128
	 *        	If true, the controller's finalize method is called after $action
129
	 * @throws \Exception
130
	 */
131
	public function forward($controller, $action = "index", $params = array(), $initialize = false, $finalize = false) {
132
		$u = array ($controller,$action );
133
		if (\is_array ( $params )) {
134
			$u = \array_merge ( $u, $params );
135
		} else {
136
			$u = \array_merge ( $u, [ $params ] );
137
		}
138
		Startup::runAction ( $u, $initialize, $finalize );
139
	}
140
141
	/**
142
	 * Redirect to a route by its name
143
	 *
144
	 * @param string $routeName
145
	 * @param array $parameters
146
	 * @param boolean $initialize
147
	 * @param boolean $finalize
148
	 * @throws RouterException
149
	 */
150
	public function redirectToRoute($routeName, $parameters = [], $initialize = false, $finalize = false) {
151
		$path = Router::getRouteByName ( $routeName, $parameters );
152
		if ($path !== false) {
153
			$route = Router::getRoute ( $path, false );
154
			if ($route !== false) {
155
				$this->forward ( $route [0], $route [1], \array_slice ( $route, 2 ), $initialize, $finalize );
156
			} else {
157
				throw new RouterException ( "Route {$routeName} not found", 404 );
158
			}
159
		} else {
160
			throw new RouterException ( "Route {$routeName} not found", 404 );
161
		}
162
	}
163
164
	/**
165
	 *
166
	 * @return \Ubiquity\views\View
167
	 */
168
	public function getView() {
169
		return $this->view;
170
	}
171
}
172