Skip to content

Commit 39c7b94

Browse files
committed
docs: update README with usage examples and benchmarking details; improve security policy wording
refactor: change container property visibility to protected in benchmarks and compiled container
1 parent e4ad13a commit 39c7b94

4 files changed

Lines changed: 91 additions & 24 deletions

File tree

README.md

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![PHP Version](https://img.shields.io/badge/php-%5E8.4-blue)](https://www.php.net/)
44
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
55
[![Tests](https://img.shields.io/badge/tests-passing-brightgreen)](tests/)
6+
[![Coverage](https://img.shields.io/badge/coverage-report-brightgreen)](#testing)
67
[![CI](https://github.com/atomic-php/container/actions/workflows/ci.yml/badge.svg)](https://github.com/atomic-php/container/actions)
78
[![Codecov](https://codecov.io/gh/atomic-php/container/branch/main/graph/badge.svg)](https://codecov.io/gh/atomic-php/container)
89
[![Packagist](https://img.shields.io/packagist/v/atomic/container)](https://packagist.org/packages/atomic/container)
@@ -57,6 +58,8 @@ composer require atomic/container
5758

5859
## Quick Start
5960

61+
### Basic Usage
62+
6063
```php
6164
<?php
6265

@@ -87,6 +90,8 @@ $uuid1 = $container->get('uuid');
8790
$uuid2 = $container->get('uuid'); // different
8891
```
8992

93+
Note: This package is framework‑agnostic and can be used with any PSR‑11 consumer.
94+
9095
## Architecture
9196

9297
### Core Components
@@ -99,6 +104,20 @@ $uuid2 = $container->get('uuid'); // different
99104
└────────────────────┘ └───────────────────────┘
100105
```
101106

107+
### Compilation Process
108+
109+
1. Boot Time: Builder collects instances, factories and aliases and compiles them into an immutable container.
110+
2. Runtime: `get()` performs O(1) lookup with alias pre-resolution and lazy singleton caching.
111+
3. Caching: Shared factories are resolved once and cached on first use.
112+
113+
```php
114+
// This happens ONCE at boot:
115+
$container = $builder->compile();
116+
117+
// This happens MILLIONS of times at runtime:
118+
$service = $container->get('service'); // ⚡ Fast path
119+
```
120+
102121
### Resolution Model
103122

104123
1. `alias(id)` → resolve to final target (pre‑resolved at compile)
@@ -158,19 +177,67 @@ composer cs-check
158177

159178
`composer test-coverage` generates `coverage.xml` (Clover) used by CI; the workflow uploads it to Codecov for the badge/report.
160179

161-
## Benchmarks
180+
## Benchmarking
181+
182+
Measure performance with the built-in benchmark suite:
162183

163184
```bash
185+
# Run all benchmarks
164186
composer benchmark
187+
188+
# View detailed performance metrics
189+
php benchmarks/run-benchmarks.php
190+
```
191+
192+
### Benchmark Results
193+
194+
```text
195+
Container Benchmark:
196+
benchGetInstance : 3,409,676 ops/sec (0.000 ms/op)
197+
benchGetSingleton : 3,419,738 ops/sec (0.000 ms/op)
198+
benchGetFactory : 1,877,929 ops/sec (0.001 ms/op)
199+
benchGetAlias : 3,254,276 ops/sec (0.000 ms/op)
200+
benchHasHit : 3,323,349 ops/sec (0.000 ms/op)
201+
benchHasMiss : 3,132,513 ops/sec (0.000 ms/op)
202+
203+
Compile Benchmark:
204+
benchCompile5 : 450,885 ops/sec (0.002 ms/op)
205+
benchCompile20 : 139,451 ops/sec (0.007 ms/op)
165206
```
166207

167-
Includes:
208+
## Code Quality
209+
210+
Maintain code quality with included tools:
211+
212+
```bash
213+
# Check code style
214+
composer cs-check
215+
216+
# Fix code style
217+
composer cs-fix
218+
219+
# Run static analysis
220+
composer psalm
221+
222+
# Run all quality checks
223+
composer qa
224+
```
225+
226+
## Contributing
227+
228+
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
229+
230+
## Changelog
231+
232+
See [CHANGELOG.md](CHANGELOG.md) for release notes and version history.
233+
234+
## License
168235

169-
- `ContainerBenchmark` — get()/has() performance for instances, singletons, factories, aliases
170-
- `ContainerCompileBenchmark` — compile time with small/medium registration sets
236+
This project is licensed under the MIT License — see [LICENSE](LICENSE).
171237

172-
## Notes
238+
### Test Coverage
173239

174-
- Immutable runtime container: build everything up front via the builder, then share the compiled instance.
175-
- Aliases are cycle‑checked during compilation; self‑aliasing and cycles throw.
176-
- The container is intentionally lean and unopinionated; add higher‑level features (e.g., providers/autowiring) in your app layer if desired.
240+
- Registrations and aliasing (including chains and cycle detection)
241+
- Singleton caching and factory creation
242+
- Error propagation (wrapping into ContainerException)
243+
- Immutability of compiled container

SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Security Policy
22

3-
Report security issues privately to the maintainers. Do not open public issues for vulnerabilities.
3+
Report security issues protectedly to the maintainers. Do not open public issues for vulnerabilities.
44

benchmarks/ContainerBenchmark.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
final class ContainerBenchmark implements BenchmarkInterface
1111
{
12-
private ContainerInterface $container;
12+
protected ContainerInterface $container;
1313

1414
public function setUp(): void
1515
{
16-
$b = new ContainerBuilder();
16+
$b = new ContainerBuilder;
1717
$b->set('config', ['env' => 'prod']);
18-
$b->set('instance', new \stdClass());
19-
$b->set('shared', fn () => new \stdClass(), shared: true);
20-
$b->factory('factory', fn () => new \stdClass());
18+
$b->set('instance', new \stdClass);
19+
$b->set('shared', fn () => new \stdClass, shared: true);
20+
$b->factory('factory', fn () => new \stdClass);
2121
$b->alias('svc', 'shared');
2222
$this->container = $b->compile();
2323
}

src/CompiledContainer.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
final class CompiledContainer implements ContainerInterface
2020
{
2121
/**
22-
* @param array<string,mixed> $instances
23-
* @param array<string,callable(ContainerInterface):mixed> $sharedFactories
24-
* @param array<string,callable(ContainerInterface):mixed> $factories
25-
* @param array<string,string> $aliases
22+
* @param array<string,mixed> $instances
23+
* @param array<string,callable(ContainerInterface):mixed> $sharedFactories
24+
* @param array<string,callable(ContainerInterface):mixed> $factories
25+
* @param array<string,string> $aliases
2626
*/
2727
public function __construct(
28-
private array $instances,
29-
private array $sharedFactories,
30-
private array $factories,
31-
private array $aliases,
32-
) {
33-
}
28+
protected array $instances,
29+
protected array $sharedFactories,
30+
protected array $factories,
31+
protected array $aliases,
32+
) {}
3433

3534
#[\Override]
3635
public function get(string $id): mixed
@@ -78,6 +77,7 @@ public function get(string $id): mixed
7877
public function has(string $id): bool
7978
{
8079
$id = $this->aliases[$id] ?? $id;
80+
8181
return \array_key_exists($id, $this->instances)
8282
|| isset($this->sharedFactories[$id])
8383
|| isset($this->factories[$id]);

0 commit comments

Comments
 (0)