Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 82d0ee1

Browse files
author
fhein
committed
Removed redundant $resolvedAliases array. Aliases are resolved when
defined. Simplified CyclicAliasException.
1 parent 66296a5 commit 82d0ee1

File tree

3 files changed

+73
-116
lines changed

3 files changed

+73
-116
lines changed

src/Exception/CyclicAliasException.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
class CyclicAliasException extends InvalidArgumentException
1313
{
14+
public static $cycle;
1415
/**
1516
* @param string conflicting alias key
1617
* @param string[] $aliases map of referenced services, indexed by alias name (string)
@@ -19,16 +20,24 @@ class CyclicAliasException extends InvalidArgumentException
1920
*/
2021
public static function fromCyclicAlias($alias, $aliases)
2122
{
22-
$msg = $alias;
23+
$cycle = $alias;
2324
$cursor = $alias;
24-
while($aliases[$cursor] !== $alias) {
25+
while (isset($aliases[$cursor]) && $aliases[$cursor] !== $alias) {
2526
$cursor = $aliases[$cursor];
26-
$msg .= ' -> '. $cursor;
27+
$cycle .= ' -> '. $cursor;
2728
}
28-
$msg .= ' -> ' . $alias . PHP_EOL;
29+
$cycle .= ' -> ' . $alias . "\n";
30+
31+
// for testing
32+
self::$cycle = $cycle;
2933
return new self(sprintf(
3034
"A cycle was detected within the aliases defintions:\n%s",
31-
$msg
32-
));
35+
$cycle
36+
));
37+
}
38+
39+
public function getCycle()
40+
{
41+
return self::$cycle;
3342
}
3443
}

src/ServiceManager.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ class ServiceManager implements ServiceLocatorInterface
105105
*/
106106
private $lazyServicesDelegator;
107107

108-
/**
109-
* @var string[]
110-
*/
111-
private $resolvedAliases = [];
108+
// /**
109+
// * @var string[]
110+
// */
111+
// private $resolvedAliases = [];
112112

