このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
basic-library:league-container:3.3 [2020/04/19 11:42] y2sunlight [テストプログラム] |
basic-library:league-container:3.3 [2020/04/19 16:37] y2sunlight [テスト2] |
||
---|---|---|---|
行 83: | 行 83: | ||
===== テストプログラム ===== | ===== テストプログラム ===== | ||
- | パッケージのテストフォルダ(test\league-container\)に、以下のテスト用のコードを作成します。この例は[[http:// | + | パッケージのテストフォルダ(test\league-container\)に、以下のテスト用のコードを作成します。この例は League/ |
* Foo.php --- 2つのサービス(BarとBza)を持つFooコントローラ | * Foo.php --- 2つのサービス(BarとBza)を持つFooコントローラ | ||
行 89: | 行 89: | ||
* 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> | ||
行 136: | 行 136: | ||
==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ||
- | サービス及びコントローラをDIコンテナーを使わずに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 | + | DIコンテナーを使わずに、サービスをコントローラに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 |
<code php index1.php> | <code php index1.php> | ||
行 145: | 行 145: | ||
spl_autoload_register(function ($class) | spl_autoload_register(function ($class) | ||
{ | { | ||
- | require __DIR__.'/ | + | require __DIR__.'/ |
}); | }); | ||
行 151: | 行 151: | ||
// 手動によるコンストラクター・インジェクション | // 手動によるコンストラクター・インジェクション | ||
// | // | ||
- | $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, | ||
行 159: | 行 158: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
+ | D: | ||
+ | D: | ||
D: | D: | ||
- | D: | ||
- | D: | ||
- | D: | ||
</ | </ | ||
行 173: | 行 170: | ||
==== テスト2 ==== | ==== テスト2 ==== | ||
- | > TODO: | + | |
+ | DIコンテナ― によるコンストラクター・インジェクションの例です。依存関係をクラスコンストラクターに渡すことは、依存性注入の最も簡単な方法です。League/ | ||
<code php index2.php> | <code php index2.php> | ||
行 182: | 行 180: | ||
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-> | ||
行 201: | 行 201: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | ||
D: | D: | ||
D: | D: | ||
行 215: | 行 213: | ||
==== テスト3 ==== | ==== テスト3 ==== | ||
- | > TODO: | + | League/ |
+ | |||
+ | >Auto Wiring はデフォルトで無効になっているので、有効にするにはコンテナデリゲートとして '' | ||
<code php index3.php> | <code php index3.php> | ||
行 224: | 行 224: | ||
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)-> | ||
行 245: | 行 285: | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | + | D: |
- | </ | + | |
\\ | \\ | ||