Skip to content

Commit

Permalink
docs: 🔖 update readme / changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
lombervid committed Jul 14, 2023
1 parent f9ec5f2 commit 15feb34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 20 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# Changelog

## [Unreleased]
## [0.4.0]

### Added

- Add `factory` method to register non shared services
- Add `hasFactory` method

### Changed

- Services registered with `register` method now are shared on all calls to `get($id)`

### Removed

- Remove `static` method
- Remove `hasStatic` method

## [0.3.0]

### Added

Expand All @@ -24,6 +40,7 @@

- Changed `EntryNotFoundException` parent from `\InvalidArgumentException` to `NotFoundException`

[Unreleased]: https://github.com/phetit/container/compare/v0.2.0...main

[Unreleased]: https://github.com/phetit/container/compare/v0.4.0...main
[0.4.0]: https://github.com/phetit/container/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/phetit/container/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/phetit/container/compare/v0.1.0...v0.2.0
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ $foo = $container->get('foo');
// $foo === 'bar'
```

### Static services
### Non shared services

By default services are resolved every time you call `get($id)` method.
By default all services are shared. This means that services are resolved only the first time `get($id)` method is called. So in following calls you'll get the same object.

```php
$container->register('service' fn() => new Service());

$serviceOne = $container->get('service'); // Service object
$serviceTwo = $container->get('service'); // Service object

// $serviceOne === $serviceTwo => false
// $serviceOne === $serviceTwo => true
```

If you want to register a service that is resolved only the first time, you can do it using `static()` method:
In order to get a new instance on every call, you need to use the `factory()` method:

```php
$container->static('service' fn() => new Service());
$container->factory('service' fn() => new Service());

$serviceOne = $container->get('service'); // Service object
$serviceTwo = $container->get('service'); // Service object

// $serviceOne === $serviceTwo => true
// $serviceOne === $serviceTwo => false
```

### Parameters
Expand All @@ -77,13 +77,13 @@ You can register parameters using `parameter()` method:

```php
$container->parameter('foo', 'bar');
$container->parameter('func', fn() => new Service());
$container->parameter('closure', fn() => new Service());

$container->get('foo'); // 'bar'

// Parameters are not resolved
$func = $container->get('func'); // $func = fn() => new Service()
$service = $func(); // 'Service object'
$closure = $container->get('closure'); // $closure = fn() => new Service()
$service = $closure(); // 'Service object'
```

### Accessing container from a service
Expand Down

0 comments on commit 15feb34

Please sign in to comment.