Skip to content

Commit

Permalink
Add extend method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vegner committed Jul 31, 2017
1 parent 262083d commit ede2428
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ into arbitrary code.
- ConsumerInitializer = ClassName | Consumer
- ServiceInitializer = ConsumerInitializer | Value

Construction
------------
Container is initialized with raw values, no Initalizers allowed:

```php
Expand All @@ -40,6 +42,8 @@ You can set a name which will be used to refer to container itself:
$container = new SD\DependencyInjection\Container([], 'container');
```

Registering services
--------------------
Services are registered with ServiceInitializers:

```php
Expand All @@ -62,6 +66,19 @@ $container->register('helloWorld', function ($name) {
$container->register('name', $container->value('Skywalker'));
```

You can extend registered services:

```php
$container->register('currency', SD\Currency\Repository::class);
$container->extend('currency', function ($container, $currency) {
$store = $container->produce(SD\CurrencyStore\Wpdb::class);
$currency->setStore($store);
return $currency;
});
```

Consumer production
-------------------
Consumers are produced with ConsumerInitializers (Value is not supported):

```php
Expand Down
14 changes: 14 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public function register($name, $initializer) {
}
}

public function extend($name, $extender) {
if (isset($this->services[$name])) {
$this->services[$name] = $this->produce($extender);
} elseif (isset($this->initializers[$name])) {
$initializer = $this->initializers[$name];
$this->initializers[$name] = function () use ($extender, $initializer, $name) {
$this->services[$name] = $this->produce($initializer);
return $this->inject($extender);
};
} else {
throw new Exception("Cannot extend unknown service $name");
}
}

public function value($value) {
return new Value($value);
}
Expand Down
21 changes: 20 additions & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testRegisterClassName() {
],
'container'
);
$serviceName = 'hello_world';
$serviceName = 'helloWorld';
$className = HelloWorldService::class;
$container->register($serviceName, $className);
$service = $container->get($serviceName);
Expand All @@ -30,4 +30,23 @@ public function testDetectCyclicDependencies() {
$this->expectException(Exception::class);
$container->get('a');
}

public function testExtend() {
$name1 = 'Jar Jar Binks';
$name2 = 'Palpatine';
$container = new Container(
[
'name' => $name1,
],
'container'
);
$serviceName = 'helloWorld';
$container->register($serviceName, HelloWorldService::class);
$container->extend($serviceName, function ($helloWorld) use ($name2) {
$helloWorld->setName($name2);
return $helloWorld;
});
$service = $container->get($serviceName);
$this->assertEquals($name2, $service->getName(), 'Must return modified name');
}
}
8 changes: 6 additions & 2 deletions tests/HelloWorldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class HelloWorldService implements DeclarerInterface {

private $name;

public function __construct($name) {
public function __construct(string $name) {
$this->name = $name;
}

public function declareDependencies() {
return ['container'];
}

public function getName() {
public function setName(string $name) {
$this->name = $name;
}

public function getName(): string {
return $this->name;
}

Expand Down

0 comments on commit ede2428

Please sign in to comment.