Frontend   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
eloc 41
c 3
b 0
f 0
dl 0
loc 179
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A cache() 0 4 1
A create() 0 19 5
A inject() 0 3 1
A createController() 0 10 2
A addDecorators() 0 15 3
A addControllerDecorators() 0 50 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller;
12
13
14
/**
15
 * Factory which can create all Frontend controllers
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Frontend
21
{
22
	private static $cache = true;
23
	private static $objects = [];
24
25
26
	/**
27
	 * Enables or disables caching of class instances
28
	 *
29
	 * @param bool $value True to enable caching, false to disable it.
30
	 */
31
	public static function cache( bool $value )
32
	{
33
		self::$cache = (boolean) $value;
34
		self::$objects = [];
35
	}
36
37
38
	/**
39
	 * Creates the required controller specified by the given path of controller names
40
	 *
41
	 * Controllers are created by providing only the domain name, e.g.
42
	 * "basket" for the \Aimeos\Controller\Frontend\Basket\Standard or a path of names to
43
	 * retrieve a specific sub-controller if available.
44
	 * Please note, that only the default controllers can be created. If you need
45
	 * a specific implementation, you need to use the factory class of the
46
	 * controller to hand over specifc implementation names.
47
	 *
48
	 * @param \Aimeos\MShop\ContextIface $context Context object required by managers
49
	 * @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "basket"
50
	 * @param string|null $name Name of the controller implementation ("Standard" if null)
51
	 * @return \Aimeos\Controller\Frontend\Iface New frontend controller
52
	 * @throws \Aimeos\Controller\Frontend\Exception If the given path is invalid or the manager wasn't found
53
	 */
54
	public static function create( \Aimeos\MShop\ContextIface $context,
55
		string $path, ?string $name = null ) : \Aimeos\Controller\Frontend\Iface
56
	{
57
		if( empty( $path ) ) {
58
			throw new \Aimeos\Controller\Frontend\Exception( 'Controller path is empty', 400 );
59
		}
60
61
		if( empty( $name ) ) {
62
			$name = $context->config()->get( 'controller/frontend/' . $path . '/name', 'Standard' );
63
		}
64
65
		$iface = '\\Aimeos\\Controller\\Frontend\\' . str_replace( '/', '\\', ucwords( $path, '/' ) ) . '\\Iface';
66
		$classname = '\\Aimeos\\Controller\\Frontend\\' . str_replace( '/', '\\', ucwords( $path, '/' ) ) . '\\' . $name;
67
68
		if( self::$cache === false || !isset( self::$objects[$classname] ) ) {
69
			self::$objects[$classname] = self::createController( $context, $classname, $iface, $path );
70
		}
71
72
		return clone self::$objects[$classname];
73
	}
74
75
76
	/**
77
	 * Injects a manager object for the given path of manager names
78
	 *
79
	 * This method is for testing only and you must call \Aimeos\MShop::cache( false )
80
	 * afterwards!
81
	 *
82
	 * @param string $classname Full name of the class for which the object should be returned
83
	 * @param \Aimeos\Controller\Frontend\Iface|null $object Frontend controller object for the given name or null to clear
84
	 */
85
	public static function inject( string $classname, ?\Aimeos\Controller\Frontend\Iface $object = null )
86
	{
87
		self::$objects['\\' . ltrim( $classname, '\\' )] = $object;
88
	}
89
90
91
	/**
92
	 * Adds the decorators to the controller object.
93
	 *
94
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
95
	 * @param \Aimeos\Controller\Frontend\Common\Iface $controller Controller object
96
	 * @param string $domain Domain name in lower case, e.g. "product"
97
	 * @return \Aimeos\Controller\Frontend\Iface Controller object
98
	 */
99
	protected static function addControllerDecorators( \Aimeos\MShop\ContextIface $context,
100
		\Aimeos\Controller\Frontend\Iface $controller, string $domain ) : \Aimeos\Controller\Frontend\Iface
