このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
basic-library:league-container:3.3 [2020/04/19 11:39] y2sunlight |
basic-library:league-container:3.3 [2020/04/19 16:38] y2sunlight |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > TODO: | ||
- | |||
- | ----- | ||
- | |||
====== DIコンテナー - League/ | ====== DIコンテナー - League/ | ||
Version 3.3 ([[https:// | Version 3.3 ([[https:// | ||
行 83: | 行 79: | ||
===== テストプログラム ===== | ===== テストプログラム ===== | ||
- | パッケージのテストフォルダ(test\league-container\)に、以下のテスト用のコードを作成します。この例は[[http:// | + | パッケージのテストフォルダ(test\league-container\)に、以下のテスト用のコードを作成します。この例は League/ |
- | * Foo.php --- 2つのサービス(BarとBza)を持つFooコントローラ\\ (さらにBarサービスはBamをコンストラクタ引数に持っている) | + | * Foo.php --- 2つのサービス(BarとBza)を持つFooコントローラ |
* index1.php --- 手動によるコンストラクター・インジェクションの例 | * index1.php --- 手動によるコンストラクター・インジェクションの例 | ||
* index2.php --- DIコンテナ― によるコンストラクター・インジェクションの例 | * index2.php --- DIコンテナ― によるコンストラクター・インジェクションの例 | ||
* index3.php --- Auto Wiring によるコンストラクター・インジェクションの例 | * index3.php --- Auto Wiring によるコンストラクター・インジェクションの例 | ||
- | * index4.php --- Auto Wiring だがDIコンテナーをキャッシュしている例 | + | * index4.php --- Auto Wiring だがコンテナーをキャッシュしている例 |
<code php Foo.php> | <code php Foo.php> | ||
行 123: | 行 119: | ||
class Bar | class Bar | ||
{ | { | ||
- | /** | + | // .. |
- | * @var \Acme\Bam | + | |
- | */ | + | |
- | public $bam; | + | |
- | + | ||
- | /** | + | |
- | * Construct. | + | |
- | * | + | |
- | * @param \Acme\Bam $bam | + | |
- | */ | + | |
- | public function __construct(Bam $bam) | + | |
- | { | + | |
- | $this-> | + | |
- | } | + | |
} | } | ||
class Baz | class Baz | ||
- | { | ||
- | // .. | ||
- | } | ||
- | |||
- | class Bam | ||
{ | { | ||
// .. | // .. | ||
行 154: | 行 132: | ||
==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ||
- | サービス及びコントローラをDIコンテナーを使わずに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 | + | DIコンテナーを使わずに、サービスをコントローラに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 |
<code php index1.php> | <code php index1.php> | ||
行 163: | 行 141: | ||
spl_autoload_register(function ($class) | spl_autoload_register(function ($class) | ||
{ | { | ||
- | require __DIR__.'/ | + | require __DIR__.'/ |
}); | }); | ||
行 169: | 行 147: | ||
// 手動によるコンストラクター・インジェクション | // 手動によるコンストラクター・インジェクション | ||
// | // | ||
- | $bam = new Acme\Bam; | ||
$baz = new Acme\Baz; | $baz = new Acme\Baz; | ||
- | $bar = new Acme\Bar($bam); | + | $bar = new Acme\Bar; |
$foo = new Acme\Foo($bar, | $foo = new Acme\Foo($bar, | ||
行 177: | 行 154: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
+ | D: | ||
+ | D: | ||
D: | D: | ||
- | D: | ||
- | D: | ||
- | D: | ||
</ | </ | ||
行 191: | 行 166: | ||
==== テスト2 ==== | ==== テスト2 ==== | ||
- | > TODO: | + | |
+ | DIコンテナ― によるコンストラクター・インジェクションの例です。依存関係をクラスコンストラクターに渡すことは、依存性注入の最も簡単な方法です。League/ | ||
<code php index2.php> | <code php index2.php> | ||
行 200: | 行 176: | ||
spl_autoload_register(function ($class) | spl_autoload_register(function ($class) | ||
{ | { | ||
- | require __DIR__.'/ | + | require __DIR__.'/ |
}); | }); | ||
// | // | ||
- | // Auto Wiring | + | // DIコンテナによるコンストラクター・インジェクション |
// | // | ||
$container = new League\Container\Container; | $container = new League\Container\Container; | ||
- | // リフレクションコンテナーをデリゲートとして登録してAuto Wiringを有効にする | + | $container |
- | // (注)既定では、リフレクションコンテナーは、要求するたびに要求を解決します。 | + | |
- | $container->delegate( | + | ->addArgument(Acme\Bar:: |
- | | + | |
- | ); | + | ; |
+ | $container-> | ||
+ | $container-> | ||
$foo = $container-> | $foo = $container-> | ||
行 219: | 行 197: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | ||
D: | D: | ||
D: | D: | ||
行 233: | 行 209: | ||
==== テスト3 ==== | ==== テスト3 ==== | ||
- | > TODO: | + | League/ |
+ | |||
+ | >Auto Wiring はデフォルトで無効になっているので、有効にするにはコンテナデリゲートとして '' | ||
<code php index3.php> | <code php index3.php> | ||
行 242: | 行 220: | ||
spl_autoload_register(function ($class) | spl_autoload_register(function ($class) | ||
{ | { | ||
- | require __DIR__.'/ | + | require __DIR__.'/ |
}); | }); | ||
// | // | ||
- | // Auto Wiring による | + | // Auto Wiring によるコンストラクター・インジェクション |
// | // | ||
$container = new League\Container\Container; | $container = new League\Container\Container; | ||
- | // リフレクションコンテナーのキャッシュを有効にするに、以下のようにします | + | // ReflectionContainerをデリゲートとして登録してAuto Wiringを有効にする |
+ | // (注)デフォルトでは、ReflectionContainerは、要求するたびにそれを解決します。 | ||
+ | $container-> | ||
+ | new League\Container\ReflectionContainer | ||
+ | ); | ||
+ | |||
+ | $foo = $container-> | ||
+ | |||
+ | var_dump($foo instanceof Acme\Foo); | ||
+ | var_dump($foo-> | ||
+ | var_dump($foo-> | ||
+ | </ | ||
+ | |||
+ | === 結果 === | ||
+ | < | ||
+ | D: | ||
+ | D: | ||
+ | D: | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== テスト4 ==== | ||
+ | デフォルトでは ReflectionContainer は、要求の度にそれを解決しようとします。ReflectionContainer でキャッシュ機能を有効にするには以下のようにcacheResolutions()を使用します。 | ||
+ | |||
+ | <code php index4.php> | ||
+ | <?php declare(strict_types=1); | ||
+ | require __DIR__.'/ | ||
+ | |||
+ | // ComposerのAutoloadの代替 | ||
+ | spl_autoload_register(function ($class) | ||
+ | { | ||
+ | require __DIR__.'/ | ||
+ | }); | ||
+ | |||
+ | // | ||
+ | // Auto Wiring によるコンストラクター・インジェクション | ||
+ | // | ||
+ | $container = new League\Container\Container; | ||
+ | |||
+ | // ReflectionContainerのキャッシュを有効にするに、以下のようにします | ||
$container-> | $container-> | ||
(new League\Container\ReflectionContainer)-> | (new League\Container\ReflectionContainer)-> | ||
行 263: | 行 281: | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | + | D: |
- | </ | + | |
\\ | \\ | ||