Skip to content

Commit c746f0f

Browse files
committed
Use single property for caching
1 parent a9bc272 commit c746f0f

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [0.3.0] - 2018-10-17
10+
### Added
11+
- The `Container::getCacheFile()` now accepts an optional callable parameter for encoding values.
12+
813
### Changed
14+
- The container now stores cached information only in a single property
915
- Improved tests and some failure conditions with mutation testing
1016

1117
## [0.2.1] - 2018-07-18
@@ -26,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2632
### Added
2733
- Initial development release
2834

29-
[Unreleased]: https://github.com/simply-framework/container/compare/v0.2.1...HEAD
35+
[Unreleased]: https://github.com/simply-framework/container/compare/v0.3.0...HEAD
36+
[0.3.0]: https://github.com/simply-framework/container/compare/v0.2.1...v0.3.0
3037
[0.2.1]: https://github.com/simply-framework/container/compare/v0.2.0...v0.2.1
3138
[0.2.0]: https://github.com/simply-framework/container/compare/v0.1.0...v0.2.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"psr/container-implementation": "^1.0"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^7.3",
28+
"phpunit/phpunit": "^7.4",
2929
"riimu/php-cs-fixer-config": "^0.1.1",
3030
"riimu/sami-config": "^0.1.0"
3131
},

src/Container.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
*/
2020
class Container implements ContainerInterface, \ArrayAccess
2121
{
22-
/** @var string[] Lists the names of classes used by different container entries */
23-
protected $types = [];
24-
25-
/** @var array[] Cached container entry parameters for initializing the entries */
26-
protected $parameters = [];
22+
/** @var array[] Information about cached container entries */
23+
protected $entries = [];
2724

2825
/** @var EntryInterface[] Cached entries used to resolve values */
2926
private $entryCache;
@@ -55,27 +52,27 @@ public function setDelegate(ContainerInterface $delegate): void
5552

5653
/**
5754
* Returns PHP code for a cached container that can be loaded quickly on runtime.
55+
* @param callable $encoder Encoder to turn the cached entry data into PHP code or null for default
5856
* @return string The PHP code for the cached container
5957
* @throws ContainerException If the container contains entries that cannot be cached
6058
*/
61-
public function getCacheFile(): string
59+
public function getCacheFile(callable $encoder = null): string
6260
{
63-
$this->loadCacheParameters();
61+
if (\is_null($encoder)) {
62+
$encoder = function ($value): string {
63+
return var_export($value, true);
64+
};
65+
}
6466

65-
ksort($this->types);
66-
ksort($this->parameters);
67+
$this->loadCacheParameters();
68+
ksort($this->entries);
69+
$encoded = $encoder($this->entries);
6770

68-
$template = <<<'TEMPLATE'
71+
return <<<TEMPLATE
6972
<?php return new class extends \Simply\Container\Container {
70-
protected $types = ['TYPES'];
71-
protected $parameters = ['PARAMETERS'];
73+
protected \$entries = $encoded;
7274
};
7375
TEMPLATE;
74-
75-
return strtr($template, [
76-
"['TYPES']" => var_export($this->types, true),
77-
"['PARAMETERS']" => var_export($this->parameters, true),
78-
]);
7976
}
8077

8178
/**
@@ -84,7 +81,7 @@ public function getCacheFile(): string
8481
*/
8582
private function loadCacheParameters(): void
8683
{
87-
foreach ($this->types as $id => $class) {
84+
foreach ($this->entries as $id => $data) {
8885
if (!isset($this->entryCache[$id])) {
8986
continue;
9087
}
@@ -95,7 +92,7 @@ private function loadCacheParameters(): void
9592
throw new ContainerException("Unable to cache entry '$id', the cache parameters are not static");
9693
}
9794

98-
$this->parameters[$id] = $parameters;
95+
$this->entries[$id] = [\get_class($this->entryCache[$id]), $parameters];
9996
}
10097
}
10198

@@ -127,11 +124,11 @@ private function isConstantValue($value): bool
127124
*/
128125
public function addEntry(string $id, EntryInterface $type): void
129126
{
130-
if (isset($this->types[$id])) {
127+
if (isset($this->entries[$id])) {
131128
throw new ContainerException("Entry for identifier '$id' already exists");
132129
}
133130

134-
$this->types[$id] = \get_class($type);
131+
$this->entries[$id] = [];
135132
$this->entryCache[$id] = $type;
136133
}
137134

@@ -165,20 +162,21 @@ public function get($id)
165162
* @param string $id The entry identifier to look for
166163
* @return EntryInterface The container entry for the given identifier
167164
* @throws NotFoundExceptionInterface If the entry cannot be found
165+
* @throws ContainerException If the cached type is not a valid entry type
168166
*/
169167
private function getEntry(string $id): EntryInterface
170168
{
171169
if (isset($this->entryCache[$id])) {
172170
return $this->entryCache[$id];
173171
}
174172

175-
if (isset($this->types[$id])) {
176-
/** @var EntryInterface $entryClass */
177-
$entryClass = $this->types[$id];
178-
return $entryClass::createFromCacheParameters($this->parameters[$id]);
173+
if (!isset($this->entries[$id])) {
174+
throw new NotFoundException("No entry was found for the identifier '$id'");
179175
}
180176

181-
throw new NotFoundException("No entry was found for the identifier '$id'");
177+
/** @var EntryInterface $class */
178+
[$class, $parameters] = $this->entries[$id];
179+
return $class::createFromCacheParameters($parameters);
182180
}
183181

184182
/**
@@ -188,7 +186,7 @@ private function getEntry(string $id): EntryInterface
188186
*/
189187
public function has($id): bool
190188
{
191-
return isset($this->types[$id]);
189+
return isset($this->entries[$id]);
192190
}
193191

194192
/**
@@ -231,8 +229,7 @@ public function offsetSet($offset, $value): void
231229
public function offsetUnset($offset): void
232230
{
233231
unset(
234-
$this->types[$offset],
235-
$this->parameters[$offset],
232+
$this->entries[$offset],
236233
$this->entryCache[$offset],
237234
$this->valueCache[$offset]
238235
);

tests/tests/ContainerTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,10 @@ public function testSortingOnCache()
194194
'a' => 'a_value',
195195
]);
196196

197-
$types = new \ReflectionProperty($container, 'types');
198-
$types->setAccessible(true);
199-
$parameters = new \ReflectionProperty($container, 'parameters');
200-
$parameters->setAccessible(true);
197+
$entries = new \ReflectionProperty($container, 'entries');
198+
$entries->setAccessible(true);
201199

202-
$this->assertSame(['a', 'b', 'c'], array_keys($types->getValue($container)));
203-
$this->assertSame(['a', 'b', 'c'], array_keys($parameters->getValue($container)));
200+
$this->assertSame(['a', 'b', 'c'], array_keys($entries->getValue($container)));
204201
}
205202

206203
private function withContainer(array $values, \Closure $suite)

0 commit comments

Comments
 (0)