Skip to content

Commit

Permalink
Merge pull request #6 from ytake/feature/dependency-injection
Browse files Browse the repository at this point in the history
Feature/dependency injection
  • Loading branch information
ytake authored Oct 31, 2017
2 parents bcf6588 + b683aef commit b00082c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 48 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ sudo: required
dist: trusty
php:
- hhvm
- hhvm-nightly
- hhvm-3.18
- hhvm-3.15
- hhvm-3.12
install: composer install
script:
- hh_client
Expand Down
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# Headacke
# HH-Container

<<<<<<<

simple light weight service location / dependency injection container

**H** ead **ack** e

[![Build Status](https://travis-ci.org/ytake/headacke.svg?branch=develop
)](https://travis-ci.org/ytake/headacke)
[![Build Status](https://travis-ci.org/ytake/hh-container.svg?branch=develop
)](https://travis-ci.org/ytake/hh-container)

## Installation

```bash
$ hhvm --php $(which composer) require ytake/headacke
$ hhvm --php $(which composer) require ytake/hh-container
```

```json
"require": {
"hhvm": ">=3.11.0",
"ytake/headacke": "~0.0"
"ytake/hh-container": "~0.0"
},
```

## Usage

```php
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing', $container ==> 'testing');
$container->get('testing'); // return string
```
Expand All @@ -37,15 +35,15 @@ default *prototype*
### SINGLETON

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

### PROTOTYPE

```php
$container = new \Headacke\FactoryContainer();
$container->set('scope:prototype', $container ==> new \stdClass(), \Headacke\Scope::PROTOTYPE);
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('scope:prototype', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::PROTOTYPE);
```

## Dependency Injection
Expand Down Expand Up @@ -83,7 +81,7 @@ final class MessageClient {
### Inject

```php
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('message.class', $container ==> new MessageClass('testing'));
$container->parameters(MessageClient::class, 'message', $container ==> $container->get('message.class'));
$instance = $container->get(MessageClient::class);
Expand All @@ -93,8 +91,8 @@ $instance = $container->get(MessageClient::class);

```php

use Headacke\ServiceModule;
use Headacke\FactoryContainer;
use Ytake\HHContainer\ServiceModule;
use Ytake\HHContainer\FactoryContainer;

class ExampleModule extends ServiceModule
{
Expand All @@ -107,7 +105,7 @@ class ExampleModule extends ServiceModule
```

```php
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->register(ExampleModule::class);
$container->lockModule();
```
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ytake/headacke",
"name": "ytake/hh-container",
"description": "simple light weight service location / dependency injection container",
"minimum-stability": "stable",
"type": "library",
Expand All @@ -10,19 +10,24 @@
"container"
],
"require": {
"hhvm": ">=3.11",
"ytake/psr-container-hhi": "~1.0"
"hhvm": ">=3.12",
"hhvm/hhvm-autoload": "^1.5",
"ytake/psr-container-hhi": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
"91carriage/phpunit-hhi": "^5.6",
"facebook/hhvm-autoload": "^1.3"
"91carriage/phpunit-hhi": "^5.7.1"
},
"autoload": {
"psr-4": {
"Headacke\\": "src/"
"Ytake\\HHContainer\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/"
]
},
"scripts": {
"test": "./vendor/bin/phpunit"
}
Expand Down
7 changes: 6 additions & 1 deletion hh_autoload.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"roots": [ "src/" ]
"roots": [
"src/"
],
"devRoots": [
"tests/"
]
}
2 changes: 1 addition & 1 deletion src/ContainerException.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Copyright (c) 2017 Yuuki Takezawa
*
*/
namespace Headacke;
namespace Ytake\HHContainer;

use Psr\Container\ContainerExceptionInterface;

Expand Down
8 changes: 5 additions & 3 deletions src/FactoryContainer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Copyright (c) 2017 Yuuki Takezawa
*
*/
namespace Headacke;
namespace Ytake\HHContainer;

use Closure;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -150,8 +150,10 @@ class FactoryContainer implements ContainerInterface
{
if ($parameters = $constructor->getParameters()) {
foreach ($parameters as $parameter) {
if (isset($this->parameters[$id][$parameter->getName()])) {
yield call_user_func($this->parameters[$id][$parameter->getName()], $this);
if (isset($this->parameters[$id])){
if (isset($this->parameters[$id][$parameter->getName()])) {
yield call_user_func($this->parameters[$id][$parameter->getName()], $this);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NotFoundException.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Copyright (c) 2017 Yuuki Takezawa
*
*/
namespace Headacke;
namespace Ytake\HHContainer;

use Psr\Container\NotFoundExceptionInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/ServiceModule.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Copyright (c) 2017 Yuuki Takezawa
*
*/
namespace Headacke;
namespace Ytake\HHContainer;

abstract class ServiceModule
{
Expand Down
32 changes: 16 additions & 16 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?hh // strict

class ContainerTest extends \PHPUnit\Framework\TestCase
final class ContainerTest extends \PHPUnit\Framework\TestCase
{
public function testShouldReturnPrimitiveTypes(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing', $container ==> 'testing');
$this->assertSame('testing', $container->get('testing'));

Expand All @@ -17,54 +17,54 @@ public function testShouldReturnPrimitiveTypes(): void

public function testShouldReturnSingletonObject(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set(
'testing:testing',
$container ==> new \stdClass(),
\Headacke\Scope::SINGLETON
\Ytake\HHContainer\Scope::SINGLETON
);
$this->assertInstanceOf(\stdClass::class, $container->get('testing:testing'));
$this->assertSame($container->get('testing:testing'), $container->get('testing:testing'));
}

public function testShouldReturnPrototypeObject(): void
{
$container = new \Headacke\FactoryContainer();
$container->set('testing:testing', $container ==> new \stdClass(), \Headacke\Scope::PROTOTYPE);
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing:testing', $container ==> new \stdClass(), \Ytake\HHContainer\Scope::PROTOTYPE);
$this->assertInstanceOf(\stdClass::class, $container->get('testing:testing'));
$this->assertNotSame($container->get('testing:testing'), $container->get('testing:testing'));
}

public function testShouldReturunResolveInstance(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('testing', $container ==> 1);
$container->set(
'testing:testing',
$container ==> $container->get('testing'),
\Headacke\Scope::PROTOTYPE
\Ytake\HHContainer\Scope::PROTOTYPE
);
$this->assertSame(1, $container->get('testing:testing'));
}

public function testShouldThrowException() :void
{
$this->expectException(\Headacke\NotFoundException::class);
$container = new \Headacke\FactoryContainer();
$this->expectException(\Ytake\HHContainer\NotFoundException::class);
$container = new \Ytake\HHContainer\FactoryContainer();
$container->get('testing');
}

public function testShouldReturnProvideInstance(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->register(StubModule::class);
$container->lockModule();
$this->assertInstanceOf(\stdClass::class, $container->get('provide:sample'));
}

public function testShouldResolveInstance(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$this->assertInstanceOf(\stdClass::class, $container->get(\stdClass::class));
$container->parameters(ResolvedObject::class, 'object', $container ==> new \stdClass());
$container->parameters(ResolvedObject::class, 'integer', $container ==> 100);
Expand All @@ -74,7 +74,7 @@ public function testShouldResolveInstance(): void

public function testShouldResolveConstructorPromotionInstance(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->parameters(ConstructorPromotionClass::class, 'object', $container ==> new \stdClass());
$container->parameters(ConstructorPromotionClass::class, 'integer', $container ==> 100);
$instance = $container->get(ConstructorPromotionClass::class);
Expand All @@ -86,7 +86,7 @@ public function testShouldResolveConstructorPromotionInstance(): void

public function testShouldResolveDependencyInjectionWithLocation(): void
{
$container = new \Headacke\FactoryContainer();
$container = new \Ytake\HHContainer\FactoryContainer();
$container->set('message.class', $container ==> new MockMessageClass('testing'));
$container->parameters(MessageClient::class, 'message', $container ==> $container->get('message.class'));
$instance = $container->get(MessageClient::class);
Expand All @@ -96,9 +96,9 @@ public function testShouldResolveDependencyInjectionWithLocation(): void
}
}

class StubModule extends \Headacke\ServiceModule
class StubModule extends \Ytake\HHContainer\ServiceModule
{
public function provide(\Headacke\FactoryContainer $container): void
public function provide(\Ytake\HHContainer\FactoryContainer $container): void
{
$container->set('provide:sample', $container ==> new \stdClass());
}
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?hh

require_once __DIR__ . "/../vendor/bin/phpunit";

0 comments on commit b00082c

Please sign in to comment.