Skip to content

Commit

Permalink
In 'Registry', factory closures are passed the 'Registry' instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
danbettles committed Nov 24, 2022
1 parent a5596ec commit 50c6fae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

No unreleased changes.

## [2.1.0] - 2022-11-24

### Added

- In `Registry`, factory closures are passed the `Registry` instance.

## [2.0.0] - 2022-11-23

### Changed
Expand All @@ -28,7 +34,8 @@ No unreleased changes.

First stable release.

[unreleased]: https://github.com/danbettles/marigold/compare/v2.0.0...HEAD
[unreleased]: https://github.com/danbettles/marigold/compare/v2.1.0...HEAD
[2.1.0]: https://github.com/danbettles/marigold/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/danbettles/marigold/compare/v1.0.1...v2.0.0
[1.0.1]: https://github.com/danbettles/marigold/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/danbettles/marigold/releases/tag/v1.0.0
2 changes: 1 addition & 1 deletion src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function createService(string $id): object

// A closure:

$service = $factory();
$service = $factory($this);

if (!is_object($service)) {
throw new RuntimeException("The factory for `{$id}` does not return an object.");
Expand Down
14 changes: 14 additions & 0 deletions tests/src/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,18 @@ public function testGetThrowsAnExceptionIfTheFactoryClosureDoesNotReturnAnObject
->get($elementId)
;
}

public function testFactoriesArePassedTheRegistryInstance(): void
{
$registry = new Registry();

$registry
->addFactory('foo', function ($shouldBeTheRegistry) use ($registry) {
$this->assertSame($registry, $shouldBeTheRegistry);

return new stdClass();
})
->get('foo')
;
}
}

0 comments on commit 50c6fae

Please sign in to comment.