Completed
Pull Request — master (#11546)
by Steve
55:05
created

PhpDiResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 4
cts 12
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getScope() 0 2 1
A isResolvable() 0 2 1
A resolve() 0 2 1
A getName() 0 2 1
A __construct() 0 3 1
1
<?php
2
3
namespace Elgg\Di;
4
5
use DI\Definition\SelfResolvingDefinition;
6
use Psr\Container\ContainerInterface;
7
use DI\Definition\CacheableDefinition;
8
use DI\Scope;
9
10
/**
11
 * Cacheable PHP-DI definition that pulls from Elgg's DIC.
12
 *
13
 * We could just use Closures that call _elgg_services(), but with current PHPDI, they
14
 * can't be serialized for the cache. Even when PHPDI uses BetterReflection, unserializing
15
 * cached Closures may not be any faster than this.
16
 */
17
class PhpDiResolver implements SelfResolvingDefinition, CacheableDefinition {
18
19
	/**
20
	 * @var string
21
	 */
22
	private $name;
23
24
	/**
25
	 * @var string
26
	 */
27
	private $elgg_dic_key;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param string $name         Entry name. E.g. "Elgg\Menu\Service"
33
	 * @param string $elgg_dic_key Key on Elgg's DIC. E.g. "menus"
34
	 */
35 18
	public function __construct($name, $elgg_dic_key) {
36 18
		$this->name = $name;
37 18
		$this->elgg_dic_key = $elgg_dic_key;
38 18
	}
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	public function getName() {
44
		return $this->name;
45
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50
	public function getScope() {
51
		return Scope::SINGLETON;
52
	}
53
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function resolve(ContainerInterface $container) {
59
		return _elgg_services()->{$this->elgg_dic_key};
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65
	public function isResolvable(ContainerInterface $container) {
66
		return true;
67
	}
68
}
69