|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
use Vectorface\SnappyRouter\Controller\AbstractController; |
|
7
|
|
|
use Vectorface\SnappyRouter\Di\Di; |
|
8
|
|
|
use Vectorface\SnappyRouter\Di\DiProviderInterface; |
|
9
|
|
|
use Vectorface\SnappyRouter\Handler\AbstractHandler; |
|
10
|
|
|
use Vectorface\SnappyRouter\Request\AbstractRequest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The base class for all plugins. It is recommended to extend this class |
|
14
|
|
|
* instead of implementing the PluginInterface directly. |
|
15
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
16
|
|
|
* @author Dan Bruce <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractPlugin implements PluginInterface, DiProviderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** the default priority of a plugin */ |
|
21
|
|
|
const PRIORITY_DEFAULT = 1000; |
|
22
|
|
|
|
|
23
|
|
|
/** A string constant indicating the whitelist/blacklist applies to all |
|
24
|
|
|
actions within a controller */ |
|
25
|
|
|
const ALL_ACTIONS = 'all'; |
|
26
|
|
|
|
|
27
|
|
|
/** The plugin options */ |
|
28
|
|
|
protected $options; |
|
29
|
|
|
|
|
30
|
|
|
// properties for plugin/service compatibility |
|
31
|
|
|
// both properties cannot be set at the same time (one or the other or both |
|
32
|
|
|
// must be null at any point) |
|
33
|
|
|
private $whitelist; |
|
34
|
|
|
private $blacklist; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor for the plugin. |
|
38
|
|
|
* @param array $options The array of options. |
|
39
|
|
|
*/ |
|
40
|
19 |
|
public function __construct($options) |
|
41
|
|
|
{ |
|
42
|
19 |
|
$this->options = $options; |
|
43
|
19 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Invoked directly after the router decides which handler will be used. |
|
47
|
|
|
* @param AbstractHandler $handler The handler selected by the router. |
|
48
|
|
|
*/ |
|
49
|
13 |
|
public function afterHandlerSelected(AbstractHandler $handler) |
|
50
|
|
|
{ |
|
51
|
13 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Invoked after the entire route has been handled. |
|
55
|
|
|
* @param AbstractHandler $handler The handler selected by the router. |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function afterFullRouteInvoked(AbstractHandler $handler) |
|
58
|
|
|
{ |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Invoked if an exception is thrown during the route. |
|
63
|
|
|
* @param AbstractHandler $handler The handler selected by the router. |
|
64
|
|
|
* @param Exception $exception The exception that was thrown. |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function errorOccurred(AbstractHandler $handler, Exception $exception) |
|
67
|
|
|
{ |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns a sortable number for sorting plugins by execution priority. A lower number indicates |
|
72
|
|
|
* higher priority. |
|
73
|
|
|
* @return integer The execution priority (as a number). |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function getExecutionOrder() |
|
76
|
|
|
{ |
|
77
|
7 |
|
return self::PRIORITY_DEFAULT; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets the controller/action whitelist of this particular plugin. Note that |
|
82
|
|
|
* setting a whitelist will remove any previously set blacklists. |
|
83
|
|
|
* @param array $whitelist The controller/action whitelist. |
|
84
|
|
|
* @return AbstractPlugin Returns $this. |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function setWhitelist($whitelist) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->whitelist = $whitelist; |
|
89
|
2 |
|
$this->blacklist = null; |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Sets the controller/action blacklist of this particular plugin. Note that |
|
94
|
|
|
* setting a blacklist will remove any previously set whitelists. |
|
95
|
|
|
* @param array $blacklist The controller/action blacklist. |
|
96
|
|
|
* @return AbstractPlugin Returns $this. |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function setBlacklist($blacklist) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->whitelist = null; |
|
101
|
1 |
|
$this->blacklist = $blacklist; |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns whether or not the given controller and action requested should |
|
106
|
|
|
* invoke this plugin. |
|
107
|
|
|
* @param string $controller The requested controller. |
|
108
|
|
|
* @param string $action The requested action. |
|
109
|
|
|
* @return boolean Returns true if the given plugin is allowed to run against |
|
110
|
|
|
* this controller/action and false otherwise. |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function supportsControllerAndAction($controller, $action) |
|
113
|
|
|
{ |
|
114
|
2 |
|
if (null === $this->blacklist) { |
|
115
|
2 |
|
if (null === $this->whitelist) { |
|
116
|
|
|
// plugin has global scope |
|
117
|
1 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
// we use a whitelist so ensure the controller is in the whitelist |
|
120
|
2 |
|
if (!isset($this->whitelist[$controller])) { |
|
121
|
1 |
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
// the whitelisted controller could be an array of actions or it could |
|
124
|
|
|
// be mapped to the "all" string |
|
125
|
2 |
|
if (is_array($this->whitelist[$controller])) { |
|
126
|
1 |
|
return in_array($action, $this->whitelist[$controller]); |
|
127
|
|
|
} |
|
128
|
2 |
|
return (self::ALL_ACTIONS === (string)$this->whitelist[$controller]); |
|
129
|
|
|
} |
|
130
|
|
|
// if the controller isn't in the blacklist at all, we're good |
|
131
|
1 |
|
if (!isset($this->blacklist[$controller])) { |
|
132
|
1 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// if the controller is not an array we return false |
|
136
|
|
|
// otherwise we check if the action is listed in the array |
|
137
|
1 |
|
return is_array($this->blacklist[$controller]) && |
|
138
|
1 |
|
!in_array($action, $this->blacklist[$controller]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Retrieve an element from the DI container. |
|
143
|
|
|
* @param string $key The DI key. |
|
144
|
|
|
* @param boolean $useCache (optional) An optional indicating whether we |
|
145
|
|
|
* should use the cached version of the element (true by default). |
|
146
|
|
|
* @return mixed Returns the DI element mapped to that key. |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function get($key, $useCache = true) |
|
149
|
|
|
{ |
|
150
|
2 |
|
return Di::getDefault()->get($key, $useCache); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets an element in the DI container for the specified key. |
|
155
|
|
|
* @param string $key The DI key. |
|
156
|
|
|
* @param mixed $element The DI element to store. |
|
157
|
|
|
* @return Di Returns the Di instance. |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function set($key, $element) |
|
160
|
|
|
{ |
|
161
|
1 |
|
return Di::getDefault()->set($key, $element); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|