Jobs::createControllers()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 13
c 1
b 0
f 0
nc 5
nop 4
dl 0
loc 25
rs 8.0555
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller;
12
13
14
/**
15
 * Factory which can create all job controllers
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Jobs
21
{
22
	private static $objects = [];
23
24
25
	/**
26
	 * Creates the required controller specified by the given path of controller names.
27
	 *
28
	 * Controllers are created by providing only the domain name, e.g.
29
	 * "stock" for the \Aimeos\Controller\Jobs\Stock\Standard.
30
	 * Please note, that only the default controllers can be created. If you need
31
	 * a specific implementation, you need to use the factory class of the
32
	 * controller to hand over specifc implementation names.
33
	 *
34
	 * @param \Aimeos\MShop\ContextIface $context Context object required by controllers
35
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object
36
	 * @param string $path Name of the domain
37
	 * @param string|null $name Name of the controller implementation ("Standard" if null)
38
	 * @return \Aimeos\Controller\Jobs\Iface Controller class instance
39
	 * @throws \Aimeos\Controller\Jobs\Exception If the given path is invalid or the controllers wasn't found
40
	 */
41
	public static function create( \Aimeos\MShop\ContextIface $context, \Aimeos\Bootstrap $aimeos,
42
		string $path, ?string $name = null ) : \Aimeos\Controller\Jobs\Iface
43
	{
44
		if( empty( $path ) ) {
45
			throw new \Aimeos\Controller\Jobs\Exception( 'Controller path is empty', 400 );
46
		}
47
48
		if( empty( $name ) ) {
49
			$name = $context->config()->get( 'controller/jobs/' . $path . '/name', 'Standard' );
50
		}
51
52
		$iface = '\\Aimeos\\Controller\\Jobs\\Iface';
53
		$classname = '\\Aimeos\\Controller\\Jobs\\' . str_replace( '/', '\\', ucwords( $path, '/' ) ) . '\\' . $name;
54
55
		return self::createController( $context, $aimeos, $classname, $iface, $path );
56
	}
57
58
59
	/**
60
	 * Returns all available controller instances.
61
	 *
62
	 * @param \Aimeos\MShop\ContextIface $context Context object required by controllers
63
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object
64
	 * @param array $cntlPaths Associative list of the base path as key and all relative job controller paths (core and extensions)
65
	 * @return \Aimeos\Controller\Jobs\Iface[] Associative list of controller names as values and class instance as values
66
	 */
67
	public static function get( \Aimeos\MShop\ContextIface $context, \Aimeos\Bootstrap $aimeos, array $cntlPaths ) : array
68
	{
69
		$cntlList = [];
70
		$ds = DIRECTORY_SEPARATOR;
71
72
		foreach( $cntlPaths as $path => $list )
73
		{
74
			foreach( $list as $relpath )
75
			{
76
				$path .= $ds . str_replace( '/', $ds, $relpath . '/Controller/Jobs' );
77
78
				if( is_dir( $path ) )
79
				{
80
					$it = new \DirectoryIterator( $path );
81
					$list = self::createControllers( $it, $context, $aimeos );
82
83
					$cntlList = array_merge( $cntlList, $list );
84
				}
85
			}
86
		}
87
88
		ksort( $cntlList );
89
90
		return $cntlList;
91
	}
92
93
94
	/**
95
	 * Injects a controller object.
96
	 *
97
	 * @param string $classname Full name of the class for which the object should be returned
98
	 * @param \Aimeos\Controller\Jobs\Iface|null $controller Frontend controller object
99
	 */
100
	public static function inject( string $classname, ?\Aimeos\Controller\Jobs\Iface $controller = null )
101
	{
102
		self::$objects['\\' . ltrim( $classname, '\\' )] = $controller;
103
	}
104
105
106
	/**
107
	 * Adds the decorators to the controller object.
108
	 *
109
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
110
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object
111
	 * @param \Aimeos\Controller\Jobs\Iface $controller Controller object
112
	 * @param string $domain Domain name in lower case, e.g. "product"
113
	 * @return \Aimeos\Controller\Jobs\Iface Controller object
114
	 */
115
	protected static function addControllerDecorators( \Aimeos\MShop\ContextIface $context, \Aimeos\Bootstrap $aimeos,
116
		\Aimeos\Controller\Jobs\Iface $controller, string $domain ) : \Aimeos\Controller\Jobs\Iface
