1
|
|
|
<?php /** MicroController */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Mvc\Controllers; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Base\IContainer; |
7
|
|
|
use Micro\Mvc\Module; |
8
|
|
|
use Micro\Web\IResponse; |
9
|
|
|
use Micro\Web\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Controller |
13
|
|
|
* |
14
|
|
|
* @author Oleg Lunegov <[email protected]> |
15
|
|
|
* @link https://github.com/lugnsk/micro |
16
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
17
|
|
|
* @license /LICENSE |
18
|
|
|
* @package Micro |
19
|
|
|
* @subpackage Mvc\Controllers |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @since 1.0 |
22
|
|
|
* @abstract |
23
|
|
|
*/ |
24
|
|
|
abstract class Controller implements IController |
25
|
|
|
{ |
26
|
|
|
/** @var Module $module */ |
27
|
|
|
public $module; |
28
|
|
|
/** @var IResponse $response Response HTTP data */ |
29
|
|
|
public $response; |
30
|
|
|
/** @var IContainer $container */ |
31
|
|
|
protected $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor controller |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* |
38
|
|
|
* @param IContainer $container |
39
|
|
|
* @param string $modules |
40
|
|
|
* |
41
|
|
|
* @result void |
42
|
|
|
*/ |
43
|
|
|
public function __construct(IContainer $container, $modules = '') |
44
|
|
|
{ |
45
|
|
|
$this->container = $container; |
46
|
|
|
|
47
|
|
|
// if module defined |
48
|
|
|
if ($modules) { |
49
|
|
|
$app = $this->container->kernel->getAppDir(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$path = $app . str_replace('\\', '/', $modules) . '/' . |
52
|
|
|
ucfirst(basename(str_replace('\\', '/', $modules))) . 'Module.php'; |
53
|
|
|
|
54
|
|
|
// search module class |
55
|
|
|
if (file_exists($path)) { |
56
|
|
|
$path = substr(str_replace('/', '\\', str_replace($app, 'App', $path)), 0, -4); |
57
|
|
|
$this->module = new $path($this->container); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$this->response = $this->container->response) { |
|
|
|
|
62
|
|
|
$this->response = new Response; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Apply filters |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* |
71
|
|
|
* @param string $action current action name |
72
|
|
|
* @param bool $isPre is pre or post |
73
|
|
|
* @param array $filters defined filters |
74
|
|
|
* @param string $data data to parse |
75
|
|
|
* |
76
|
|
|
* @return null|string |
77
|
|
|
* @throws Exception |
78
|
|
|
*/ |
79
|
|
|
public function applyFilters($action, $isPre = true, array $filters = [], $data = null) |
80
|
|
|
{ |
81
|
|
|
if (!$filters) { |
|
|
|
|
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ($filters AS $filter) { |
86
|
|
|
if (empty($filter['class']) || !class_exists($filter['class'])) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (empty($filter['actions']) || !in_array($action, $filter['actions'], true)) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @var \Micro\Filter\IFilter $_filter */ |
95
|
|
|
$_filter = new $filter['class']($action, $this->container); |
96
|
|
|
|
97
|
|
|
$res = $isPre ? $_filter->pre($filter) : $_filter->post($filter + ['data' => $data]); |
98
|
|
|
if (!$res) { |
99
|
|
|
if (!empty($_filter->result['redirect'])) { |
|
|
|
|
100
|
|
|
header('Location: ' . $_filter->result['redirect']); |
|
|
|
|
101
|
|
|
|
102
|
|
|
die(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
throw new Exception($_filter->result['message']); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
$data = $res; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get action class by name |
114
|
|
|
* |
115
|
|
|
* @access public |
116
|
|
|
* |
117
|
|
|
* @param string $name action name |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function getActionClassByName($name) |
122
|
|
|
{ |
123
|
|
|
if (method_exists($this, 'actions')) { |
124
|
|
|
$actions = $this->actions(); |
|
|
|
|
125
|
|
|
|
126
|
|
|
if (!empty($actions[$name]) && class_exists($actions[$name])) { |
127
|
|
|
return $actions[$name]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: