|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2012 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2021 |
|
7
|
|
|
* @package Controller |
|
8
|
|
|
* @subpackage Frontend |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Aimeos\Controller\Frontend; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Common methods for frontend controller classes. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Controller |
|
19
|
|
|
* @subpackage Frontend |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class Base |
|
22
|
|
|
{ |
|
23
|
|
|
private static $methods = []; |
|
24
|
|
|
|
|
25
|
|
|
private $object; |
|
26
|
|
|
private $context; |
|
27
|
|
|
private $cond = []; |
|
28
|
|
|
private $sort = []; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Common initialization for controller classes. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->context = $context; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Registers a custom method that has access to the class properties if called non-static. |
|
44
|
|
|
* |
|
45
|
|
|
* Examples: |
|
46
|
|
|
* Provider::method( 'test', function( $name ) { |
|
47
|
|
|
* return $this->getConfigValue( $name ) ? true : false; |
|
48
|
|
|
* } ); |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $name Method name |
|
51
|
|
|
* @param \Closure $function Anonymous method |
|
52
|
|
|
* @return \Closure|null Registered method |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function method( string $name, \Closure $function = null ) : ?\Closure |
|
55
|
|
|
{ |
|
56
|
|
|
$self = get_called_class(); |
|
57
|
|
|
|
|
58
|
|
|
if( $function ) { |
|
59
|
|
|
self::$methods[$self][$name] = $function; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach( array_merge( [$self], class_parents( static::class ) ) as $class ) |
|
63
|
|
|
{ |
|
64
|
|
|
if( isset( self::$methods[$class][$name] ) ) { |
|
65
|
|
|
return self::$methods[$class][$name]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Passes unknown method calls to the custom methods |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $method Method name |
|
77
|
|
|
* @param array $args Method arguments |
|
78
|
|
|
* @return mixed Result or method call |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __call( string $method, array $args ) |
|
81
|
|
|
{ |
|
82
|
|
|
if( $fcn = static::method( $method ) ) { |
|
83
|
|
|
return call_user_func_array( $fcn->bindTo( $this, static::class ), $args ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$msg = 'Called unknown method "%1$s" on class "%2$s"'; |
|
87
|
|
|
throw new \BadMethodCallException( sprintf( $msg, $method, get_class( $this ) ) ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Passes unknown method calls to the custom methods |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $method Method name |
|
95
|
|
|
* @param array $args Method arguments |
|
96
|
|
|
* @return mixed Result or method call |
|
97
|
|
|
*/ |
|
98
|
|
|
public function call( string $method, ...$args ) |
|
99
|
|
|
{ |
|
100
|
|
|
if( $fcn = static::method( $method ) ) { |
|
101
|
|
|
return call_user_func_array( $fcn->bindTo( $this, static::class ), $args ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->$method( ...$args ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Adds the given compare, combine or sort expression to the list of expressions |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Aimeos\MW\Criteria\Expression\Iface|null $expr Compare, combine or sort expression |
|
112
|
|
|
* @return \Aimeos\Controller\Frontend\Iface Controller object for chaining method calls |
|
113
|
|
|
*/ |
|
114
|
|
|
public function addExpression( \Aimeos\MW\Criteria\Expression\Iface $expr = null ) : Iface |
|
115
|
|
|
{ |
|
116
|
|
|
if( $expr instanceof \Aimeos\MW\Criteria\Expression\Sort\Iface ) { |
|
117
|
|
|
$this->sort[] = $expr; |
|
118
|
|
|
} elseif( $expr ) { |
|
119
|
|
|
$this->cond[] = $expr; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the compare and combine expressions added by addExpression() |
|
128
|
|
|
* |
|
129
|
|
|
* @return array List of compare and combine expressions |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getConditions() : array |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->cond; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the compare and combine expressions added by addExpression() |
|
139
|
|
|
* |
|
140
|
|
|
* @return array List of sort expressions |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getSortations() : array |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->sort; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the context object. |
|
150
|
|
|
* |
|
151
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object implementing \Aimeos\MShop\Context\Item\Iface |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->context; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns the outmost decorator of the decorator stack |
|
161
|
|
|
* |
|
162
|
|
|
* @return \Aimeos\Controller\Frontend\Iface Outmost decorator object |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getObject() : Iface |
|
165
|
|
|
{ |
|
166
|
|
|
if( $this->object !== null ) { |
|
167
|
|
|
return $this->object; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Injects the reference of the outmost object |
|
176
|
|
|
* |
|
177
|
|
|
* @param \Aimeos\Controller\Frontend\Iface $object Reference to the outmost controller or decorator |
|
178
|
|
|
* @return \Aimeos\Controller\Frontend\Iface Controller object for chaining method calls |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setObject( \Aimeos\Controller\Frontend\Iface $object ) : Iface |
|
181
|
|
|
{ |
|
182
|
|
|
$this->object = $object; |
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Splits search keys by comma |
|
189
|
|
|
* |
|
190
|
|
|
* @param string|null $keys Comma separated string of search keys |
|
191
|
|
|
* @return array List of search keys |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function splitKeys( ?string $keys ) : array |
|
194
|
|
|
{ |
|
195
|
|
|
$list = []; |
|
196
|
|
|
|
|
197
|
|
|
if( preg_match_all( '/(?P<key>[^(,]+(\(("([^"]|\")*")?[^)]*\))?),?/', (string) $keys, $list ) !== false ) { |
|
198
|
|
|
return $list['key'] ?? []; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return []; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|