Skip to content

ytake/Iono.Container

Repository files navigation

Iono.Container

About

illuminate/containerを拡張したSpring Frameworkスタイルのコンテナライブラリです
Iono.Container is a PHP Service Container(with Annotation)
like Spring Framework Style Annotations(java)
Build Status Coverage Status Scrutinizer Code Quality Dependency Status Iono.Container
SensioLabsInsight

install

composer.json

    "require": {
        "php": ">=5.4",
        "ext-tokenizer": "*",
        "iono/container": "0.*"
    },

Usage

initailize はじめに

step1

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)

step2 configuration

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.
必要に応じてディレクトリなどを変更してください

step3 set up container

$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

step4 field injection

like spring framework"s annotation javaのspring frameworkスタイルでアノテーションを利用します

added Component

Componentアノテーションで自動スキャンで登録させるクラスを指定します
use @Component Annotation

namespace Acme\Container;

use Iono\Container\Annotation\Annotations\Component;

/**
 * Class Repository
 * @package Acme\Container
 * @Component("repository")
 */
class Repository
{

}
auto scan

自動スキャンさせるクラスは基本的にどこにあっても構いません

$ 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";
field injection
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) {
  }
}

container method

Annotations

現在利用できるものは

@Component

スキャンによるコンテナへの登録
Indicates a auto scan component

use Ytake\Container\Annotations\Annotation\Component;

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

/**
 * @Component
 * Class Repository
 */
class AnnotationRepository implements AnnotationRepositoryInterface {}

@Scope

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 {}

@Autowired

フィールドインジェクション
annotation based field injection(Spring's own @Autowired)

class TestingClass
{
    /**
     * @Autowired("AnnotationRepositoryInterface")
     */
    protected $class;

    public function get()
    {
        return $this->class;
    }
}

@Component Named

named component annotation
クラス自体に名前をつけてコンテナに登録します

/**
 * @Component("hello")
 * Class Repository
 */
class AnnotationRepository {}

@Value

@Component("name") で登録したクラスをコンテナから取得して
フィールドにインジェクトします

class Processer
{
    /**
     * @Value('hello')
     */
    protected $hello;
}

詳しくはdemoディレクトリをご覧ください

About

spring framework style PHP service container library(with annotations)

Resources

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages