Skip to content

Commit 00a0087

Browse files
committed
Replaced spl_object_hash with SplObjectStorage for performance
1 parent 0d3b0b9 commit 00a0087

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/Container.php

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

1515
use ArrayAccess;
1616
use IteratorAggregate;
17+
use SplObjectStorage;
1718
use Psr\Container\ContainerInterface;
1819
use BitFrame\Exception\{
1920
ContainerItemNotFoundException,
@@ -27,7 +28,6 @@
2728
use function gettype;
2829
use function sprintf;
2930
use function is_callable;
30-
use function spl_object_hash;
3131
use function array_key_exists;
3232

3333
class Container implements ContainerInterface, ArrayAccess, IteratorAggregate
@@ -38,12 +38,12 @@ class Container implements ContainerInterface, ArrayAccess, IteratorAggregate
3838

3939
private array $instantiated = [];
4040

41-
private array $factories = [];
41+
private SplObjectStorage $factories;
4242

43-
/*public function __construct()
43+
public function __construct()
4444
{
4545
$this->factories = new SplObjectStorage();
46-
}*/
46+
}
4747

4848
/**
4949
* @param string $id
@@ -101,8 +101,7 @@ public function offsetUnset($id): void
101101
unset($this->bag[$id], $this->frozen[$id]);
102102

103103
if (is_object($this->instantiated[$id])) {
104-
$hash = spl_object_hash($this->bag[$id]);
105-
unset($this->factories[$hash]);
104+
unset($this->factories[$this->bag[$id]]);
106105
}
107106
}
108107

@@ -124,8 +123,7 @@ public function freeze(string $id): self
124123

125124
public function factory(object $factory): object
126125
{
127-
$hash = spl_object_hash($factory);
128-
$this->factories[$hash] = $factory;
126+
$this->factories->attach($factory);
129127

130128
return $factory;
131129
}
@@ -160,9 +158,7 @@ public function get($id)
160158
}
161159

162160
if (isset($this->instantiated[$id])) {
163-
$hash = spl_object_hash($this->bag[$id]);
164-
165-
if (isset($this->factories[$hash])) {
161+
if (isset($this->factories[$this->bag[$id]])) {
166162
return $this->bag[$id]($this);
167163
}
168164

0 commit comments

Comments
 (0)