113113
/**
114114
* A list of already loaded services (this act as a local cache)
@@ -200,7 +200,7 @@ public function get($name)
200200

201201
// We achieve better performance if we can let all alias
202202
// considerations out
203-
if (empty($this->resolvedAliases)) {
203+
if (empty($this->aliases)) {
204204
$object = $this->doCreate($name);
205205

206206
// Cache the object for later, if it is supposed to be shared.
@@ -211,7 +211,7 @@ public function get($name)
211211
}
212212

213213
// Here we have to deal with requests which may be aliases
214-
$resolvedName = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
214+
$resolvedName = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;
215215

216216
// Can only become true, if the requested service is an shared alias
217217
$sharedAlias = $sharedService && isset($this->services[$resolvedName]);
@@ -244,7 +244,7 @@ public function get($name)
244244
public function build($name, array $options = null)
245245
{
246246
// We never cache when using "build"
247-
$name = $this->resolvedAliases[$name] ?? $name;
247+
$name = $this->aliases[$name] ?? $name;
248248
return $this->doCreate($name, $options);
249249
}
250250

@@ -253,7 +253,7 @@ public function build($name, array $options = null)
253253
*/
254254
public function has($name)
255255
{
256-
$name = $this->resolvedAliases[$name] ?? $name;
256+
$name = $this->aliases[$name] ?? $name;
257257
$found = isset($this->services[$name]) || isset($this->factories[$name]);
258258

259259
if ($found) {
@@ -904,18 +904,17 @@ private function validateConfig(array $config)
904904
*/
905905
private function doSetAlias($alias, $target)
906906
{
907-
$this->aliases[$alias] = $target;
908-
$this->resolvedAliases[$alias] =
909-
isset($this->resolvedAliases[$target]) ? $this->resolvedAliases[$target] : $target;
907+
$this->aliases[$alias] =
908+
isset($this->aliases[$target]) ? $this->aliases[$target] : $target;
910909

911-
if ($alias === $this->resolvedAliases[$alias]) {
912-
throw CyclicAliasException::fromAliasesMap([$alias]);
910+
if ($alias === $this->aliases[$alias]) {
911+
throw CyclicAliasException::fromCyclicAlias($alias, $this->aliases);
913912
}
914913

915-
if (in_array($alias, $this->resolvedAliases)) {
916-
$r = array_intersect($this->resolvedAliases, [ $alias ]);
914+
if (in_array($alias, $this->aliases)) {
915+
$r = array_intersect($this->aliases, [ $alias ]);
917916
foreach ($r as $name => $service) {
918-
$this->resolvedAliases[$name] = $target;
917+
$this->aliases[$name] = $target;
919918
}
920919
}
921920
}

test/Exception/CyclicAliasExceptionTest.php

Lines changed: 43 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,137 +18,86 @@ class CyclicAliasExceptionTest extends TestCase
1818
/**
1919
* @dataProvider aliasesProvider
2020
*
21+
* @param string conflicting alias key
2122
* @param string[] $aliases
2223
* @param string $expectedMessage
2324
*
2425
* @return void
2526
*/
26-
public function testFromAliasesMap(array $aliases, $expectedMessage)
27+
public function testFromCyclicAlias($alias, array $aliases, $expectedMessage)
2728
{
28-
$exception = CyclicAliasException::fromAliasesMap($aliases);
29+
$exception = CyclicAliasException::fromCyclicAlias($alias, $aliases);
2930

3031
self::assertInstanceOf(CyclicAliasException::class, $exception);
31-
self::assertSame($expectedMessage, $exception->getMessage());
32+
self::assertSame($expectedMessage, $exception->getCycle());
3233
}
3334

3435
/**
36+
* Test data provider for testFromCyclicAlias
37+
*
3538
* @return string[][]|string[][][]
3639
*/
3740
public function aliasesProvider()
3841
{
39-
return [
40-
'empty set' => [
41-
[],
42-
'A cycle was detected within the following aliases map:
43-
44-
[
45-
46-
]'
47-
],
48-
'acyclic set' => [
42+
return
43+
[
44+
[
45+
'a',
4946
[
50-
'b' => 'a',
51-
'd' => 'c',
47+
'a' => 'a',
5248
],
53-
'A cycle was detected within the following aliases map:
54-
55-
[
56-
"b" => "a"
57-
"d" => "c"
58-
]'
49+
"a -> a\n",
5950
],
60-
'acyclic self-referencing set' => [
51+
[
52+
'a',
6153
[
62-
'b' => 'a',
63-
'c' => 'b',
64-
'd' => 'c',
54+
'a' => 'b',
55+
'b' => 'a'
6556
],
66-
'A cycle was detected within the following aliases map:
67-
68-
[
69-
"b" => "a"
70-
"c" => "b"
71-
"d" => "c"
72-
]'
57+
"a -> b -> a\n",
7358
],
74-
'cyclic set' => [
59+
[
60+
'b',
7561
[
76-
'b' => 'a',
7762
'a' => 'b',
63+
'b' => 'a'
7864
],
79-
'Cycles were detected within the provided aliases:
80-
81-
[
82-
"b" => "a" => "b"
83-
]
84-
85-
The cycle was detected in the following alias map:
86-
87-
[
88-
"b" => "a"
89-
"a" => "b"
90-
]'
65+
"b -> a -> b\n",
9166
],
92-
'cyclic set (indirect)' => [
67+
[
68+
'b',
9369
[
70+
'a' => 'b',
9471
'b' => 'a',
95-
'c' => 'b',
96-
'a' => 'c',
9772
],
98-
'Cycles were detected within the provided aliases:
99-
100-
[
101-
"b" => "a" => "c" => "b"
102-
]
103-
104-
The cycle was detected in the following alias map:
105-
106-
[
107-
"b" => "a"
108-
"c" => "b"
109-
"a" => "c"
110-
]'
73+
"b -> a -> b\n",
11174
],
112-
'cyclic set + acyclic set' => [
75+
[
76+
'a',
11377
[
114-
'b' => 'a',
11578
'a' => 'b',
116-
'd' => 'c',
79+
'b' => 'c',
80+
'c' => 'a',
11781
],
118-
'Cycles were detected within the provided aliases:
119-
120-
[
121-
"b" => "a" => "b"
122-
]
123-
124-
The cycle was detected in the following alias map:
125-
126-
[
127-
"b" => "a"
128-
"a" => "b"
129-
"d" => "c"
130-
]'
82+
"a -> b -> c -> a\n",
13183
],
132-
'cyclic set + reference to cyclic set' => [
84+
[
85+
'b',
13386
[
134-
'b' => 'a',
13587
'a' => 'b',
88+
'b' => 'c',
13689
'c' => 'a',
13790
],
138-
'Cycles were detected within the provided aliases:
139-
140-
[
141-
"b" => "a" => "b"
142-
"c" => "a" => "b" => "c"
143-
]
144-
145-
The cycle was detected in the following alias map:
146-
147-
[
148-
"b" => "a"
149-
"a" => "b"
150-
"c" => "a"
151-
]'
91+
"b -> c -> a -> b\n",
92+
],
93+
[
94+
'c',
95+
[
96+
'a' => 'b',
97+
'b' => 'c',
98+
'c' => 'a',
99+
],
100+
"c -> a -> b -> c\n",
152101
],
153102
];
154103
}

0 commit comments

Comments
 (0)