1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Controller Manager |
5
|
|
|
* |
6
|
|
|
* @category core |
7
|
|
|
* @package core\Controller |
8
|
|
|
* @author Judicaël Paquet <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
10
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
11
|
|
|
* @version Release: 2.0.0 |
12
|
|
|
* @filesource https://github.com/las93/venus2 |
13
|
|
|
* @link https://github.com/las93 |
14
|
|
|
* @since 2.0.0 |
15
|
|
|
*/ |
16
|
|
|
namespace Venus\core; |
17
|
|
|
|
18
|
|
|
use \Venus\core\Router as Router; |
19
|
|
|
use \Venus\core\Security as Security; |
20
|
|
|
use \Venus\core\UrlManager as UrlManager; |
21
|
|
|
use \Venus\lib\I18n as I18n; |
22
|
|
|
use \Venus\lib\Vendor as Vendor; |
23
|
|
|
use \Venus\lib\Form as Form; |
24
|
|
|
use \Venus\lib\Mail as Mail; |
25
|
|
|
use \Venus\lib\Session as Session; |
26
|
|
|
use \Venus\lib\Cookie as Cookie; |
27
|
|
|
use \Venus\lib\Di as Di; |
28
|
|
|
use \Venus\lib\Request as Request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Controller Manager |
32
|
|
|
* |
33
|
|
|
* @category core |
34
|
|
|
* @package core\Controller |
35
|
|
|
* @author Judicaël Paquet <[email protected]> |
36
|
|
|
* @copyright Copyright (c) 2013-2016 PAQUET Judicaël FR Inc. (https://github.com/las93) |
37
|
|
|
* @license https://github.com/las93/venus3/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
38
|
|
|
* @version Release: 3.0.0 |
39
|
|
|
* @filesource https://github.com/las93/venus3 |
40
|
|
|
* @link https://github.com/las93 |
41
|
|
|
* @since 3.0.0 |
42
|
|
|
*/ |
43
|
|
|
abstract class Controller extends Mother |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Cache to know if a model was initialize or not because we must initialize it just one time by script |
48
|
|
|
* |
49
|
|
|
* @access private |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private static $_aInitialize = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
* @return object |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$aClass = explode('\\', get_called_class()); |
63
|
|
|
$sClassName = $aClass[count($aClass) - 1]; |
64
|
|
|
$sNamespaceName = preg_replace('/\\\\'.$sClassName.'$/', '', get_called_class()); |
65
|
|
|
|
66
|
|
|
if (isset($sClassName)) { |
67
|
|
|
|
68
|
|
|
$sNamespaceBaseName = str_replace('\Controller', '', $sNamespaceName); |
69
|
|
|
$sDefaultModel = $sNamespaceBaseName.'\Model\\'.$sClassName; |
70
|
|
|
$sDefaultView = str_replace('\\', DIRECTORY_SEPARATOR, str_replace('Venus\\', '\\', $sNamespaceBaseName)).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'View'.DIRECTORY_SEPARATOR.$sClassName.'.tpl'; |
71
|
|
|
$sDefaultLayout = str_replace('\\', DIRECTORY_SEPARATOR, str_replace('Venus\\', '\\', $sNamespaceBaseName)).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'View'.DIRECTORY_SEPARATOR.'Layout.tpl'; |
72
|
|
|
|
73
|
|
|
$this->model = function() use ($sDefaultModel) { return new $sDefaultModel; }; |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->view = function() use ($sDefaultView) { return Vendor::getVendor('Apollina\Template', $sDefaultView); }; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$this->layout = function() use ($sDefaultLayout) { return Vendor::getVendor('Apollina\Template', $sDefaultLayout, true); }; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->layout->assign('model', $sDefaultView); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->form = function() { return new Form(); }; |
|
|
|
|
83
|
|
|
$this->security = function() { return new Security(); }; |
|
|
|
|
84
|
|
|
$this->router = function() { return new Router; }; |
|
|
|
|
85
|
|
|
$this->mail = function() { return new Mail; }; |
|
|
|
|
86
|
|
|
$this->session = function() { return new Session; }; |
|
|
|
|
87
|
|
|
$this->translator = function() { return new I18n; }; |
|
|
|
|
88
|
|
|
$this->url = function() { return new UrlManager; }; |
|
|
|
|
89
|
|
|
$this->cookie = function() { return new Cookie; }; |
|
|
|
|
90
|
|
|
$this->di = function() { return new Di; }; |
|
|
|
|
91
|
|
|
$this->request = function() { return new Request; }; |
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Trigger on a model to initialize it. You could fill entity with it. |
95
|
|
|
*/ |
96
|
|
|
if (method_exists(get_called_class(), 'initialize')) { |
97
|
|
|
|
98
|
|
|
if (!isset(self::$_aInitialize[get_called_class()])) { |
99
|
|
|
|
100
|
|
|
static::initialize(); |
|
|
|
|
101
|
|
|
self::$_aInitialize[get_called_class()] = true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Trigger on a model to initialize it every time you construct it |
107
|
|
|
*/ |
108
|
|
|
if (method_exists(get_called_class(), 'onConstruct')) { static::onConstruct(); } |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* redirection HTTP |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
* @param string $sUrl url for the redirection |
116
|
|
|
* @param int $iHttpCode code of the http request |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function redirect(string $sUrl, int $iHttpCode = 301) |
120
|
|
|
{ |
121
|
|
|
if ($iHttpCode === 301) { header('Status: 301 Moved Permanently', false, 301); } |
122
|
|
|
else if ($iHttpCode === 302) { header('Status: Moved Temporarily', false, 301); } |
123
|
|
|
|
124
|
|
|
header('Location: '.$sUrl); |
125
|
|
|
exit; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* call an other action as you call action with URL/Cli |
130
|
|
|
* |
131
|
|
|
* @access public |
132
|
|
|
* @param string $sUri uri for the redirection |
133
|
|
|
* @param array $aParams parameters |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function forward(string $sUri, array $aParams = array()) |
137
|
|
|
{ |
138
|
|
|
$this->router->runByFoward($sUri, $aParams); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* call the 404 Not Found page |
143
|
|
|
* |
144
|
|
|
* @access public |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function notFound() |
148
|
|
|
{ |
149
|
|
|
$$this->router->runHttpErrorPage(404); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* call the 403 Forbidden page |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function forbidden() |
159
|
|
|
{ |
160
|
|
|
$$this->router->runHttpErrorPage(403); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* get a property |
165
|
|
|
* |
166
|
|
|
* @access public |
167
|
|
|
* @param unknown_type $mKey |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function __get($mKey) |
171
|
|
|
{ |
172
|
|
|
if (isset($this->di) && property_exists($this, 'di')) { |
173
|
|
|
|
174
|
|
|
$mDi = $this->di->get($mKey); |
|
|
|
|
175
|
|
|
|
176
|
|
|
if (isset($mDi) && $mDi !== false) { return $mDi; } |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return parent::__get($mKey); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.