|
1
|
|
|
<?php /** MicroController */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Mvc\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
|
6
|
|
|
use Micro\Mvc\Module; |
|
7
|
|
|
use Micro\Web\ResponseInjector; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Controller |
|
12
|
|
|
* |
|
13
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
14
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
15
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
|
16
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
|
17
|
|
|
* @package Micro |
|
18
|
|
|
* @subpackage Mvc\Controllers |
|
19
|
|
|
* @version 1.0 |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
* @abstract |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class Controller implements IController |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var Module $module */ |
|
26
|
|
|
public $module; |
|
27
|
|
|
/** @var ResponseInterface $response Response HTTP data */ |
|
28
|
|
|
public $response; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor controller |
|
33
|
|
|
* |
|
34
|
|
|
* @access public |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $modules |
|
37
|
|
|
* |
|
38
|
|
|
* @result void |
|
39
|
|
|
* @throws Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($modules = '') |
|
42
|
|
|
{ |
|
43
|
|
|
// if module defined |
|
44
|
|
|
if ($modules) { |
|
45
|
|
|
$className = '\\App'.$modules.'\\'.ucfirst(basename(str_replace('\\', '/', $modules))).'Module'; |
|
46
|
|
|
|
|
47
|
|
|
if (class_exists($className) && is_subclass_of($className, '\Micro\Mvc\Module')) { |
|
48
|
|
|
$this->module = new $className(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!$this->response = (new ResponseInjector)->build()) { |
|
53
|
|
|
throw new Exception('Component `response` not configured'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Apply filters |
|
59
|
|
|
* |
|
60
|
|
|
* @access public |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $action current action name |
|
63
|
|
|
* @param bool $isPre is pre or post |
|
64
|
|
|
* @param array $filters defined filters |
|
65
|
|
|
* @param string $data data to parse |
|
66
|
|
|
* |
|
67
|
|
|
* @return null|string |
|
68
|
|
|
* @throws Exception|\InvalidArgumentException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function applyFilters($action, $isPre = true, array $filters = [], $data = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$filters) { |
|
|
|
|
|
|
73
|
|
|
return $data; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
foreach ($filters as $filter) { |
|
77
|
|
|
if (empty($filter['class']) || !class_exists($filter['class'])) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (empty($filter['actions']) || !in_array($action, $filter['actions'], true)) { |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @var \Micro\Filter\IFilter $_filter */ |
|
86
|
|
|
$_filter = new $filter['class']($action); |
|
87
|
|
|
/** @var ResponseInterface $response */ |
|
88
|
|
|
$response = $isPre ? $_filter->pre($filter) : $_filter->post($filter + ['data' => $data]); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
if (!$response) { |
|
91
|
|
|
if (!empty($_filter->result['redirect'])) { |
|
|
|
|
|
|
92
|
|
|
/** @var ResponseInterface $redirect */ |
|
93
|
|
|
$redirect = (new ResponseInjector)->build(); |
|
94
|
|
|
return $redirect->withHeader('Location', $_filter->result['redirect']); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
throw new Exception($_filter->result['message']); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
|
99
|
|
|
$data = $response; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $data; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get action class by name |
|
107
|
|
|
* |
|
108
|
|
|
* @access public |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name action name |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getActionClassByName($name) |
|
115
|
|
|
{ |
|
116
|
|
|
if (method_exists($this, 'actions')) { |
|
117
|
|
|
$actions = $this->actions(); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
if ( |
|
120
|
|
|
!empty($actions[$name]) && |
|
121
|
|
|
class_exists($actions[$name]) && |
|
122
|
|
|
is_subclass_of($actions[$name], '\Micro\Mvc\Action') |
|
123
|
|
|
) { |
|
124
|
|
|
return $actions[$name]; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.