Skip to content

Commit

Permalink
add linking (multiple aliases) to ioc manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wilson committed Jun 27, 2016
1 parent 8b7bdb7 commit afe4970
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All Notable changes to `Manager` will be documented in this file
- Add integration tests
- Cleanup and refactor
- add `has('$dep.whatever')` interpolation
- add linking (multiple aliases) to ioc manager

# v0.8.9 - 3-11-2016
- Add full composer.json
Expand Down
8 changes: 8 additions & 0 deletions ioc.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ When you're ready to call dependencies:
$manager->fetch('event_dispatcher');
```

You may also register multiple aliases to a single dependency
```php
$manager->di(['one', 'two', 'three'], $factory);
$manager->fetch('one');
$manager->fetch('two');
$manager->fetch('three'); // All the same
```

## Dependencies that need Dependencies
The easiest way to setup a dependency that needs a dependency is to use a closure.
```php
Expand Down
23 changes: 22 additions & 1 deletion src/Traits/ManagesIocTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function getIocManifest()
*/
public function fetch($alias, $fallback = '_michaels_no_fallback')
{
// If this is a link, just go back to the master
$link = $this->getIfExists($this->nameOfIocManifest . ".$alias");
if (is_string($link) && strpos($link, '_michaels_link_') !== false) {
return $this->fetch(str_replace('_michaels_link_', '', $link));
}

// Otherwise, continue
$shared = $this->getIfExists($this->nameOfIocManifest . "._singletons.$alias");

if ($shared instanceof NoItemFoundMessage) {
Expand All @@ -66,7 +73,7 @@ public function fetch($alias, $fallback = '_michaels_no_fallback')
if (is_object($shared)) {
return $shared;

// This is shared, but we must produce and cache it
// This is shared, but we must produce and cache it
} else {
$object = $this->produceDependency($alias, $fallback);
$this->set($this->nameOfIocManifest . "._singletons.$alias", $object);
Expand All @@ -90,13 +97,27 @@ public function fetch($alias, $fallback = '_michaels_no_fallback')
*/
public function di($alias, $factory, array $declared = null)
{
// Setup links, if necessary
if (is_array($alias)) {
$links = $alias;
$alias = $alias[0];
unset($links[0]);
}

$this->set($this->nameOfIocManifest . ".$alias", $factory);

// Setup any declared dependencies
if ($declared) {
$this->set($this->nameOfIocManifest . "._declarations.$alias", $declared);
}

// Add Links
if (!empty($links)) {
foreach ($links as $link) {
$this->set($this->nameOfIocManifest . ".$link", "_michaels_link_$alias");
}
}

return $this;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Scenarios/ManagesIocScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ public function test_has_with_dep()
$this->assertTrue($manager->has('$dep.dependency'), "Failed to interpolate `dep` ");
}

public function test_links()
{
$manager = $this->getManager();
$manager->di(['one', 'two', 'three'], '\\stdClass');

$this->assertInstanceOf('\stdClass', $manager->fetch('one'), "failed to produce the master'");
$this->assertInstanceOf('\stdClass', $manager->fetch('two'), "failed to produce the first link'");
$this->assertInstanceOf('\stdClass', $manager->fetch('three'), "failed to produce the second link'");
}

public function test_complex_example()
{
$this->setupTestData();
Expand Down

0 comments on commit afe4970

Please sign in to comment.