Passed
Push — master ( 3393ef...c6d119 )
by Aimeos
05:27
created

MAdmin::addManagerDecorators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 49
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 49
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
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 MAdmin managers
15
 *
16
 * @package MAdmin
17
 */
18
class MAdmin
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\MAdmin\Log\Manager\Standard or a path of names to
43
	 * retrieve a specific sub-manager.
44
	 * Please note, that only the default managers can be created. If you need
45
	 * a specific implementation, you need to use the factory class of the
46
	 * domain or the getSubManager() method to hand over specifc implementation
47
	 * names.
48
	 *
49
	 * @param \Aimeos\MShop\ContextIface $context Context object required by managers
50
	 * @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "log"
51
	 * @param string|null $name Name of the controller implementation ("Standard" if null)
52
	 * @return \Aimeos\MShop\Common\Manager\Iface MAdmin manager object
53
	 * @throws \Aimeos\MAdmin\Exception If the given path is invalid or the manager wasn't found
54
	 */
55
	public static function create( \Aimeos\MShop\ContextIface $context,
56
		string $path, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
57
	{
58
		if( empty( $path ) ) {
59
			throw new \Aimeos\MAdmin\Exception( 'Manager path is empty', 400 );
60
		}
61
62
		if( preg_match( '/^[a-z0-9\/]+$/', $path ) !== 1 ) {
63
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Invalid component path "%1$s"', $path, 400 ) );
64
		}
65
66
		if( self::$context !== null && self::$context !== $context ) {
67
			self::$objects = []; // clear cached objects on context change
68
		}
69
		self::$context = $context;
70
71
		$parts = explode( '/', $path );
72
73
		if( ( $domain = array_shift( $parts ) ) === null ) {
74
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Manager path is empty', $path ) );
75
		}
76
77
		if( empty( $name ) ) {
78
			$name = $context->config()->get( 'madmin/' . $domain . '/manager/name', 'Standard' );
79
		}
80
81
		$classname = '\\Aimeos\\MAdmin\\' . ucfirst( $domain ) . '\\Manager\\' . $name;
82
83
		if( self::$cache === false || !isset( self::$objects[$classname] ) )
84
		{
85
			$iface = '\\Aimeos\\MAdmin\\' . ucfirst( $domain ) . '\\Manager\\Iface';
86
87
			$manager = self::createManager( $context, $classname, $iface );
88
			$manager = self::addManagerDecorators( $context, $manager, $domain );
89
90
			self::$objects[$classname] = $manager;
91
		}
92
93
		return self::$objects[$classname];
94
	}
95
96
97
	/**
98
	 * Injects a manager object for the given path of manager names
99
	 *
100
	 * This method is for testing only and you must call \Aimeos\MAdmin::cache( false )
101
	 * afterwards!
102
	 *
103
	 * @param string $classname Full name of the class for which the object should be returned
104
	 * @param \Aimeos\MShop\Common\Manager\Iface|null $object Manager object for the given manager path or null to clear
105
	 */
106
	public static function inject( string $classname, \Aimeos\MShop\Common\Manager\Iface $object = null )
107
	{
108
		self::$objects['\\' . ltrim( $classname, '\\' )] = $object;
109
	}
110
111
112
	/**
113
	 * Adds the decorators to the manager object.
114
	 *
115
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
116
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object
117
	 * @param array $decorators List of decorator names that should be wrapped around the manager object
118
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\MShop\Product\Manager\Decorator\"
119
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
120
	 */
121
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context,
122
		\Aimeos\MShop\Common\Manager\Iface $manager, array $decorators, string $classprefix ) : \Aimeos\MShop\Common\Manager\Iface
123
	{
124
		foreach( $decorators as $name )
125
		{
126
			if( ctype_alnum( $name ) === false )
127
			{
128
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
129
				throw new \Aimeos\MAdmin\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 400 );
130
			}
131
132
			$classname = $classprefix . $name;
133
134
			if( class_exists( $classname ) === false ) {
135
				throw new \Aimeos\MAdmin\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
136
			}
137
138
			$interface = \Aimeos\MShop\Common\Manager\Decorator\Iface::class;
139
			$manager = new $classname( $manager, $context );
140
141
			if( !( $manager instanceof $interface ) )
142
			{
143
				$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
144
				throw new \Aimeos\MAdmin\Exception( $msg, 400 );
145
			}
146
		}
147
148
		return $manager;
149
	}
150
151
152
	/**
153
	 * Adds the decorators to the manager object.
154
	 *
155
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
156
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object
157
	 * @param string $domain Domain name in lower case, e.g. "product"
158
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
159
	 */
160
	protected static function addManagerDecorators( \Aimeos\MShop\ContextIface $context,
161
		\Aimeos\MShop\Common\Manager\Iface $manager, string $domain ) : \Aimeos\MShop\Common\Manager\Iface
162
	{
163
		$config = $context->config();
164
165
		/** madmin/common/manager/decorators/default
166
		 * Configures the list of decorators applied to all admin managers
167
		 *
168
		 * Decorators extend the functionality of a class by adding new aspects
169
		 * (e.g. log what is currently done), executing the methods of the underlying
170
		 * class only in certain conditions (e.g. only for logged in users) or
171
		 * modify what is returned to the caller.
172
		 *
173
		 * This option allows you to configure a list of decorator names that should
174
		 * be wrapped around the original instances of all created managers:
175
		 *
176
		 *  madmin/common/manager/decorators/default = array( 'decorator1', 'decorator2' )
177
		 *
178
		 * This would wrap the decorators named "decorator1" and "decorator2" around
179
		 * all controller instances in that order. The decorator classes would be
180
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" and
181
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator2".
182
		 *
183
		 * @param array List of decorator names
184
		 * @since 2014.03
185
		 * @category Developer
186
		 */
187
		$decorators = $config->get( 'madmin/common/manager/decorators/default', [] );
188
		$excludes = $config->get( 'madmin/' . $domain . '/manager/decorators/excludes', [] );
189
190
		foreach( $decorators as $key => $name )
191
		{
192
			if( in_array( $name, $excludes ) ) {
193
				unset( $decorators[$key] );
194
			}
195
		}
196
197
		$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\';
198
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
199
200
		$classprefix = '\Aimeos\MShop\Common\Manager\Decorator\\';
201
		$decorators = $config->get( 'madmin/' . $domain . '/manager/decorators/global', [] );
202
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
203
204
		$classprefix = '\Aimeos\MShop\\' . ucfirst( $domain ) . '\Manager\Decorator\\';
205
		$decorators = $config->get( 'madmin/' . $domain . '/manager/decorators/local', [] );
206
		$manager = self::addDecorators( $context, $manager, $decorators, $classprefix );
207
208
		return $manager;
209
	}
210
211
212
	/**
213
	 * Creates a manager object.
214
	 *
215
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
216
	 * @param string $classname Name of the manager class
217
	 * @param string $interface Name of the manager interface
218
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
219
	 */
220
	protected static function createManager( \Aimeos\MShop\ContextIface $context,
221
		string $classname, string $interface ) : \Aimeos\MShop\Common\Manager\Iface
222
	{
223
		if( isset( self::$objects[$classname] ) ) {
224
			return self::$objects[$classname];
225
		}
226
227
		if( class_exists( $classname ) === false ) {
228
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
229
		}
230
231
		$manager = new $classname( $context );
232
233
		if( !( $manager instanceof $interface ) )
234
		{
235
			$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
236
			throw new \Aimeos\MAdmin\Exception( $msg, 400 );
237
		}
238
239
		return $manager;
240
	}
241
}
242