|
1
|
|
|
<?php /** MicroController */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Mvc\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
|
6
|
|
|
use Micro\Web\ResponseInjector; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Controller |
|
11
|
|
|
* |
|
12
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
13
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
14
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
|
15
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
|
16
|
|
|
* @package Micro |
|
17
|
|
|
* @subpackage Mvc\Controllers |
|
18
|
|
|
* @version 1.0 |
|
19
|
|
|
* @since 1.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class ViewController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var string $layout */ |
|
24
|
|
|
public $layout; |
|
25
|
|
|
/** @var bool $asWidget */ |
|
26
|
|
|
public $asWidget = false; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
* @throws Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public function action($name = 'index') |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
// Set widgetStack for widgets |
|
36
|
|
|
if (empty($GLOBALS['widgetStack'])) { |
|
37
|
|
|
$GLOBALS['widgetStack'] = []; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$actionClass = false; |
|
41
|
|
|
|
|
42
|
|
|
if (!method_exists($this, 'action'.ucfirst($name))) { |
|
43
|
|
|
$actionClass = $this->getActionClassByName($name); |
|
44
|
|
|
|
|
45
|
|
|
if (!$actionClass) { |
|
46
|
|
|
throw new Exception('Action `'.$name.'` not found into '.get_class($this)); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$filters = method_exists($this, 'filters') ? $this->filters() : []; |
|
|
|
|
|
|
51
|
|
|
$result = $this->applyFilters($name, true, $filters, null); |
|
52
|
|
|
|
|
53
|
|
|
if ($result instanceof ResponseInterface) { |
|
54
|
|
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
if ($actionClass) { |
|
|
|
|
|
|
58
|
|
|
/** @var \Micro\mvc\Action $cl */ |
|
59
|
|
|
$cl = new $actionClass(); |
|
60
|
|
|
$view = $cl->run(); |
|
61
|
|
|
} else { |
|
62
|
|
|
$view = $this->{'action'.ucfirst($name)}(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (is_object($view)) { |
|
66
|
|
|
$view->module = get_class($this->module); |
|
67
|
|
|
$view->layout = $view->layout ?: $this->layout; |
|
68
|
|
|
$view->view = $view->view ?: $name; |
|
69
|
|
|
$view->path = get_called_class(); |
|
70
|
|
|
$view = $view->render(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$response = (new ResponseInjector)->build(); |
|
74
|
|
|
if (!$response) { |
|
75
|
|
|
throw new Exception('Component `response` not configured'); |
|
76
|
|
|
} |
|
77
|
|
|
$stream = $response->getBody(); |
|
78
|
|
|
$stream->write($this->applyFilters($name, false, $filters, $view)); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return $response->withBody($stream); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Redirect user to path |
|
85
|
|
|
* |
|
86
|
|
|
* @access public |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $path path to redirect |
|
89
|
|
|
* @param integer $status status for redirect |
|
90
|
|
|
* |
|
91
|
|
|
* @return false|ResponseInterface |
|
92
|
|
|
* @throws Exception|\InvalidArgumentException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function redirect($path, $status = 301) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$this->asWidget) { |
|
97
|
|
|
/** @var ResponseInterface $response */ |
|
98
|
|
|
$response = (new ResponseInjector)->build(); |
|
99
|
|
|
if (!$response) { |
|
100
|
|
|
throw new Exception('Component `response` not configured'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$response = $response->withStatus($status); |
|
104
|
|
|
$response = $response->getHeaderLine('Location: '.$path); |
|
105
|
|
|
|
|
106
|
|
|
return $response; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: