Passed
Push — master ( 856fde...d81139 )
by Aimeos
05:19
created

MShop::create()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 12
c 5
b 0
f 0
dl 0
loc 24
rs 9.2222
cc 6
nc 6
nop 3
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();
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
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
		$classname = self::classname( $context, $parts, $domain, $name );
74
75
		if( self::$cache === false || !isset( self::$objects[$classname] ) ) {
76
			self::instantiate( $context, $parts, $domain, $name );
77
		}
78
79
		return self::$objects[$classname]->setObject( self::$objects[$classname] );
80
	}
81
82
83
	/**
84
	 * Injects a manager object for the given path of manager names
85
	 *
86
	 * This method is for testing only and you must call \Aimeos\MShop::cache( false )
87
	 * afterwards!
88
	 *
89
	 * @param string $classname Full name of the class for which the object should be returned
90
	 * @param \Aimeos\MShop\Common\Manager\Iface|null $object Manager object for the given manager path or null to clear
91
	 */
92
	public static function inject( string $classname, \Aimeos\MShop\Common\Manager\Iface $object = null )
93
	{
94
		self::$objects['\\' . ltrim( $classname, '\\' )] = $object;
95
	}
96
97
98
	/**
99
	 * Adds the decorators to the manager object.
100
	 *
101
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
102
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object
103
	 * @param array $decorators List of decorator names that should be wrapped around the manager object
104
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\MShop\Product\Manager\Decorator\"
105
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
106
	 */
107
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context,
108
		\Aimeos\MShop\Common\Manager\Iface $manager, array $decorators, string $classprefix ) : \Aimeos\MShop\Common\Manager\Iface
109
	{
110
		foreach( $decorators as $name )
111
		{
112
			if( ctype_alnum( $name ) === false )
113
			{
114
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
115
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 400 );
116
			}
117
118
			$classname = $classprefix . $name;
119
120
			if( class_exists( $classname ) === false ) {
121
				throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
122
			}
123
124
			$interface = \Aimeos\MShop\Common\Manager\Decorator\Iface::class;
125
			$manager = new $classname( $manager, $context );
126
127
			if( !( $manager instanceof $interface ) )
128
			{
129
				$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
130
				throw new \Aimeos\MShop\Exception( $msg, 400 );
131
			}
132
		}
133
134
		return $manager;
135
	}
136
137
138
	/**
139
	 * Adds the decorators to the manager object.
140
	 *
141
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
142
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object
143
	 * @param string $domain Domain name in lower case, e.g. "product"
144
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
145
	 */
146
	protected static function addManagerDecorators( \Aimeos\MShop\ContextIface $context,
147
		\Aimeos\MShop\Common\Manager\Iface $manager, string $domain ) : \Aimeos\MShop\Common\Manager\Iface
148
	{
149
		$config = $context->config();
150
151
		/** mshop/common/manager/decorators/default
152
		 * Configures the list of decorators applied to all admin managers
153
		 *
154
		 * Decorators extend the functionality of a class by adding new aspects
155
		 * (e.g. log what is currently done), executing the methods of the underlying
156
		 * class only in certain conditions (e.g. only for logged in users) or
157
		 * modify what is returned to the caller.
158
		 *
159
		 * This option allows you to configure a list of decorator names that should
160
		 * be wrapped around the original instances of all created managers:
161
		 *
162
		 *  mshop/common/manager/decorators/default = array( 'decorator1', 'decorator2' )
163
		 *
164
		 * This would wrap the decorators named "decorator1" and "decorator2" around
165
		 * all controller instances in that order. The decorator classes would be
166
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" and
167
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator2".
168
		 *
169
		 * @param array List of decorator names
170
		 * @since 2014.03
171
		 * @category Developer
172
		 */
173
		$decorators = $config->get( 'mshop/common/manager/decorators/default', [] );
174
		$excludes = $config->get( 'mshop/' . $domain . '/manager/decorators/excludes', [] );
175
176
		foreach( $decorators as $key => $name )
177
		{
178
			if( in_array( $name, $excludes ) ) {
179
				unset( $decorators[$key] );
180
			}
181
		}
182
183
		$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\';
184
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
185
186
		$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\';
187
		$decorators = $config->get( 'mshop/' . $domain . '/manager/decorators/global', [] );
188
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
189
190
		$classprefix = '\Aimeos\MShop\\' . ucfirst( $domain ) . '\Manager\Decorator\\';
191
		$decorators = $config->get( 'mshop/' . $domain . '/manager/decorators/local', [] );
192
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
193
194
		return $manager;
195
	}
