1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Controller |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Controller; |
10
|
|
|
|
11
|
|
|
use \Pimf\Param, \Pimf\Config, \Pimf\Sapi, |
12
|
|
|
\Pimf\Controller\Exception as Bomb, |
13
|
|
|
\Pimf\Request, \Pimf\Util\Header, \Pimf\Url, |
14
|
|
|
\Pimf\Response, \Pimf\EntityManager, \Pimf\Logger, |
15
|
|
|
\Pimf\Util\Value, \Pimf\Environment, \Pimf\Router; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Defines the general controller behaviour - you have to extend it. |
19
|
|
|
* |
20
|
|
|
* @package Controller |
21
|
|
|
* @author Gjero Krsteski <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class Base |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Request |
27
|
|
|
*/ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Response |
32
|
|
|
*/ |
33
|
|
|
protected $response; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Logger |
37
|
|
|
*/ |
38
|
|
|
protected $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var EntityManager |
42
|
|
|
*/ |
43
|
|
|
protected $em; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Router |
47
|
|
|
*/ |
48
|
|
|
protected $router; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Environment |
52
|
|
|
*/ |
53
|
|
|
protected $env; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Request $request |
57
|
|
|
* @param Response $response |
58
|
|
|
* @param Logger $logger |
59
|
|
|
* @param EntityManager $em |
60
|
|
|
* @param Router $router |
61
|
|
|
* @param Environment $env |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
Request $request, |
65
|
|
|
Response $response = null, |
66
|
|
|
Logger $logger, |
67
|
|
|
$em, |
68
|
|
|
Router $router, |
69
|
|
|
Environment $env |
70
|
|
|
) { |
71
|
|
|
$this->request = $request; |
72
|
|
|
$this->response = $response; |
73
|
|
|
$this->logger = $logger; |
74
|
|
|
$this->em = $em; |
75
|
|
|
$this->router = $router; |
76
|
|
|
$this->env = $env; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
abstract public function indexAction(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Method to show the content. |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
* @throws \Exception If not supported request method or bad controller |
86
|
|
|
*/ |
87
|
|
|
public function render() |
88
|
|
|
{ |
89
|
|
|
if (Sapi::isCli() && Config::get('environment') == 'production') { |
90
|
|
|
$suffix = 'CliAction'; |
91
|
|
|
$action = $this->request->fromCli()->get('action', 'index'); |
92
|
|
|
} else { |
93
|
|
|
|
94
|
|
|
$suffix = 'Action'; |
95
|
|
|
|
96
|
|
|
if ($this->request->getMethod() != 'GET' && $this->request->getMethod() != 'POST') { |
97
|
|
|
|
98
|
|
|
$redirectUrl = new Value($this->env->REDIRECT_URL); |
99
|
|
|
$redirectUrl = $redirectUrl->deleteLeading('/')->deleteTrailing('/')->explode('/'); |
100
|
|
|
$action = isset($redirectUrl[1]) ? $redirectUrl[1] : 'index'; |
101
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$bag = sprintf('from%s', ucfirst(strtolower($this->request->getMethod()))); |
104
|
|
|
$action = $this->request->{$bag}()->get('action', 'index'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (Config::get('app.routeable') === true && $this->router instanceof \Pimf\Router) { |
108
|
|
|
|
109
|
|
|
$target = $this->router->find(); |
110
|
|
|
|
111
|
|
|
if ($target instanceof \Pimf\Route\Target) { |
112
|
|
|
|
113
|
|
|
$action = $target->getAction(); |
114
|
|
|
|
115
|
|
|
Request::$getData = new Param( |
116
|
|
|
array_merge($target->getParams(), Request::$getData->getAll()) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$action = strtolower($action) . $suffix; |
123
|
|
|
|
124
|
|
|
if (method_exists($this, 'init')) { |
125
|
|
|
call_user_func(array($this, 'init')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!method_exists($this, $action)) { |
129
|
|
|
throw new Bomb("no action '{$action}' defined at controller " . get_class($this)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return call_user_func(array($this, $action)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Prepares the response object to return an HTTP Redirect response to the client. |
137
|
|
|
* |
138
|
|
|
* @param string $route The redirect destination like controller/action |
139
|
|
|
* @param boolean $permanent If permanent redirection or not. |
140
|
|
|
* @param boolean $exit |
141
|
|
|
*/ |
142
|
|
|
public function redirect($route, $permanent = false, $exit = true) |
143
|
|
|
{ |
144
|
|
|
$url = Url::compute($route); |
145
|
|
|
|
146
|
|
|
($permanent === true) ? Header::sendMovedPermanently() : Header::sendFound(); |
147
|
|
|
|
148
|
|
|
Header::toLocation($url, $exit); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|