1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
6
|
|
|
* @package MShop |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\Controller; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Factory which can create all Frontend controllers |
15
|
|
|
* |
16
|
|
|
* @package \Aimeos\Controller\Frontend |
17
|
|
|
*/ |
18
|
|
|
class Frontend |
19
|
|
|
{ |
20
|
|
|
static private $cache = true; |
21
|
|
|
static private $controllers = []; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Enables or disables caching of class instances |
26
|
|
|
* |
27
|
|
|
* @param boolean $value True to enable caching, false to disable it. |
28
|
|
|
* @return boolean Previous cache setting |
29
|
|
|
*/ |
30
|
|
|
static public function cache( $value ) |
31
|
|
|
{ |
32
|
|
|
$old = self::$cache; |
33
|
|
|
self::$cache = (boolean) $value; |
34
|
|
|
|
35
|
|
|
return $old; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Removes all controller objects from the cache |
41
|
|
|
* |
42
|
|
|
* If neither a context ID nor a path is given, the complete cache will be pruned. |
43
|
|
|
* |
44
|
|
|
* @param integer $id Context ID the objects have been created with (string of \Aimeos\MShop\Context\Item\Iface) |
45
|
|
|
* @param string $path Path describing the controller to clear, e.g. "basket" |
46
|
|
|
*/ |
47
|
|
|
static public function clear( $id = null, $path = null ) |
48
|
|
|
{ |
49
|
|
|
if( $id !== null ) |
50
|
|
|
{ |
51
|
|
|
if( $path !== null ) { |
52
|
|
|
self::$controllers[$id][$path] = null; |
53
|
|
|
} else { |
54
|
|
|
self::$controllers[$id] = []; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
self::$controllers = []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates the required controller specified by the given path of controller names |
66
|
|
|
* |
67
|
|
|
* Controllers are created by providing only the domain name, e.g. |
68
|
|
|
* "basket" for the \Aimeos\Controller\Frontend\Basket\Standard or a path of names to |
69
|
|
|
* retrieve a specific sub-controller if available. |
70
|
|
|
* Please note, that only the default controllers can be created. If you need |
71
|
|
|
* a specific implementation, you need to use the factory class of the |
72
|
|
|
* controller to hand over specifc implementation names. |
73
|
|
|
* |
74
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object required by managers |
75
|
|
|
* @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "basket" |
76
|
|
|
* @return \Aimeos\Controller\Frontend\Iface New frontend controller |
77
|
|
|
* @throws \Aimeos\Controller\Frontend\Exception If the given path is invalid or the manager wasn't found |
78
|
|
|
*/ |
79
|
|
|
static public function create( \Aimeos\MShop\Context\Item\Iface $context, $path ) |
80
|
|
|
{ |
81
|
|
|
if( empty( $path ) ) { |
82
|
|
|
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Controller path is empty' ) ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$id = (string) $context; |
86
|
|
|
|
87
|
|
|
if( self::$cache === false || !isset( self::$controllers[$id][$path] ) ) |
88
|
|
|
{ |
89
|
|
|
$parts = explode( '/', $path ); |
90
|
|
|
|
91
|
|
|
foreach( $parts as $key => $part ) |
92
|
|
|
{ |
93
|
|
|
if( ctype_alnum( $part ) === false ) { |
94
|
|
|
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid characters in controller name "%1$s" in "%2$s"', $part, $path ) ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$parts[$key] = ucwords( $part ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$factory = '\\Aimeos\\Controller\\Frontend\\' . join( '\\', $parts ) . '\\Factory'; |
101
|
|
|
|
102
|
|
|
if( class_exists( $factory ) === false ) { |
103
|
|
|
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Class "%1$s" not available', $factory ) ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$manager = call_user_func_array( array( $factory, 'createController' ), array( $context ) ); |
107
|
|
|
|
108
|
|
|
if( $manager === false ) { |
109
|
|
|
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid factory "%1$s"', $factory ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
self::$controllers[$id][$path] = $manager; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return self::$controllers[$id][$path]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|