Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Fix invokable configuration when key is not provided and docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Feb 7, 2018
1 parent 111a895 commit 63575dc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -49,7 +49,7 @@ The `dependencies` sub associative array can contain the following keys:
- `services`: an associative array that maps a key to a specific service instance.
- `invokables`: an associative array that map a key to a constructor-less
service; i.e., for services that do not require arguments to the constructor.
The key and service name may be the same; if they are not, the name is treated
The key and service name may be the same; if they are not, the key is treated
as an alias.
- `factories`: an associative array that maps a service name to a factory class
name, or any callable. Factory classes must be instantiable without arguments,
Expand Down
4 changes: 4 additions & 0 deletions src/Config.php
Expand Up @@ -83,6 +83,10 @@ private function injectInvokables(Container $container, array $dependencies)
}

foreach ($dependencies['invokables'] as $name => $object) {
if (is_int($name)) {
$name = $object;
}

$container[$name] = function (Container $c) use ($object) {
return new $object();
};
Expand Down
15 changes: 15 additions & 0 deletions test/ConfigTest.php
Expand Up @@ -334,4 +334,19 @@ public function testInjectMultipleDelegators()
$service->injected
);
}

public function testInvokablesWithoutAlias()
{
$dependencies = [
'invokables' => [
TestAsset\Service::class,
],
];

(new Config(['dependencies' => $dependencies]))->configureContainer($this->container);

self::assertTrue($this->container->offsetExists(TestAsset\Service::class));
$service = $this->container->offsetGet(TestAsset\Service::class);
self::assertInstanceOf(TestAsset\Service::class, $service);
}
}

0 comments on commit 63575dc

Please sign in to comment.