From 15feb34d16a1e25ebff05e81021ce5ec53f1d4af Mon Sep 17 00:00:00 2001 From: David Lombera Date: Fri, 14 Jul 2023 17:40:52 -0600 Subject: [PATCH] docs: :bookmark: update readme / changelog --- CHANGELOG.md | 23 ++++++++++++++++++++--- README.md | 18 +++++++++--------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0586956..67b32b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index a2cf1c6..51bec77 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,9 @@ $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()); @@ -57,18 +57,18 @@ $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 @@ -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