1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2022 |
6
|
|
|
* @package MShop |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Factory which can create all MShop managers |
15
|
|
|
* |
16
|
|
|
* @package MShop |
17
|
|
|
*/ |
18
|
|
|
class MShop |
19
|
|
|
{ |
20
|
|
|
private static $context; |
21
|
|
|
private static $cache = true; |
22
|
|
|
private static $objects = []; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Enables or disables caching of class instances and clears cache |
27
|
|
|
* |
28
|
|
|
* @param bool $value True to enable caching, false to disable it. |
29
|
|
|
*/ |
30
|
|
|
public static function cache( bool $value ) |
31
|
|
|
{ |
32
|
|
|
self::$cache = (bool) $value; |
33
|
|
|
self::$context = null; |
34
|
|
|
self::$objects = []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates the required manager specified by the given path of manager names |
40
|
|
|
* |
41
|
|
|
* Domain managers are created by providing only the domain name, e.g. |
42
|
|
|
* "product" for the \Aimeos\MShop\Product\Manager\Standard or a path of names to |
43
|
|
|
* retrieve a specific sub-manager, e.g. "product/type" for the |
44
|
|
|
* \Aimeos\MShop\Product\Manager\Type\Standard manager. |
45
|
|
|
* Please note, that only the default managers can be created. If you need |
46
|
|
|
* a specific implementation, you need to use the factory class of the |
47
|
|
|
* domain or the getSubManager() method to hand over specifc implementation |
48
|
|
|
* names. |
49
|
|
|
* |
50
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object required by managers |
51
|
|
|
* @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "product/list" |
52
|
|
|
* @param string|null $name Name of the controller implementation ("Standard" if null) |
53
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
54
|
|
|
* @throws \Aimeos\MShop\Exception If the given path is invalid or the manager wasn't found |
55
|
|
|
*/ |
56
|
|
|
public static function create( \Aimeos\MShop\ContextIface $context, |
57
|
|
|
string $path, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
58
|
|
|
{ |
59
|
|
|
$path = self::checkPath( $path ); |
60
|
|
|
|
61
|
|
|
if( self::$context !== null && self::$context !== $context ) { |
62
|
|
|
self::$objects = []; // clear cached objects on context change |
63
|
|
|
} |
64
|
|
|
self::$context = $context; |
65
|
|
|
|
66
|
|
|
$config = $context->config(); |
67
|
|
|
$parts = explode( '/', $path ); |
68
|
|
|
|
69
|
|
|
if( ( $domain = array_shift( $parts ) ) === null ) { |
70
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Manager path is empty', $path ) ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if( empty( $name ) ) |
74
|
|
|
{ |
75
|
|
|
$subpath = !empty( $parts ) ? join( '/', $parts ) . '/' : ''; |
76
|
|
|
$name = $config->get( 'mshop/' . $domain . '/manager/' . $subpath . 'name', 'Standard' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$localClass = !empty( $parts ) ? ucwords( join( '\\', $parts ), '\\' ) . '\\' : ''; |
80
|
|
|
$finalClass = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\' . $localClass . $name; |
81
|
|
|
|
82
|
|
|
if( self::$cache === false || !isset( self::$objects[$finalClass] ) ) { |
83
|
|
|
self::instantiate( $context, $parts, $domain, $name ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return self::$objects[$finalClass]->setObject( self::$objects[$finalClass] ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Injects a manager object for the given path of manager names |
92
|
|
|
* |
93
|
|
|
* This method is for testing only and you must call \Aimeos\MShop::cache( false ) |
94
|
|
|
* afterwards! |
95
|
|
|
* |
96
|
|
|
* @param string $classname Full name of the class for which the object should be returned |
97
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface|null $object Manager object for the given manager path or null to clear |
98
|
|
|
*/ |
99
|
|
|
public static function inject( string $classname, \Aimeos\MShop\Common\Manager\Iface $object = null ) |
100
|
|
|
{ |
101
|
|
|
self::$objects['\\' . ltrim( $classname, '\\' )] = $object; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Adds the decorators to the manager object. |
107
|
|
|
* |
108
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
109
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object |
110
|
|
|
* @param array $decorators List of decorator names that should be wrapped around the manager object |
111
|
|
|
* @param string $classprefix Decorator class prefix, e.g. "\Aimeos\MShop\Product\Manager\Decorator\" |
112
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
113
|
|
|
*/ |
114
|
|
|
protected static function addDecorators( \Aimeos\MShop\ContextIface $context, |
115
|
|
|
\Aimeos\MShop\Common\Manager\Iface $manager, array $decorators, string $classprefix ) : \Aimeos\MShop\Common\Manager\Iface |
116
|
|
|
{ |
117
|
|
|
foreach( $decorators as $name ) |
118
|
|
|
{ |
119
|
|
|
if( ctype_alnum( $name ) === false ) |
120
|
|
|
{ |
121
|
|
|
$classname = is_string( $name ) ? $classprefix . $name : '<not a string>'; |
122
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 400 ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$classname = $classprefix . $name; |
126
|
|
|
|
127
|
|
|
if( class_exists( $classname ) === false ) { |
128
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$interface = \Aimeos\MShop\Common\Manager\Decorator\Iface::class; |
132
|
|
|
$manager = new $classname( $manager, $context ); |
133
|
|
|
|
134
|
|
|
if( !( $manager instanceof $interface ) ) |
135
|
|
|
{ |
136
|
|
|
$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface ); |
137
|
|
|
throw new \Aimeos\MShop\Exception( $msg, 400 ); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $manager; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adds the decorators to the manager object. |
147
|
|
|
* |
148
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
149
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object |
150
|
|
|
* @param string $domain Domain name in lower case, e.g. "product" |
151
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
152
|
|
|
*/ |
153
|
|
|
protected static function addManagerDecorators( \Aimeos\MShop\ContextIface $context, |
154
|
|
|
\Aimeos\MShop\Common\Manager\Iface $manager, string $domain ) : \Aimeos\MShop\Common\Manager\Iface |
155
|
|
|
{ |
156
|
|
|
$config = $context->config(); |
157
|
|
|
|
158
|
|
|
/** mshop/common/manager/decorators/default |
159
|
|
|
* Configures the list of decorators applied to all admin managers |
160
|
|
|
* |
161
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
162
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
163
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
164
|
|
|
* modify what is returned to the caller. |
165
|
|
|
* |
166
|
|
|
* This option allows you to configure a list of decorator names that should |
167
|
|
|
* be wrapped around the original instances of all created managers: |
168
|
|
|
* |
169
|
|
|
* mshop/common/manager/decorators/default = array( 'decorator1', 'decorator2' ) |
170
|
|
|
* |
171
|
|
|
* This would wrap the decorators named "decorator1" and "decorator2" around |
172
|
|
|
* all controller instances in that order. The decorator classes would be |
173
|
|
|
* "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" and |
174
|
|
|
* "\Aimeos\MShop\Common\Manager\Decorator\Decorator2". |
175
|
|
|
* |
176
|
|
|
* @param array List of decorator names |
177
|
|
|
* @since 2014.03 |
178
|
|
|
* @category Developer |
179
|
|
|
*/ |
180
|
|
|
$decorators = $config->get( 'mshop/common/manager/decorators/default', [] ); |
181
|
|
|
$excludes = $config->get( 'mshop/' . $domain . '/manager/decorators/excludes', [] ); |
182
|
|
|
|
183
|
|
|
foreach( $decorators as $key => $name ) |
184
|
|
|
{ |
185
|
|
|
if( in_array( $name, $excludes ) ) { |
186
|
|
|
unset( $decorators[$key] ); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\'; |
191
|
|
|
$manager = self::addDecorators( $context, $manager, $decorators, $classprefix ); |
192
|
|
|
|
193
|
|
|
$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\'; |
194
|
|
|
$decorators = $config->get( 'mshop/' . $domain . '/manager/decorators/global', [] ); |
195
|
|
|
$manager = self::addDecorators( $context, $manager, $decorators, $classprefix ); |
196
|
|
|
|
197
|
|
|
$classprefix = '\Aimeos\MShop\\' . ucfirst( $domain ) . '\Manager\Decorator\\'; |
198
|
|
|
$decorators = $config->get( 'mshop/' . $domain . '/manager/decorators/local', [] ); |
199
|
|
|
$manager = self::addDecorators( $context, $manager, $decorators, $classprefix ); |
200
|
|
|
|
201
|
|
|
return $manager; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Validates the given path |
207
|
|
|
* |
208
|
|
|
* @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "product/list" |
209
|
|
|
* @return string Sanitized path |
210
|
|
|
*/ |
211
|
|
|
protected static function checkPath( string $path ) : string |
212
|
|
|
{ |
213
|
|
|
$path = trim( $path, '/' ); |
214
|
|
|
|
215
|
|
|
if( empty( $path ) ) { |
216
|
|
|
throw new \Aimeos\MShop\Exception( 'Manager path is empty', 400 ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if( preg_match( '/^[a-z0-9\/]+$/', $path ) !== 1 ) { |
220
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid component path "%1$s"', $path, 400 ) ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $path; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Creates a manager object. |
229
|
|
|
* |
230
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
231
|
|
|
* @param string $classname Name of the manager class |
232
|
|
|
* @param string $interface Name of the manager interface |
233
|
|
|
* @param string $domain Domain name in lower case, e.g. "product" |
234
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
235
|
|
|
*/ |
236
|
|
|
protected static function createManager( \Aimeos\MShop\ContextIface $context, |
237
|
|
|
string $classname, string $interface, string $domain ) : \Aimeos\MShop\Common\Manager\Iface |
238
|
|
|
{ |
239
|
|
|
if( isset( self::$objects[$classname] ) ) { |
240
|
|
|
return self::$objects[$classname]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if( class_exists( $classname ) === false ) { |
244
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not found', $classname ) ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$manager = new $classname( $context ); |
248
|
|
|
|
249
|
|
|
if( !( $manager instanceof $interface ) ) |
250
|
|
|
{ |
251
|
|
|
$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface ); |
252
|
|
|
throw new \Aimeos\MShop\Exception( $msg, 400 ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return self::addManagerDecorators( $context, $manager, $domain ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Instantiates the manager objects for all parts of the path |
261
|
|
|
* |
262
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
263
|
|
|
* @param array $parts List of sub-path parts (without domain) |
264
|
|
|
* @param string $domain Domain name (first part of the path) |
265
|
|
|
* @param string $name Name of the manager implementation |
266
|
|
|
*/ |
267
|
|
|
protected static function instantiate( \Aimeos\MShop\ContextIface $context, array $parts, string $domain, string $name ) |
268
|
|
|
{ |
269
|
|
|
$iface = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\Iface'; |
270
|
|
|
$classname = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\' . $name; |
271
|
|
|
|
272
|
|
|
$manager = self::createManager( $context, $classname, $iface, $domain ); |
273
|
|
|
|
274
|
|
|
self::$objects[$classname] = $manager; |
275
|
|
|
$paths = [$domain => $manager]; |
276
|
|
|
|
277
|
|
|
$subpath = ''; |
278
|
|
|
$tmppath = $domain; |
279
|
|
|
$last = end( $parts ); |
280
|
|
|
|
281
|
|
|
foreach( $parts as $part ) |
282
|
|
|
{ |
283
|
|
|
$localName = $name; |
284
|
|
|
$subpath .= $part . '/'; |
285
|
|
|
|
286
|
|
|
if( $part !== $last ) { |
287
|
|
|
$localName = $context->config()->get( 'mshop/' . $domain . '/manager/' . $subpath . 'name', 'Standard' ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$localClass = str_replace( '/', '\\', ucwords( $subpath, '/' ) ); |
291
|
|
|
$classname = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\' . $localClass . $localName; |
292
|
|
|
|
293
|
|
|
$paths[$tmppath . '/' . $part] = $paths[$tmppath]->getSubManager( $part, $localName ); |
294
|
|
|
$tmppath .= '/' . $part; |
295
|
|
|
|
296
|
|
|
self::$objects[$classname] = $paths[$tmppath]; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|