Passed
Branch master (901174)
by Aimeos
03:14
created

Base   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 38
dl 0
loc 142
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addControllerDecorators() 0 54 5
A injectController() 0 3 1
A addDecorators() 0 23 5
A createController() 0 15 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package Controller
8
 * @subpackage Frontend
9
 */
10
11
12
namespace Aimeos\Controller\Frontend\Common\Factory;
13
14
15
/**
16
 * Common methods for all factories.
17
 *
18
 * @package Controller
19
 * @subpackage Frontend
20
 */
21
class Base
22
{
23
	private static $objects = [];
24
25
26
	/**
27
	 * Injects a controller object.
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\Controller\Frontend\Iface|null $controller Frontend controller object
33
	 */
34
	public static function injectController( $classname, \Aimeos\Controller\Frontend\Iface $controller = null )
35
	{
36
		self::$objects[$classname] = $controller;
37
	}
38
39
40
	/**
41
	 * Adds the decorators to the controller object.
42
	 *
43
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
44
	 * @param \Aimeos\Controller\Frontend\Common\Iface $controller Controller object
45
	 * @param array $decorators List of decorator names that should be wrapped around the controller object
46
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Controller\Frontend\Basket\Decorator\"
47
	 * @return \Aimeos\Controller\Frontend\Common\Iface Controller object
48
	 */
49
	protected static function addDecorators( \Aimeos\MShop\Context\Item\Iface $context,
50
		\Aimeos\Controller\Frontend\Iface $controller, array $decorators, $classprefix )
51
	{
52
		foreach( $decorators as $name )
53
		{
54
			if( ctype_alnum( $name ) === false )
55
			{
56
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
57
				throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
58
			}
59
60
			$classname = $classprefix . $name;
61
62
			if( class_exists( $classname ) === false ) {
63
				throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
64
			}
65
66
			$controller = new $classname( $controller, $context );
67
68
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Controller\\Frontend\\Common\\Decorator\\Iface', $controller );
69
		}
70
71
		return $controller;
72
	}
73
74
75
	/**
76
	 * Adds the decorators to the controller object.
77
	 *
78
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
79
	 * @param \Aimeos\Controller\Frontend\Common\Iface $controller Controller object
80
	 * @param string $domain Domain name in lower case, e.g. "product"
81
	 * @return \Aimeos\Controller\Frontend\Common\Iface Controller object
82
	 */
83
	protected static function addControllerDecorators( \Aimeos\MShop\Context\Item\Iface $context,
84
		\Aimeos\Controller\Frontend\Iface $controller, $domain )
85
	{
86
		if( !is_string( $domain ) || $domain === '' ) {
0 ignored issues
show
introduced by
The condition is_string($domain) is always true.
Loading history...
87
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid domain "%1$s"', $domain ) );
88
		}
89
90
		$localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $domain ) ) );
91
		$config = $context->getConfig();
92
93
		/** controller/frontend/common/decorators/default
94
		 * Configures the list of decorators applied to all frontend controllers
95
		 *
96
		 * Decorators extend the functionality of a class by adding new aspects
97
		 * (e.g. log what is currently done), executing the methods of the underlying
98
		 * class only in certain conditions (e.g. only for logged in users) or
99
		 * modify what is returned to the caller.
100
		 *
101
		 * This option allows you to configure a list of decorator names that should
102
		 * be wrapped around the original instance of all created controllers:
103
		 *
104
		 *  controller/frontend/common/decorators/default = array( 'decorator1', 'decorator2' )
105
		 *
106
		 * This would wrap the decorators named "decorator1" and "decorator2" around
107
		 * all controller instances in that order. The decorator classes would be
108
		 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" and
109
		 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator2".
110
		 *
111
		 * @param array List of decorator names
112
		 * @since 2014.03
113
		 * @category Developer
114
		 */
115
		$decorators = $config->get( 'controller/frontend/common/decorators/default', [] );
116
		$excludes = $config->get( 'controller/frontend/' . $domain . '/decorators/excludes', [] );
117
118
		foreach( $decorators as $key => $name )
119
		{
120
			if( in_array( $name, $excludes ) ) {
121
				unset( $decorators[$key] );
122
			}
123
		}
124
125
		$classprefix = '\\Aimeos\\Controller\\Frontend\\Common\\Decorator\\';
126
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
127
128
		$classprefix = '\\Aimeos\\Controller\\Frontend\\Common\\Decorator\\';
129
		$decorators = $config->get( 'controller/frontend/' . $domain . '/decorators/global', [] );
130
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
131
132
		$classprefix = '\\Aimeos\\Controller\\Frontend\\' . ucfirst( $localClass ) . '\\Decorator\\';
133
		$decorators = $config->get( 'controller/frontend/' . $domain . '/decorators/local', [] );
134
		$controller = self::addDecorators( $context, $controller, $decorators, $classprefix );
135
136
		return $controller;
137
	}
138
139
140
	/**
141
	 * Creates a controller object.
142
	 *
143
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
144
	 * @param string $classname Name of the controller class
145
	 * @param string $interface Name of the controller interface
146
	 * @return \Aimeos\Controller\Frontend\Common\Iface Controller object
147
	 */
148
	protected static function createController( \Aimeos\MShop\Context\Item\Iface $context, $classname, $interface )
149
	{
150
		if( isset( self::$objects[$classname] ) ) {
151
			return self::$objects[$classname];
152
		}
153
154
		if( class_exists( $classname ) === false ) {
155
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
156
		}
157
158
		$controller = new $classname( $context );
159
160
		\Aimeos\MW\Common\Base::checkClass( $interface, $controller );
161
162
		return $controller;
163
	}
164
165
}
166