Skip to content

Commit

Permalink
remove join
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Mar 6, 2018
1 parent 3ad3627 commit 12a2bdf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# HH-Container

<<<<<<<

simple light weight service location / dependency injection container

[![Build Status](https://travis-ci.org/ytake/hh-container.svg?branch=master)](https://travis-ci.org/ytake/hh-container)
Expand All @@ -21,7 +18,7 @@ $ hhvm --php $(which composer) require ytake/hh-container

## Usage

```php
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing', $container ==> 'testing');
$container->get('testing'); // return string
Expand All @@ -33,14 +30,14 @@ default *prototype*

### SINGLETON

```php
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('scope:singleton', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::SINGLETON);
```

### PROTOTYPE

```php
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('scope:prototype', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::PROTOTYPE);
```
Expand All @@ -49,7 +46,7 @@ $container->set('scope:prototype', $container ==> new \stdClass(), \Ytake\HHCont

### set parameters

```php
```hack
$container->parameters(
'string className',
'parameter name',
Expand All @@ -58,7 +55,8 @@ $container->parameters(
```

sample class
```php

```hack
final class MessageClass {
public function __construct(protected string $message) {
}
Expand All @@ -79,7 +77,7 @@ final class MessageClient {

### Inject

```php
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('message.class', $container ==> new MessageClass('testing'));
$container->parameters(MessageClient::class, 'message', $container ==> $container->get('message.class'));
Expand All @@ -89,7 +87,7 @@ $instance = $container->get(MessageClient::class);
### callable
returns the value of a callable with parameters supplied at calltime.

```php
```hack
final class TestingInvokable {
public function __invoke(FactoryContainer $container): int {
return 1;
Expand All @@ -109,7 +107,7 @@ $container->set(TestingInvokable::class, $container ==>

## Use modules

```php
```hack
use Ytake\HHContainer\ServiceModule;
use Ytake\HHContainer\FactoryContainer;
Expand All @@ -124,7 +122,7 @@ class ExampleModule extends ServiceModule
```

```php
```hack
$container = new \Ytake\HHContainer\FactoryContainer();
$container->register(ExampleModule::class);
$container->lockModule();
Expand Down
28 changes: 10 additions & 18 deletions src/FactoryContainer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class FactoryContainer implements ContainerInterface {
$arguments = [];
$constructor = $reflectionClass->getConstructor();
if ($constructor instanceof \ReflectionMethod) {
$resolvedParameters =
\HH\Asio\join($this->resolveConstructorParameters($id, $constructor));
$resolvedParameters = $this->resolveConstructorParameters($id, $constructor);
if (count($resolvedParameters)) {
$arguments = $resolvedParameters;
}
Expand All @@ -114,11 +113,11 @@ class FactoryContainer implements ContainerInterface {
throw new ContainerException(sprintf('Error retrieving "%s"', $id));
}

protected async function resolveParameters(
protected function resolveParameters(
string $id,
\ReflectionMethod $constructor,
) : Awaitable<array<mixed>> {
return await $this->resolveConstructorParameters($id, $constructor);
) : array<mixed> {
return $this->resolveConstructorParameters($id, $constructor);
}

<<__Memoize>>
Expand Down Expand Up @@ -149,6 +148,7 @@ class FactoryContainer implements ContainerInterface {
public function flush(): void {
$this->bindings->clear();
$this->scopes->clear();
$this->locked = false;
}

public function remove(string $id): void {
Expand All @@ -165,18 +165,16 @@ class FactoryContainer implements ContainerInterface {
}

public function lockModule(): void {
\HH\Asio\join($this->asyncLockModules());
}

protected async function asyncLockModules(): Awaitable<void> {
await $this->asyncRegister();
foreach ($this->modules->getIterator() as $iterator) {
(new $iterator())->provide($this);
}
$this->locked = true;
}

protected async function resolveConstructorParameters(
protected function resolveConstructorParameters(
string $id,
\ReflectionMethod $constructor,
): Awaitable<array<mixed>> {
): array<mixed> {
$r = [];
if ($parameters = $constructor->getParameters()) {
foreach ($parameters as $parameter) {
Expand All @@ -196,10 +194,4 @@ class FactoryContainer implements ContainerInterface {
public function callable(Invokable $invokable): mixed {
return $invokable->proceed();
}

protected async function asyncRegister(): Awaitable<void> {
foreach ($this->modules->getIterator() as $iterator) {
(new $iterator())->provide($this);
}
}
}

0 comments on commit 12a2bdf

Please sign in to comment.