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