Passed
Push — master ( 29d5f6...586595 )
by Aimeos
04:48
created

jobs/src/Controller/Jobs/Common/Factory/Base.php (1 issue)

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