Test Failed
Push — master ( ac669c...c918ec )
by Jean-Christophe
08:37
created

DiManager::init()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Ubiquity\controllers\di;
4
5
use Ubiquity\cache\CacheManager;
6
7
/**
8
 * Manage dependency injection.
9
 * Ubiquity\controllers\di$DiManager
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.0
14
 * @since Ubiquity 2.1.0
15
 *
16
 */
17
class DiManager {
18
	protected static $key = "controllers/di/";
19
20
	/**
21
	 * Initialize dependency injection cache
22
	 * To use in dev only!
23
	 *
24
	 * @param array $config
25
	 */
26
	public static function init(&$config) {
27
		$controllers = CacheManager::getControllers ();
28
		foreach ( $controllers as $controller ) {
29
			$parser = new DiControllerParser ();
30
			$parser->parse ( $controller, $config );
31
			$injections = $parser->getInjections ();
32
			if (sizeof ( $injections ) > 0) {
33
				self::store ( $controller, $parser->__toString () );
34
			}
35
		}
36
	}
37
38
	protected static function store($controller, $injections) {
39
		CacheManager::$cache->store ( self::getControllerCacheKey ( $controller ), $injections );
40
	}
41
42
	public static function fetch($controller) {
43
		$key = self::getControllerCacheKey ( $controller );
44
		if (CacheManager::$cache->exists ( $key )) {
45
			return CacheManager::$cache->fetch ( $key );
46
		}
47
		return false;
48
	}
49
50
	protected static function getControllerCacheKey($classname) {
51
		if (is_object ( $classname )) {
52
			$classname = get_class ( $classname );
53
		}
54
		return self::$key . \str_replace ( "\\", \DS, $classname );
55
	}
56
}
57
58