JsonApi::inject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client;
12
13
14
/**
15
 * Factory which can create all JSON API clients
16
 *
17
 * @package Client
18
 * @subpackage JsonApi
19
 */
20
class JsonApi
21
{
22
	/** client/jsonapi/name
23
	 * Class name of the used JSON API client implementation
24
	 *
25
	 * Each default JSON API client can be replace by an alternative imlementation.
26
	 * To use this implementation, you have to set the last part of the class
27
	 * name as configuration value so the client factory knows which class it
28
	 * has to instantiate.
29
	 *
30
	 * For example, if the name of the default class is
31
	 *
32
	 *  \Aimeos\Client\JsonApi\Standard
33
	 *
34
	 * and you want to replace it with your own version named
35
	 *
36
	 *  \Aimeos\Client\JsonApi\Mycntl
37
	 *
38
	 * then you have to set the this configuration option:
39
	 *
40
	 *  client/jsonapi/name = Mycntl
41
	 *
42
	 * The value is the last part of your own class name and it's case sensitive,
43
	 * so take care that the configuration value is exactly named like the last
44
	 * part of the class name.
45
	 *
46
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
47
	 * characters are possible! You should always start the last part of the class
48
	 * name with an upper case character and continue only with lower case characters
49
	 * or numbers. Avoid chamel case names like "MyCntl"!
50
	 *
51
	 * @param string Last part of the class name
52
	 * @since 2015.12
53
	 * @category Developer
54
	 */
55
56
	/** client/jsonapi/decorators/excludes
57
	 * Excludes decorators added by the "common" option from the JSON API clients
58
	 *
59
	 * Decorators extend the functionality of a class by adding new aspects
60
	 * (e.g. log what is currently done), executing the methods of the underlying
61
	 * class only in certain conditions (e.g. only for logged in users) or
62
	 * modify what is returned to the caller.
63
	 *
64
	 * This option allows you to remove a decorator added via
65
	 * "client/jsonapi/common/decorators/default" before they are wrapped
66
	 * around the Jsonadm client.
67
	 *
68
	 *  client/jsonapi/decorators/excludes = array( 'decorator1' )
69
	 *
70
	 * This would remove the decorator named "decorator1" from the list of
71
	 * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via
72
	 * "client/jsonapi/common/decorators/default" for the JSON API client.
73
	 *
74
	 * @param array List of decorator names
75
	 * @since 2016.01
76
	 * @category Developer
77
	 * @see client/jsonapi/common/decorators/default
78
	 * @see client/jsonapi/decorators/global
79
	 * @see client/jsonapi/decorators/local
80
	 */
81
82
	/** client/jsonapi/decorators/global
83
	 * Adds a list of globally available decorators only to the Jsonadm client
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Client\Jsonadm\Common\Decorator\*") around the Jsonadm
92
	 * client.
93
	 *
94
	 *  client/jsonapi/product/decorators/global = array( 'decorator1' )
95
	 *
96
	 * This would add the decorator named "decorator1" defined by
97
	 * "\Aimeos\Client\Jsonadm\Common\Decorator\Decorator1" only to the
98
	 * "product" Jsonadm client.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2016.01
102
	 * @category Developer
103
	 * @see client/jsonapi/common/decorators/default
104
	 * @see client/jsonapi/decorators/excludes
105
	 * @see client/jsonapi/decorators/local
106
	 */
107
108
	/** client/jsonapi/decorators/local
109
	 * Adds a list of local decorators only to the Jsonadm client
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Client\Jsonadm\Product\Decorator\*") around the Jsonadm
118
	 * client.
119
	 *
120
	 *  client/jsonapi/product/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Client\Jsonadm\Product\Decorator\Decorator2" only to the
124
	 * "product" Jsonadm client.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2016.01
128
	 * @category Developer
129
	 * @see client/jsonapi/common/decorators/default
130
	 * @see client/jsonapi/decorators/excludes
131
	 * @see client/jsonapi/decorators/global
132
	 */
133
134
135
	private static $objects = [];
136
137
138
	/**
139
	 * Creates the required client specified by the given path of client names
140
	 *
141
	 * Clients are created by providing only the domain name, e.g. "product"
142
	 *  for the \Aimeos\Client\JsonApi\Product\Standard or a path of names to
143
	 * retrieve a specific sub-client, e.g. "product/type" for the
144
	 * \Aimeos\Client\JsonApi\Product\Type\Standard client.
145
	 *
146
	 * @param \Aimeos\MShop\ContextIface $context Context object required by clients
147
	 * @param string $path Name of the client separated by slashes, e.g "order/product"
148
	 * @param string|null $name Name of the client implementation ("Standard" if null)
149
	 * @return \Aimeos\Client\JsonApi\Iface JSON client instance
150
	 * @throws \Aimeos\Client\JsonApi\Exception If the given path is invalid
151
	 */
152
	public static function create( \Aimeos\MShop\ContextIface $context,
153
		string $path, ?string $name = null ) : \Aimeos\Client\JsonApi\Iface
154
	{
155
		empty( $path = trim( $path, '/' ) ) ?: $path .= '/';
156
157
		if( $name === null ) {
158
			$name = $context->config()->get( 'client/jsonapi/' . $path . 'name', 'Standard' );
159
		}
160
161
		$interface = 'Aimeos\\Client\\JsonApi\\Iface';
162
		$classname = 'Aimeos\\Client\\JsonApi\\' . str_replace( '/', '\\', ucwords( $path, '/' ) ) . $name;
163
164
		if( class_exists( $classname ) === false ) {
165
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Class "%1$s" not found', $classname, 404 ) );
166
		}
167
168
		$client = self::createComponent( $context, $classname, $interface, $path );
169
170
		return $client->setView( $context->view() );
171
	}
172
173
174
	/**
175
	 * Injects a client object
176
	 *
177
	 * The object is returned via create() if an instance of the class
178
	 * with the name name is requested.
179
	 *
180
	 * @param string $classname Full name of the class for which the object should be returned
181
	 * @param \Aimeos\Client\JsonApi\Iface|null $client JSON API client object
182
	 */
183
	public static function inject( string $classname, ?\Aimeos\Client\JsonApi\Iface $client = null )
184
	{
185
		self::$objects['\\' . ltrim( $classname, '\\' )] = $client;
186
	}
187
188
189
	/**
190
	 * Adds the decorators to the JSON API client object
191
	 *
192
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
193
	 * @param \Aimeos\Client\JsonApi\Iface $client Client object
194
	 * @param string $path Name of the client, e.g "product"
195
	 * @return \Aimeos\Client\JsonApi\Iface Client object
196
	 */
197
	protected static function addComponentDecorators( \Aimeos\MShop\ContextIface $context,
198
		\Aimeos\Client\JsonApi\Iface $client, string $path ) : \Aimeos\Client\JsonApi\Iface
199
	{
200
		$config = $context->config();
201
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
202
203
		$classprefix = '\\Aimeos\\Client\\JsonApi\\' . $localClass . 'Decorator\\';
204
		$decorators = array_reverse( $config->get( 'client/jsonapi/' . $path . 'decorators/local', [] ) );
205
		$client = self::addDecorators( $context, $client, $path, $decorators, $classprefix );
206
207
		$classprefix = '\\Aimeos\\Client\\JsonApi\\Common\\Decorator\\';
208
		$decorators = array_reverse( $config->get( 'client/jsonapi/' . $path . 'decorators/global', [] ) );
209
		$client = self::addDecorators( $context, $client, $path, $decorators, $classprefix );
210
211
		/** client/jsonapi/common/decorators/default
212
		 * Configures the list of decorators applied to all JSON API clients
213
		 *
214
		 * Decorators extend the functionality of a class by adding new aspects
215
		 * (e.g. log what is currently done), executing the methods of the underlying
216
		 * class only in certain conditions (e.g. only for logged in users) or
217
		 * modify what is returned to the caller.
218
		 *
219
		 * This option allows you to configure a list of decorator names that should
220
		 * be wrapped around the original instance of all created clients:
221
		 *
222
		 *  client/jsonapi/common/decorators/default = array( 'decorator1', 'decorator2' )
223
		 *
224
		 * This would wrap the decorators named "decorator1" and "decorator2" around
225
		 * all client instances in that order. The decorator classes would be
226
		 * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" and
227
		 * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator2".
228
		 *
229
		 * @param array List of decorator names
230
		 * @since 2015.12
231
		 * @category Developer
232
		 */
233
		$decorators = array_reverse( $config->get( 'client/jsonapi/common/decorators/default', [] ) );
234
		$excludes = $config->get( 'client/jsonapi/' . $path . 'decorators/excludes', [] );
235
236
		foreach( $decorators as $key => $name )
237
		{
238
			if( in_array( $name, $excludes ) ) {
239
				unset( $decorators[$key] );
240
			}
241
		}
242
243
		$classprefix = '\\Aimeos\\Client\\JsonApi\\Common\\Decorator\\';
244
		$client = self::addDecorators( $context, $client, $path, $decorators, $classprefix );
245
246
		return $client;
247
	}
248
249
250
	/**
251
	 * Adds the decorators to the client object
252
	 *
253
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
254
	 * @param \Aimeos\Client\JsonApi\Iface $client Client object
255
	 * @param string $path Name of the component, e.g "product"
256
	 * @param array $decorators List of decorator names
257
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Client\JsonApi\Product\Decorator\"
258
	 * @return \Aimeos\Client\JsonApi\Iface Client object
259
	 * @throws \LogicException If class can't be instantiated
260
	 */
261
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context, \Aimeos\Client\JsonApi\Iface $client,
262
		string $path, array $decorators, string $classprefix ) : \Aimeos\Client\JsonApi\Iface
