-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.php
86 lines (73 loc) · 2.08 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Exception returned if one of reserved properties is being set/unset
*/
class Controller_Exception extends Exception {}
class Controller
{
protected $_request = null;
protected $_response = null;
protected $view = null;
/**
* Init the controller class
*
* The constructor must init the view
*
* @param Request $request
*/
public function __construct(Request $request, Response $response)
{
$config = Config::getInstance();
$controllerName = $request->getControllerName();
$actionName = $request->getActionName();
$viewExtension = $config->getOption('view/extension');
$this->_request = $request;
$this->_response = $response;
$this->view = new View($controllerName.DIRECTORY_SEPARATOR.$actionName . $viewExtension);
}
/**
* Init function called just after the constructor
*/
public function init() {}
public function getRequest()
{
return $this->_request;
}
public function getView()
{
return $this->view;
}
/**
* Ensure that the user cannot set value of our reserved properties
*
* @param string $name
* @param string $value
* @throws Controller_Exception
*/
public function __set($name, $value)
{
if ($name[0] == '_')
throw new Controller_Exception('trying to set reserved property');
$this->$name = $value;
}
/**
* Ensure that the user cannot unset our reserved properties
*
* @param string $name
* @throws Controller_Exception
*/
public function __unset($name)
{
if ($name[0] == '_')
throw new Controller_Exception('trying to unset reserved property');
unset($this->$name);
}
/**
* Function called after the action, it's main purpose is to render
* the view into the response's content
*/
public function render()
{
$this->view->render($this->_response);
}
}