Skip to content

Commit

Permalink
Add Container::set()
Browse files Browse the repository at this point in the history
  • Loading branch information
hultberg committed Jun 16, 2018
1 parent 72700b3 commit 3da6126
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.6.3

* Add method `Container::set()`, it is not part of any interface.

## 1.6.2

* Fix resolving builtin types with default values.
Expand Down
15 changes: 15 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public function call($callable, array $parameters = array())
throw new InvokeException('Unsupported format.');
}

/**
* Set an instance/value or an definition to the container.
*
* @param string $id
* @param AbstractDefinition|mixed $value
*/
public function set($id, $value): void
{
if ($value instanceof AbstractDefinition) {
$this->definitions[$id] = $value;
} else {
$this->singletons[$id] = $value;
}
}

/**
* Build a class without cache.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,17 @@ public function testResolvesWithDefault()

$this->assertInstanceOf(ClassWithDefaultArgument::class, $class);
}

public function testSet()
{
$container = new Container();
$container->set(Class1::class, new Class1());
$this->assertInstanceOf(Class1::class, $container->get(Class1::class));

$container->set('hei2', factory(function() {
return 'hei';
}));

$this->assertEquals('hei', $container->get('hei2'));
}
}

0 comments on commit 3da6126

Please sign in to comment.