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