-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetServiceCapableCachingTrait.php
120 lines (109 loc) · 3.9 KB
/
GetServiceCapableCachingTrait.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
<?php
namespace Dhii\Di;
use Dhii\Cache\ContainerInterface as CacheContainerInterface;
use Dhii\Util\String\StringableInterface as Stringable;
use Exception as RootException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerInterface as BaseContainerInterface;
use RuntimeException;
/**
* Functionality for service retrieval.
*
* @since [*next-version*]
*/
trait GetServiceCapableCachingTrait
{
/**
* Retrieves a service by key.
*
* @since [*next-version*]
*
* @param string|int|float|bool|Stringable $key The key, for which to get the service.
*
* @throw NotFoundExceptionInterface If no service or definition found for key.
* @throw ContainerExceptionInterface If service or service definition could not be retrieved.
*
* @return mixed The corresponding service.
*/
protected function _getService($key)
{
$cache = $this->_getServiceCache();
$notFoundException = null;
try {
return $cache->get($key, function ($key) use (&$notFoundException) {
try {
$definition = $this->_get($key);
} catch (NotFoundExceptionInterface $e) {
$notFoundException = $e;
throw $e;
}
return $this->_resolveDefinition($definition);
});
} catch (RootException $e) {
if ($notFoundException instanceof NotFoundExceptionInterface) {
throw $notFoundException;
}
throw $this->_throwContainerException($this->__('Could not retrieve service'), null, $e, true);
}
}
/**
* Gets a service definition.
*
* @since [*next-version*]
*
* @param string|int|float|bool|Stringable $key The key, for which to get the definition.
*
* @throws NotFoundExceptionInterface If the key was not found in the container.
* @throws ContainerExceptionInterface If data could not be retrieved from the container.
*
* @return mixed The definition for the specified key.
*/
abstract protected function _get($key);
/**
* Retrieves the service cache.
*
* @since [*next-version*]
*
* @return CacheContainerInterface The cached.
*/
abstract protected function _getServiceCache();
/**
* Resolves a service definition.
*
* Resolving a definition means acquiring an instance of a service according to its definition.
*
* @since [*next-version*]
*
* @param mixed $definition The service definition.
*
* @throws RuntimeException If the definition cannot be resolved.
*
* @return mixed The resolved service.
*/
abstract protected function _resolveDefinition($definition);
/**
* Throws a container exception.
*
* @param string|Stringable|null $message The exception message, if any.
* @param int|string|Stringable|null $code The numeric exception code, if any.
* @param RootException|null $previous The inner exception, if any.
* @param BaseContainerInterface|true|null $container The associated container, if any. Pass `true` to use available container.
*
* @throws ContainerExceptionInterface
*/
abstract protected function _throwContainerException($message = null, $code = null, $previous = null, $container = null);
/**
* Translates a string, and replaces placeholders.
*
* @since [*next-version*]
* @see sprintf()
*
* @param string $string The format string to translate.
* @param array $args Placeholder values to replace in the string.
* @param mixed $context The context for translation.
*
* @return string The translated string.
*/
abstract protected function __($string, $args = array(), $context = null);
}