117
	{
118
		$config = $context->config();
119
		$localClass = str_replace( '/', '\\', ucwords( $domain, '/' ) );
120
121
		$classprefix = '\Aimeos\Controller\Jobs\\' . ucfirst( $localClass ) . '\Decorator\\';
122
		$decorators = array_reverse( $config->get( 'controller/jobs/' . $domain . '/decorators/local', [] ) );
123
		$controller = self::addDecorators( $context, $aimeos, $controller, $decorators, $classprefix );
124
125
		$classprefix = '\Aimeos\Controller\Jobs\Common\Decorator\\';
126
		$decorators = array_reverse( $config->get( 'controller/jobs/' . $domain . '/decorators/global', [] ) );
127
		$controller = self::addDecorators( $context, $aimeos, $controller, $decorators, $classprefix );
128
129
		/** controller/jobs/common/decorators/default
130
		 * Configures the list of decorators applied to all job controllers
131
		 *
132
		 * Decorators extend the functionality of a class by adding new aspects
133
		 * (e.g. log what is currently done), executing the methods of the underlying
134
		 * class only in certain conditions (e.g. only for logged in users) or
135
		 * modify what is returned to the caller.
136
		 *
137
		 * This option allows you to configure a list of decorator names that should
138
		 * be wrapped around the original instance of all created controllers:
139
		 *
140
		 *  controller/jobs/common/decorators/default = array( 'decorator1', 'decorator2' )
141
		 *
142
		 * This would wrap the decorators named "decorator1" and "decorator2" around
143
		 * all controller instances in that order. The decorator classes would be
144
		 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" and
145
		 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator2".
146
		 *
147
		 * @param array List of decorator names
148
		 * @since 2014.03
149
		 */
150
		$decorators = array_reverse( $config->get( 'controller/jobs/common/decorators/default', [] ) );
151
		$excludes = $config->get( 'controller/jobs/' . $domain . '/decorators/excludes', [] );
152
153
		foreach( $decorators as $key => $name )
154
		{
155
			if( in_array( $name, $excludes ) ) {
156
				unset( $decorators[$key] );
157
			}
158
		}
159
160
		$classprefix = '\Aimeos\Controller\Jobs\Common\Decorator\\';
161
		$controller = self::addDecorators( $context, $aimeos, $controller, $decorators, $classprefix );
162
163
		return $controller;
164
	}
165
166
167
	/**
168
	 * Adds the decorators to the controller object.
169
	 *
170
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
171
	 * @param \Aimeos\Bootstrap $aimeos Aimeos Bootstrap object
172
	 * @param \Aimeos\Controller\Jobs\Iface $controller Controller object
173
	 * @param array $decorators List of decorator names that should be wrapped around the controller object
174
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Controller\Jobs\Attribute\Decorator\"
175
	 * @return \Aimeos\Controller\Jobs\Iface Controller object
176
	 * @throws \LogicException If class can't be instantiated
177
	 */
178
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context, \Aimeos\Bootstrap $aimeos,
179
		\Aimeos\Controller\Jobs\Iface $controller, array $decorators, string $classprefix ) : \Aimeos\Controller\Jobs\Iface
180
	{
181
		$interface = \Aimeos\Controller\Jobs\Common\Decorator\Iface::class;
182
183
		foreach( $decorators as $name )
184
		{
185
			if( ctype_alnum( $name ) === false ) {
186
				throw new \LogicException( sprintf( 'Invalid class name "%1$s"', $name ), 400 );
187
			}
188
189
			$controller = \Aimeos\Utils::create( $classprefix . $name, [$controller, $context, $aimeos], $interface );
190
		}
191
192
		return $controller;
193
	}
194
195
196
	/**
197
	 * Creates a controller object.
198
	 *
199
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
200
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object
201
	 * @param string $classname Name of the controller class
202
	 * @param string $interface Name of the controller interface
203
	 * @param string $path Name of the domain
204
	 * @return \Aimeos\Controller\Jobs\Iface Controller object
205
	 */
206
	protected static function createController( \Aimeos\MShop\ContextIface $context, \Aimeos\Bootstrap $aimeos,
207
		string $classname, string $interface, string $path ) : \Aimeos\Controller\Jobs\Iface
208
	{
209
		if( isset( self::$objects[$classname] ) ) {
210
			return self::$objects[$classname];
211
		}
212
213
		$cntl = \Aimeos\Utils::create( $classname, [$context, $aimeos], $interface );
214
215
		return self::addControllerDecorators( $context, $aimeos, $cntl, $path );
216
	}
217
218
219
	/**
220
	 * Instantiates all found factories and stores the controller instances in the class variable.
221
	 *
222
	 * @param \DirectoryIterator $dir Iterator over the (sub-)directory which might contain a factory
223
	 * @param \Aimeos\MShop\ContextIface $context Context object required by controllers
224
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap object
225
	 * @param string $prefix Part of the class name between "\Aimeos\Controller\Jobs" and "Factory"
226
	 * @return \Aimeos\Controller\Jobs\Iface[] Associative list if prefixes as values and job controller instances as values
227
	 * @throws \Aimeos\Controller\Jobs\Exception If factory name is invalid or if the controller couldn't be instantiated
228
	 */
229
	protected static function createControllers( \DirectoryIterator $dir, \Aimeos\MShop\ContextIface $context,
230
		\Aimeos\Bootstrap $aimeos, string $prefix = '' ) : array
231
	{
232
		$list = [];
233
234
		foreach( $dir as $entry )
235
		{
236
			if( $entry->getType() === 'dir' && $entry->isDot() === false
237
				&& !in_array( $entry->getBaseName(), ['Common', 'Decorator'] )
238
			) {
239
				$name = strtolower( $entry->getBaseName() );
240
				$it = new \DirectoryIterator( $entry->getPathName() );
241
				$pref = ( $prefix !== '' ? $prefix . '/' : '' ) . $name;
242
				$subList = self::createControllers( $it, $context, $aimeos, $pref );
243
244
				$list = array_merge( $list, $subList );
245
			}
246
			else if( $prefix !== '' && $entry->getType() === 'file'
247
				&& !in_array( $entry->getBaseName( '.php' ), ['Base'] ) )
248
			{
249
				$list[$prefix] = self::create( $context, $aimeos, $prefix );
250
			}
251
		}
252
253
		return $list;
254
	}
255
}
256