Di::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
/**
4
 * Dependency Injection Manager
5
 *
6
 * @category  	core
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
/**
18
 * Dependency Injection Manager
19
 *
20
 * @category  	core
21
 * @author    	Judicaël Paquet <[email protected]>
22
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
23
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
24
 * @version   	Release: 1.0.0
25
 * @filesource	https://github.com/las93/venus2
26
 * @link      	https://github.com/las93
27
 * @since     	1.0
28
 */
29
class Di
30
{
31
	/**
32
	 * contener of depency injector for all instances
33
	 *
34
	 * @access private
35
	 * @var    array
36
	 */
37
	private static $_aSharedDependencyInjectorContener = null;
38
	
39
	/**
40
	 * contener of depency injector just for this instance
41
	 *
42
	 * @access private
43
	 * @var    array
44
	 */
45
	private $_aDependencyInjectorContener = null;
46
47
	/**
48
	 * get the injection (no replace it if it exists)
49
	 *
50
	 * @access public
51
	 * @param  string $sNameOfDi name of injection
52
	 * @return mixed
53
	 */
54
	public function get(string $sNameOfDi)
55
	{
56
		if (isset(self::$_aSharedDependencyInjectorContener[md5($sNameOfDi)])) {
57
58
			return self::$_aSharedDependencyInjectorContener[md5($sNameOfDi)];
59
		}
60
		else if (isset($this->_aDependencyInjectorContener[md5($sNameOfDi)])) {
61
		    
62
		    return $this->_aDependencyInjectorContener[md5($sNameOfDi)];
63
		}
64
65
		return false;
66
	}
67
68
	/**
69
	 * get a property
70
	 *
71
	 * @access public
72
	 * @param  string $sNameOfDi name of di
73
	 * @param  callable $cFunction functrion to use when you get this dependance injection
74
	 * @param  bool $bShared indicate if you want shares or not this injection with others instances
75
	 * @return \Venus\core\Di
76
	 */
77
	public function set(string $sNameOfDi, callable $cFunction, bool $bShared = false) : Di
78
	{
79
	    if ($bShared === true) { self::$_aSharedDependencyInjectorContener[md5($sNameOfDi)] = $cFunction; }
80
	    else { $this->_aDependencyInjectorContener[md5($sNameOfDi)] = $cFunction; }
81
		return $this;
82
	}
83
}
84