101
	{
102
		$config = $context->config();
103
		$localClass = str_replace( '/', '\\', ucwords( $domain, '/' ) );
104
105
		$classprefix = '\\Aimeos\\Controller\\Frontend\\' . ucfirst( $localClass ) . '\\Decorator\\';
106
		$decorators = array_reverse( $config->get( 'controller/frontend/' . $domain . '/decorators/local', [] ) );
107
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
108
109
		$classprefix = '\\Aimeos\\Controller\\Frontend\\Common\\Decorator\\';
110
		$decorators = array_reverse( $config->get( 'controller/frontend/' . $domain . '/decorators/global', [] ) );
111
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
112
113
		/** controller/frontend/common/decorators/default
114
		 * Configures the list of decorators applied to all frontend controllers
115
		 *
116
		 * Decorators extend the functionality of a class by adding new aspects
117
		 * (e.g. log what is currently done), executing the methods of the underlying
118
		 * class only in certain conditions (e.g. only for logged in users) or
119
		 * modify what is returned to the caller.
120
		 *
121
		 * This option allows you to configure a list of decorator names that should
122
		 * be wrapped around the original instance of all created controllers:
123
		 *
124
		 *  controller/frontend/common/decorators/default = array( 'decorator1', 'decorator2' )
125
		 *
126
		 * This would wrap the decorators named "decorator1" and "decorator2" around
127
		 * all controller instances in that order. The decorator classes would be
128
		 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" and
129
		 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator2".
130
		 *
131
		 * @param array List of decorator names
132
		 * @since 2014.03
133
		 * @category Developer
134
		 */
135
		$decorators = array_reverse( $config->get( 'controller/frontend/common/decorators/default', [] ) );
136
		$excludes = $config->get( 'controller/frontend/' . $domain . '/decorators/excludes', [] );
137
138
		foreach( $decorators as $key => $name )
139
		{
140
			if( in_array( $name, $excludes ) ) {
141
				unset( $decorators[$key] );
142
			}
143
		}
144
145
		$classprefix = '\\Aimeos\\Controller\\Frontend\\Common\\Decorator\\';
146
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
147
148
		return $controller->setObject( $controller );
149
	}
150
151
152
	/**
153
	 * Adds the decorators to the controller object.
154
	 *
155
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
156
	 * @param \Aimeos\Controller\Frontend\Common\Iface $controller Controller object
157
	 * @param array $decorators List of decorator names that should be wrapped around the controller object
158
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Controller\Frontend\Basket\Decorator\"
159
	 * @return \Aimeos\Controller\Frontend\Iface Controller object
160
	 * @throws \LogicException If class can't be instantiated
161
	 */
162
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context, \Aimeos\Controller\Frontend\Iface $controller,
163
		array $decorators, string $classprefix ) : \Aimeos\Controller\Frontend\Iface
164
	{
165
		$interface = \Aimeos\Controller\Frontend\Iface::class;
166
167
		foreach( $decorators as $name )
168
		{
169
			if( ctype_alnum( $name ) === false ) {
170
				throw new \LogicException( sprintf( 'Invalid class name "%1$s"', $name ), 400 );
171
			}
172
173
			$controller = \Aimeos\Utils::create( $classprefix . $name, [$controller, $context], $interface );
174
		}
175
176
		return $controller;
177
	}
178
179
180
	/**
181
	 * Creates a controller object.
182
	 *
183
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
184
	 * @param string $classname Name of the controller class
185
	 * @param string $interface Name of the controller interface
186
	 * @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "basket"
187
	 * @return \Aimeos\Controller\Frontend\Iface Controller object
188
	 */
189
	protected static function createController( \Aimeos\MShop\ContextIface $context,
190
		string $classname, string $interface, string $path ) : \Aimeos\Controller\Frontend\Iface
191
	{
192
		if( isset( self::$objects[$classname] ) ) {
193
			return self::$objects[$classname];
194
		}
195
196
		$cntl = \Aimeos\Utils::create( $classname, [$context], $interface );
197
198
		return self::addControllerDecorators( $context, $cntl, $path );
199
	}
200
}
201