Completed
Push — master ( bc3c0c...8086a8 )
by Aimeos
01:52
created

Frontend::clear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
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 $objects = [];
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
		self::$cache = (boolean) $value;
33
		self::$objects = [];
34
	}
35
36
37
	/**
38
	 * Creates the required controller specified by the given path of controller names
39
	 *
40
	 * Controllers are created by providing only the domain name, e.g.
41
	 * "basket" for the \Aimeos\Controller\Frontend\Basket\Standard or a path of names to
42
	 * retrieve a specific sub-controller if available.
43
	 * Please note, that only the default controllers can be created. If you need
44
	 * a specific implementation, you need to use the factory class of the
45
	 * controller to hand over specifc implementation names.
46
	 *
47
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by managers
48
	 * @param string $path Name of the domain (and sub-managers) separated by slashes, e.g "basket"
49
	 * @return \Aimeos\Controller\Frontend\Iface New frontend controller
50
	 * @throws \Aimeos\Controller\Frontend\Exception If the given path is invalid or the manager wasn't found
51
	 */
52
	static public function create( \Aimeos\MShop\Context\Item\Iface $context, $path )
53
	{
54
		if( empty( $path ) ) {
55
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Controller path is empty' ) );
56
		}
57
58
		if( self::$cache === false || !isset( self::$objects[$path] ) )
59
		{
60
			if( ctype_alnum( $path ) === false ) {
61
				throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid characters in controller name "%1$s"', $path ) );
62
			}
63
64
			$factory = '\\Aimeos\\Controller\\Frontend\\' . ucfirst( $path ) . '\\Factory';
65
66
			if( class_exists( $factory ) === false ) {
67
				throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Class "%1$s" not available', $factory ) );
68
			}
69
70
			if( ( $controller = call_user_func_array( [$factory, 'create'], [$context] ) ) === false ) {
71
				throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid factory "%1$s"', $factory ) );
72
			}
73
74
			self::$objects[$path] = $controller;
75
		}
76
77
		return clone self::$objects[$path];
78
	}
79
}
80