illuminate/containerを拡張したSpring Frameworkスタイルのコンテナライブラリです
Iono.Container is a PHP Service Container(with Annotation)
like Spring Framework Style Annotations(java)
composer.json
"require": {
"php": ">=5.4",
"ext-tokenizer": "*",
"iono/container": "0.*"
},
make directory structure
初期設定関連のファイルを作成します
$ php vendor/bin/init.container.php
default structure
project root
- resource(configure file, annotaion scanned file, cache and compiled directory)
- scanner.php(cli)
auto make config
自動でデフォルトのフォルダ構成を設定ファイルとして書き出します
// annotation file cache directory, and field injection cache file only)
'cache.path' => "/path/to/project/resource",
// @Component, @Scope annotation scan target directory
'scan.target.path' => "/path/to/project",
// doctrine/annotation cache driver("file", "apc", "simple"(no cache))
'annotation.cache.driver' => 'file',
Edit resource/config.php and change the your application location.
必要に応じてディレクトリなどを変更してください
$annotation = new \Iono\Container\Annotation\AnnotationManager();
$config = new Configure();
$config->set(require dirname(dirname(__FILE__)) . '/resource/config.php');
$container = new \Iono\Container\Container(
new \Iono\Container\Compiler($annotation->driver('file'), $config)
);
$container->register();
Lightweight container(illuminate/container)
can not be used annotation
コンパイラ、アノテーションを利用しない場合は軽量コンテナとして利用できます
(illuminate/contaier extend)
illuminate/contaierを継承していますので、利用しない場合はilluminate/contaierが利用されます
$container = new \Iono\Container\Container();
Laravel's container illuminate/container
like spring framework"s annotation javaのspring frameworkスタイルでアノテーションを利用します
Componentアノテーションで自動スキャンで登録させるクラスを指定します
use @Component Annotation
namespace Acme\Container;
use Iono\Container\Annotation\Annotations\Component;
/**
* Class Repository
* @package Acme\Container
* @Component("repository")
*/
class Repository
{
}
自動スキャンさせるクラスは基本的にどこにあっても構いません
$ php scanner.php
put resource/scanned.binding.php file
resource/scanned.binding.phpが生成されます
$this->bind("repository", "Acme\Container\Repository");
$this->map["repository"] = "Acme\Container\Repository";
namespace Acme;
use Iono\Container\Annotation\Annotations\Value;
/**
* Class Perform
* @package Acme
*/
class Perform
{
/**
* @Value("repository")
*/
protected $repository;
}
Acme\Performクラスを利用します
$container->make("Acme\Perform");
フィールドに指定したクラスが利用可能な状態となります
出力されるクラスは実クラスを継承したコンパイル済みクラスが実行されます
object(AcmePerform)#20 (1) {
["repository":protected]=>
object(Acme\Container\Repository)#21 (0) {
}
}
現在利用できるものは
スキャンによるコンテナへの登録
Indicates a auto scan component
use Ytake\Container\Annotations\Annotation\Component;
/**
* Interface RepositoryInterface
*/
interface AnnotationRepositoryInterface {}
/**
* @Component
* Class Repository
*/
class AnnotationRepository implements AnnotationRepositoryInterface {}
singleton or prototype Scope annotation
required @Component annotation (default prototype)
use Ytake\Container\Annotations\Annotation\Component;
/**
* Interface RepositoryInterface
*/
interface AnnotationRepositoryInterface {}
/**
* @Component
* @Scope("singleton")
* Class Repository
*/
class AnnotationRepository implements AnnotationRepositoryInterface {}
or named component annotation
/**
* @Component("hello")
* @Scope("singleton")
* Class Repository
*/
class AnnotationRepository {}
フィールドインジェクション
annotation based field injection(Spring's own @Autowired)
class TestingClass
{
/**
* @Autowired("AnnotationRepositoryInterface")
*/
protected $class;
public function get()
{
return $this->class;
}
}
named component annotation
クラス自体に名前をつけてコンテナに登録します
/**
* @Component("hello")
* Class Repository
*/
class AnnotationRepository {}
@Component("name") で登録したクラスをコンテナから取得して
フィールドにインジェクトします
class Processer
{
/**
* @Value('hello')
*/
protected $hello;
}
詳しくはdemoディレクトリをご覧ください