1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
6
|
|
|
* @package Admin |
7
|
|
|
* @subpackage JsonAdm |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JsonAdm\Common\Factory; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Common methods for all JSON API factories |
16
|
|
|
* |
17
|
|
|
* @package Admin |
18
|
|
|
* @subpackage JsonAdm |
19
|
|
|
*/ |
20
|
|
|
class Base |
21
|
|
|
{ |
22
|
|
|
private static $objects = []; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Injects a client object |
27
|
|
|
* |
28
|
|
|
* The object is returned via create() if an instance of the class |
29
|
|
|
* with the name name is requested. |
30
|
|
|
* |
31
|
|
|
* @param string $classname Full name of the class for which the object should be returned |
32
|
|
|
* @param \Aimeos\Admin\JsonAdm\Iface|null $client JSON API client object |
33
|
|
|
*/ |
34
|
|
|
public static function injectClient( string $classname, ?\Aimeos\Admin\JsonAdm\Iface $client = null ) |
35
|
|
|
{ |
36
|
|
|
self::$objects[$classname] = $client; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds the decorators to the JSON API client object |
42
|
|
|
* |
43
|
|
|
* @param \Aimeos\Admin\JsonAdm\Common\Iface $client Client object |
44
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
45
|
|
|
* @param string $path Name of the client separated by slashes, e.g "product/property" |
46
|
|
|
* @return \Aimeos\Admin\JsonAdm\Iface Client object |
47
|
|
|
*/ |
48
|
|
|
protected static function addClientDecorators( \Aimeos\Admin\JsonAdm\Iface $client, |
49
|
|
|
\Aimeos\MShop\ContextIface $context, string $path ) : \Aimeos\Admin\JsonAdm\Iface |
50
|
|
|
{ |
51
|
|
|
$config = $context->config(); |
52
|
|
|
|
53
|
|
|
/** admin/jsonadm/common/decorators/default |
54
|
|
|
* Configures the list of decorators applied to all JSON API clients |
55
|
|
|
* |
56
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
57
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
58
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
59
|
|
|
* modify what is returned to the caller. |
60
|
|
|
* |
61
|
|
|
* This option allows you to configure a list of decorator names that should |
62
|
|
|
* be wrapped around the original instance of all created clients: |
63
|
|
|
* |
64
|
|
|
* admin/jsonadm/common/decorators/default = array( 'decorator1', 'decorator2' ) |
65
|
|
|
* |
66
|
|
|
* This would wrap the decorators named "decorator1" and "decorator2" around |
67
|
|
|
* all client instances in that order. The decorator classes would be |
68
|
|
|
* "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator1" and |
69
|
|
|
* "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator2". |
70
|
|
|
* |
71
|
|
|
* @param array List of decorator names |
72
|
|
|
* @since 2015.12 |
73
|
|
|
* @category Developer |
74
|
|
|
*/ |
75
|
|
|
$decorators = $config->get( 'admin/jsonadm/common/decorators/default', [] ); |
76
|
|
|
|
77
|
|
|
if( empty( $path ) ) |
78
|
|
|
{ |
79
|
|
|
$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\'; |
80
|
|
|
return self::addDecorators( $client, $decorators, $classprefix, $context, $path ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$dpath = trim( $path, '/' ); |
84
|
|
|
$dpath = ( $dpath !== '' ? $dpath . '/' : $dpath ); |
85
|
|
|
|
86
|
|
|
$excludes = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/excludes', [] ); |
87
|
|
|
$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
88
|
|
|
|
89
|
|
|
foreach( $decorators as $key => $name ) |
90
|
|
|
{ |
91
|
|
|
if( in_array( $name, $excludes ) ) { |
92
|
|
|
unset( $decorators[$key] ); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\'; |
97
|
|
|
$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path ); |
98
|
|
|
|
99
|
|
|
$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\'; |
100
|
|
|
$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/global', [] ); |
101
|
|
|
$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path ); |
102
|
|
|
|
103
|
|
|
$classprefix = '\\Aimeos\\Admin\\JsonAdm\\' . ucfirst( $localClass ) . '\\Decorator\\'; |
104
|
|
|
$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/local', [] ); |
105
|
|
|
return self::addDecorators( $client, $decorators, $classprefix, $context, $path ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds the decorators to the client object |
111
|
|
|
* |
112
|
|
|
* @param \Aimeos\Admin\JsonAdm\Iface $client Client object |
113
|
|
|
* @param array $decorators List of decorator names |
114
|
|
|
* @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JsonAdm\Product\Decorator\" |
115
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects |
116
|
|
|
* @param string $path Name of the client separated by slashes, e.g "product/stock" |
117
|
|
|
* @return \Aimeos\Admin\JsonAdm\Iface Client object |
118
|
|
|
* @throws \LogicException If class can't be instantiated |
119
|
|
|
*/ |
120
|
|
|
protected static function addDecorators( \Aimeos\Admin\JsonAdm\Iface $client, array $decorators, string $classprefix, |
121
|
|
|
\Aimeos\MShop\ContextIface $context, string $path ) : \Aimeos\Admin\JsonAdm\Iface |
122
|
|
|
{ |
123
|
|
|
$interface = \Aimeos\Admin\JsonAdm\Common\Decorator\Iface::class; |
124
|
|
|
|
125
|
|
|
foreach( $decorators as $name ) |
126
|
|
|
{ |
127
|
|
|
if( ctype_alnum( $name ) === false ) { |
128
|
|
|
throw new \LogicException( sprintf( 'Invalid class name "%1$s"', $name ), 400 ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$client = \Aimeos\Utils::create( $classprefix . $name, [$client, $context, $path], $interface ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $client; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Creates a new client object |
140
|
|
|
* |
141
|
|
|
* @param string $classname Name of the client class |
142
|
|
|
* @param string $interface Name of the client interface |
143
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
144
|
|
|
* @param string $path Name of the client separated by slashes, e.g "product/property" |
145
|
|
|
* @return \Aimeos\Admin\JsonAdm\Common\Iface Client object |
146
|
|
|
*/ |
147
|
|
|
protected static function createAdmin( string $classname, string $interface, |
148
|
|
|
\Aimeos\MShop\ContextIface $context, string $path ) : \Aimeos\Admin\JsonAdm\Common\Iface |
149
|
|
|
{ |
150
|
|
|
if( isset( self::$objects[$classname] ) ) { |
151
|
|
|
return self::$objects[$classname]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return \Aimeos\Utils::create( $classname, [$context, $path], $interface ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|