Skip to content

Commit

Permalink
Merge pull request #30 from ytake/develop
Browse files Browse the repository at this point in the history
merged
  • Loading branch information
ytake committed Dec 13, 2014
2 parents 07b538b + 35a4e20 commit 22fd63e
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 26 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ interface AnnotationRepositoryInterface {}
class AnnotationRepository implements AnnotationRepositoryInterface {}
```

### @Scope
singleton or prototype Scope annotation
required @Component annotation (default prototype)
```php
use Ytake\Container\Annotations\Annotation\Component;

/**
* Interface RepositoryInterface
*/
interface AnnotationRepositoryInterface {}

/**
* @Component
* @Scope("singleton")
* Class Repository
*/
class AnnotationRepository implements AnnotationRepositoryInterface {}
```

or named component annotation
```php
/**
* @Component("hello")
* @Scope("singleton")
* Class Repository
*/
class AnnotationRepository {}
```


### @Autowired
フィールドインジェクション
annotation based field injection(Spring's own **@Autowired**)
Expand All @@ -177,6 +207,7 @@ class TestingClass
```

### @Component Named
named component annotation
クラス自体に名前をつけてコンテナに登録します
```php
/**
Expand All @@ -196,7 +227,7 @@ class Processer
/**
* @Value('hello')
*/
$protected $hello;
protected $hello;
}
```
**詳しくはdemoディレクトリをご覧ください**
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "iono/container",
"description": "spring framework style service container library",
"description": "spring framework style dependency injection container",
"minimum-stability": "stable",
"license": "MIT",
"keywords": ["container", "field injection", "reflection", "annotation"],
"keywords": ["container", "field injection", "reflection", "annotation", "dependency injection"],
"authors": [
{
"name": "yuuki.takezawa",
Expand Down
3 changes: 2 additions & 1 deletion demo/Resolve/NotImplementRepository.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php
namespace Iono\_TestContainer\Resolve;

use Iono\Container\Annotation\Annotations\Scope;
use Iono\Container\Annotation\Annotations\Component;
use Iono\Container\Annotation\Annotations\Autowired;

/**
* Class Repository
* @package Iono\_TestContainer
* @Component("not")
* @Scope("singleton")
*/
class NotImplementRepository
{
Expand Down
8 changes: 5 additions & 3 deletions src/Annotation/Annotations/Autowired.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Iono\Container\Annotation\Annotations;

use Iono\Container\Exception\AnnotationAutowiredException;

/**
* @Annotation
* @Target("PROPERTY")
Expand All @@ -19,14 +21,14 @@ final class Autowired extends Annotation

/**
* @return null|string
* @throws \ErrorException
* @throws AnnotationAutowiredException
*/
public function resolver()
{
$this->value = ltrim($this->value, '\\');
if($this->required) {
if(!($this->value) ? $this->value : null) {
throw new \ErrorException();
if(empty($this->value)) {
throw new AnnotationAutowiredException("context expected", 500);
}
return $this->value;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Annotation/Annotations/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ final class Component extends Annotation

/**
* @param \ReflectionClass $reflectionClass
* @param string $scope
* @return array
* @throws AnnotationComponentException
*/
public function resolver(\ReflectionClass $reflectionClass)
public function resolver(\ReflectionClass $reflectionClass, $scope = "prototype")
{
// implements interfaces
$interfaces = $reflectionClass->getInterfaceNames();
$scope = ($scope === "singleton") ? true : false;
if(!count($interfaces)) {
if(!$this->value) {
throw new AnnotationComponentException("mismatch");
Expand All @@ -33,7 +35,7 @@ public function resolver(\ReflectionClass $reflectionClass)
$this->value => [
'binding' => $reflectionClass->getName(),
'as' => $this->value,
'scope' => null,
'scope' => $scope,
'relation' => $reflectionClass->getName()
]
];
Expand All @@ -42,7 +44,7 @@ public function resolver(\ReflectionClass $reflectionClass)
$interfaces[0] => [
'binding' => $reflectionClass->getName(),
'as' => $interfaces[0],
'scope' => null,
'scope' => $scope,
'relation' => null
]
];
Expand Down
15 changes: 13 additions & 2 deletions src/Annotation/Resolver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Iono\Container\Annotation;

use Iono\Container\Annotation\Annotations\Scope;
use Iono\Container\Annotation\Annotations\Component;

/**
* Class Resolver
* @package Iono\Container\Annotation
Expand All @@ -19,11 +22,19 @@ class Resolver
public function classAnnotation(array $annotations, \ReflectionClass $reflectionClass)
{
$classAnnotation = [];
$component = null;
$scope = "prototype";
foreach($annotations as $annotation) {
if($annotation instanceof \Iono\Container\Annotation\Annotations\Component) {
$classAnnotation[] = $annotation->resolver($reflectionClass);
if($annotation instanceof Scope) {
$scope = $annotation->value;
}
if($annotation instanceof Component) {
$component = $annotation;
}
}
if($component instanceof Component) {
$classAnnotation = $component->resolver($reflectionClass, $scope);
}
return $classAnnotation;
}

Expand Down
14 changes: 8 additions & 6 deletions src/Annotation/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ public function writeRelationFile(array $array)
{
$string = "<?php\n";
foreach($array as $row) {
foreach($row as $dependencies) {
foreach($dependencies as $as => $value) {

foreach($row as $value) {
if(!$value['scope']) {
$string .= "\$this->bind(\"{$value['as']}\", \"{$value['binding']}\");\n";
if($value['relation']) {
$string .= "\$this->relations[\"{$value['as']}\"] = \"{$value['binding']}\";\n";
}
}
if($value['scope']) {
$string .= "\$this->singleton(\"{$value['as']}\", \"{$value['binding']}\");\n";
}
if ($value['relation']) {
$string .= "\$this->relations[\"{$value['as']}\"] = \"{$value['binding']}\";\n";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function putCompiledFile($filename, $stmts)
*/
protected function getClassName($class)
{
return str_replace('\\', '', $class);
return str_replace('\\', '', $class) . md5($class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function register()
* @param array $parameters
* @return mixed
* @throws BindingResolutionException
* @throws \ErrorException
* @see \Illuminate\Container\Container::build
*/
public function build($concrete, $parameters = [])
Expand Down
43 changes: 43 additions & 0 deletions tests/Annotations/AutowiredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Iono\_TestContainer\Annotations;

use Iono\Container\Annotation\Annotations\Autowired;

class AutowiredTest extends \PHPUnit_Framework_TestCase
{
/** @var Autowired */
protected $wired;
public function setUp()
{
$this->wired = new Autowired;
}

public function testInstance()
{
$this->assertInstanceOf("Iono\Container\Annotation\Annotations\Autowired", $this->wired);
}

public function testResolver()
{
$this->wired->required = false;
$this->wired->value = null;
$this->assertNull($this->wired->resolver());

$this->wired->value = 'stdClass';
$this->assertEquals('stdClass', $this->wired->resolver());
}

/**
* @expectedException \Iono\Container\Exception\AnnotationAutowiredException
*/
public function testResolverException()
{
$this->wired->required = true;
$this->wired->value = 'stdClass';
$this->assertEquals('stdClass', $this->wired->resolver());

$this->wired->value = null;
$this->wired->resolver();

}
}
2 changes: 1 addition & 1 deletion tests/Annotations/CacheReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Iono\Container\Annotation\AnnotationManager;

class ApcCacheReaderTest extends \PHPUnit_Framework_TestCase
class CacheReaderTest extends \PHPUnit_Framework_TestCase
{
/** @var */
protected $reader;
Expand Down
8 changes: 3 additions & 5 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,19 @@ public function testBuilder()
{
$result = $this->compiler->builder(['Iono\_TestContainer\InstanceTest' => []]);
$this->assertInternalType('array', $result);
$this->assertEquals('\Iono_TestContainerInstanceTest', $result['class']);
$this->assertInternalType('string', $result['class']);
$this->assertTrue(file_exists($result['file']));
}

public function testCompileClass()
{
$reflection = $this->compiler->getCompilation(['Iono\_TestContainer\InstanceTest' => []]);
$this->assertEquals($reflection->getName(), 'Iono_TestContainerInstanceTest');
$this->assertInstanceOf('ReflectionClass', $reflection);
$this->assertInstanceOf('\Iono_TestContainerInstanceTest', $reflection->newInstance());
$this->assertInstanceOf('Iono\_TestContainer\InstanceTest', $reflection->newInstance());
$this->compiler->setForceCompile(true);
$reflection = $this->compiler->getCompilation(['Iono\_TestContainer\InstanceTest' => []]);
$this->assertEquals($reflection->getName(), 'Iono_TestContainerInstanceTest');
$this->assertInstanceOf('ReflectionClass', $reflection);
$this->assertInstanceOf('\Iono_TestContainerInstanceTest', $reflection->newInstance());
$this->assertInstanceOf('Iono\_TestContainer\InstanceTest', $reflection->newInstance());
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function testSetUpContainer()
$this->scanner();
$this->container->register();
$autowired = $this->container->make('Iono\_TestContainer\Resolve\AutowiredDemo');
$this->assertInstanceOf("Iono_TestContainerResolveAutowiredDemo", $autowired);
$this->assertInstanceOf("Iono\_TestContainer\Resolve\AutowiredDemo", $autowired);
}
}

0 comments on commit 22fd63e

Please sign in to comment.