/
ServiceManager.php
155 lines (138 loc) · 3.68 KB
/
ServiceManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Concrete\Core\Service\Manager;
use Concrete\Core\Application\Application;
use Concrete\Core\Service\ServiceInterface;
class ServiceManager implements ManagerInterface
{
/**
* @var Application
*/
protected $app;
/**
* @var array
*/
protected $extensions = array();
/**
* @var ServiceInterface[]
*/
protected $services = array();
/**
* Manager constructor.
*
* @param \Concrete\Core\Application\Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Add an extension to this manager.
*
* @param string $handle
* @param string|callable|ServiceInterface $abstract
*/
public function extend($handle, $abstract)
{
$this->extensions[$handle] = $abstract;
}
/**
* An array of handles that have been added with `->extend`.
*
* @return string[]
*/
public function getExtensions()
{
return array_keys($this->extensions);
}
/**
* Does this handle exist?
* This method MUST return true for anything added with `->extend`.
*
* @param $handle
*
* @return bool
*/
public function has($handle)
{
return isset($this->extensions[$handle]);
}
/**
* Get the driver for this handle.
*
* @param string $handle
* @param string $version
*
* @return ServiceInterface|null
*/
public function getService($handle, $version = '')
{
$result = null;
if ($this->has($handle)) {
$key = $handle;
$version = (string) $version;
if ($version !== '') {
$key .= "@$version";
}
if (!isset($this->services[$key])) {
$abstract = $this->extensions[$handle];
$service = $this->buildService($abstract, $version);
if ($service === null) {
throw new \RuntimeException('Invalid service binding.');
}
$this->services[$key] = $service;
}
$result = $this->services[$key];
}
return $result;
}
/**
* Build a service from an abstract.
*
* @param string|callable $abstract
* @param string $version
*
* @return ServiceInterface|null
*/
private function buildService($abstract, $version = '')
{
$resolved = null;
if (is_string($abstract)) {
// If it's a string, throw it at the IoC container
$resolved = $this->app->make($abstract, array($version));
} elseif (is_callable($abstract)) {
// If it's a callable, lets call it with the application and $this
$resolved = $abstract($version, $this->app, $this);
}
return $resolved;
}
/**
* Returns all the available services.
*
* @return ServiceInterface[]
*/
public function getAllServices()
{
$result = array();
foreach ($this->getExtensions() as $handle) {
$result[$handle] = $this->getService($handle);
}
return $result;
}
/**
* Loops through the bound services and returns the ones that are active.
*
* @return ServiceInterface[]
*/
public function getActiveServices()
{
$active = array();
foreach ($this->getExtensions() as $handle) {
$service = $this->getService($handle);
$version = $service->getDetector()->detect();
if ($version !== null) {
$active[] = $this->getService($handle, $version);
}
}
return $active;
}
}