1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
5
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
7
|
|
|
* @package Controller |
8
|
|
|
* @subpackage Common |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\Controller\Common\Order; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Common order controller factory. |
17
|
|
|
* |
18
|
|
|
* @package Controller |
19
|
|
|
* @subpackage Common |
20
|
|
|
*/ |
21
|
|
|
class Factory |
22
|
|
|
{ |
23
|
|
|
private static $objects = []; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates a new controller specified by the given name. |
28
|
|
|
* |
29
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object required by controllers |
30
|
|
|
* @param string|null $name Name of the controller or "Standard" if null |
31
|
|
|
* @return \Aimeos\Controller\Common\Order\Iface New order controller object |
32
|
|
|
* @throws \Aimeos\Controller\Common\Exception |
33
|
|
|
*/ |
34
|
|
|
public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name = null ) |
35
|
|
|
{ |
36
|
|
|
/** controller/common/order/name |
37
|
|
|
* Class name of the used order common controller implementation |
38
|
|
|
* |
39
|
|
|
* Each default common controller can be replace by an alternative imlementation. |
40
|
|
|
* To use this implementation, you have to set the last part of the class |
41
|
|
|
* name as configuration value so the controller factory knows which class it |
42
|
|
|
* has to instantiate. |
43
|
|
|
* |
44
|
|
|
* For example, if the name of the default class is |
45
|
|
|
* |
46
|
|
|
* \Aimeos\Controller\Common\Order\Standard |
47
|
|
|
* |
48
|
|
|
* and you want to replace it with your own version named |
49
|
|
|
* |
50
|
|
|
* \Aimeos\Controller\Common\Order\Myorder |
51
|
|
|
* |
52
|
|
|
* then you have to set the this configuration option: |
53
|
|
|
* |
54
|
|
|
* controller/common/order/name = Myorder |
55
|
|
|
* |
56
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
57
|
|
|
* so take care that the configuration value is exactly named like the last |
58
|
|
|
* part of the class name. |
59
|
|
|
* |
60
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
61
|
|
|
* characters are possible! You should always start the last part of the class |
62
|
|
|
* name with an upper case character and continue only with lower case characters |
63
|
|
|
* or numbers. Avoid chamel case names like "MyOrder"! |
64
|
|
|
* |
65
|
|
|
* @param string Last part of the class name |
66
|
|
|
* @since 2014.07 |
67
|
|
|
* @category Developer |
68
|
|
|
*/ |
69
|
|
|
if( $name === null ) { |
70
|
|
|
$name = $context->getConfig()->get( 'controller/common/order/name', 'Standard' ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if( ctype_alnum( $name ) === false ) |
74
|
|
|
{ |
75
|
|
|
$classname = is_string( $name ) ? '\Aimeos\Controller\Common\Order\\' . $name : '<not a string>'; |
76
|
|
|
throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$iface = \Aimeos\Controller\Common\Order\Iface::class; |
80
|
|
|
$classname = 'Aimeos\Controller\Common\Order\\' . $name; |
81
|
|
|
|
82
|
|
|
if( isset( self::$objects[$classname] ) ) { |
83
|
|
|
return self::$objects[$classname]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if( class_exists( $classname ) === false ) { |
87
|
|
|
throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$controller = new $classname( $context ); |
91
|
|
|
|
92
|
|
|
if( !( $controller instanceof $iface ) ) { |
93
|
|
|
throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $controller; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Injects a controller object. |
102
|
|
|
* |
103
|
|
|
* The object is returned via create() if an instance of the class |
104
|
|
|
* with the name name is requested. |
105
|
|
|
* |
106
|
|
|
* @param string $classname Full name of the class for which the object should be returned |
107
|
|
|
* @param null|\Aimeos\Controller\Common\Order\Iface $controller Frontend controller object |
108
|
|
|
*/ |
109
|
|
|
public static function inject( $classname, \Aimeos\Controller\Common\Order\Iface $controller = null ) |
110
|
|
|
{ |
111
|
|
|
self::$objects[trim( $classname, '\\' )] = $controller; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|