196
197
198
	/**
199
	 * Validates the given path
200
	 *
201
	 * @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "product/list"
202
	 * @return string Sanitized path
203
	 */
204
	protected static function checkPath( string $path ) : string
205
	{
206
		$path = trim( $path, '/' );
207
208
		if( empty( $path ) ) {
209
			throw new \Aimeos\MShop\Exception( 'Manager path is empty', 400 );
210
		}
211
212
		if( preg_match( '/^[a-z0-9\/]+$/', $path ) !== 1 ) {
213
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid component path "%1$s"', $path, 400 ) );
214
		}
215
216
		return $path;
217
	}
218
219
220
	/**
221
	 * Creates a manager object.
222
	 *
223
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
224
	 * @param string $classname Name of the manager class
225
	 * @param string $interface Name of the manager interface
226
	 * @param string $domain Domain name in lower case, e.g. "product"
227
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
228
	 */
229
	protected static function createManager( \Aimeos\MShop\ContextIface $context,
230
		string $classname, string $interface, string $domain ) : \Aimeos\MShop\Common\Manager\Iface
231
	{
232
		if( isset( self::$objects[$classname] ) ) {
233
			return self::$objects[$classname];
234
		}
235
236
		if( class_exists( $classname ) === false ) {
237
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
238
		}
239
240
		$manager = new $classname( $context );
241
242
		if( !( $manager instanceof $interface ) )
243
		{
244
			$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
245
			throw new \Aimeos\MShop\Exception( $msg, 400 );
246
		}
247
248
		return self::addManagerDecorators( $context, $manager, $domain );
249
	}
250
251
252
	/**
253
	 * Returns the class name for the manager object
254
	 *
255
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
256
	 * @param array $parts List of sub-path parts (without domain)
257
	 * @param string $domain Domain name (first part of the path)
258
	 * @param string|null $name Name of the manager implementation
259
	 * @return string Manager class name
260
	 */
261
	protected static function classname( \Aimeos\MShop\ContextIface $context, array $parts, string $domain, string $name = null ) : string
262
	{
263
		$subClass = !empty( $parts ) ? ucwords( join( '\\', $parts ), '\\' ) . '\\' : '';
264
		$classname = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\' . $subClass;
265
266
		$subPath = !empty( $parts ) ? join( '/', $parts ) . '/' : '';
267
		$localName = $context->config()->get( 'mshop/' . $domain . '/manager/name', 'Standard' );
268
		$localName = $name ?: $context->config()->get( 'mshop/' . $domain . '/manager/' . $subPath . 'name', $localName );
269
270
		if( class_exists( $classname . $localName ) ) {
271
			return $classname . $localName;
272
		}
273
274
		return $classname . 'Standard';
275
	}
276
277
278
	/**
279
	 * Instantiates the manager objects for all parts of the path
280
	 *
281
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
282
	 * @param array $parts List of sub-path parts (without domain)
283
	 * @param string $domain Domain name (first part of the path)
284
	 * @param string|null $name Name of the manager implementation
285
	 */
286
	protected static function instantiate( \Aimeos\MShop\ContextIface $context, array $parts, string $domain, string $name = null )
287
	{
288
		$classname = self::classname( $context, [], $domain, $name );
289
		$iface = '\\Aimeos\\MShop\\' . ucfirst( $domain ) . '\\Manager\\Iface';
290
		$manager = self::createManager( $context, $classname, $iface, $domain );
291
292
		self::$objects[$classname] = $manager;
293
		$paths = [$domain => $manager];
294
295
		$subParts = [];
296
		$tmppath = $domain;
297
		$last = end( $parts );
298
299
		foreach( $parts as $part )
300
		{
301
			$subParts[] = $part;
302
			$localName = $part === $last ? $name : null;
303
			$classname = self::classname( $context, $subParts, $domain, $localName );
304
305
			$paths[$tmppath . '/' . $part] = $paths[$tmppath]->getSubManager( $part, $localName );
306
			$tmppath .= '/' . $part;
307
308
			self::$objects[$classname] = $paths[$tmppath];
309
		}
310
	}
311
}
312