263
	{
264
		$interface = \Aimeos\Client\JsonApi\Common\Decorator\Iface::class;
265
266
		foreach( $decorators as $name )
267
		{
268
			if( ctype_alnum( $name ) === false ) {
269
				throw new \LogicException( sprintf( 'Invalid class name "%1$s"', $name ), 400 );
270
			}
271
272
			$client = \Aimeos\Utils::create( $classprefix . $name, [$client, $context, $path], $interface );
273
		}
274
275
		return $client;
276
	}
277
278
279
	/**
280
	 * Creates a new client object
281
	 *
282
	 * @param \Aimeos\MShop\ContextIface $context Context object
283
	 * @param string $classname Name of the client class
284
	 * @param string $interface Name of the client interface
285
	 * @param string $path Name of the client separated by slashes, e.g "order/product"
286
	 * @return \Aimeos\Client\JsonApi\Iface Client object
287
	 */
288
	protected static function createComponent( \Aimeos\MShop\ContextIface $context,
289
		string $classname, string $interface, string $path ) : \Aimeos\Client\JsonApi\Iface
290
	{
291
		if( isset( self::$objects[$classname] ) ) {
292
			return self::$objects[$classname];
293
		}
294
295
		$client = \Aimeos\Utils::create( $classname, [$context], $interface );
296
297
		return self::addComponentDecorators( $context, $client, $path );
298
	}
299
}
300