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

PhpDiResolver::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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