このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
basic-library:league-container:3.3 [2020/04/19 11:37] y2sunlight |
basic-library:league-container:3.3 [2020/04/19 16:00] y2sunlight |
||
---|---|---|---|
行 83: | 行 83: | ||
===== テストプログラム ===== | ===== テストプログラム ===== | ||
- | パッケージのテストフォルダ(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 --- Auto Wiring によるコンストラクター・インジェクションの例 | + | * index2.php --- DIコンテナ― によるコンストラクター・インジェクションの例 |
- | * index3.php --- Auto Wiring だがDIコンテナーをキャッシュしている例 | + | * index3.php --- Auto Wiring によるコンストラクター・インジェクションの例 |
+ | * index4.php --- Auto Wiring だがコンテナーをキャッシュしている例 | ||
<code php Foo.php> | <code php Foo.php> | ||
行 122: | 行 123: | ||
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 | ||
{ | { | ||
// .. | // .. | ||
行 153: | 行 136: | ||
==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ==== 【テスト1】手動によるコンストラクター・インジェクション ==== | ||
- | サービス及びコントローラをDIコンテナーを使わずに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 | + | DIコンテナーを使わずに、サービスをコントローラに手動で注入している例です。尚、例題ではComposerによるAutoloadを使用していないので、spl_autoload_register()で代替しています。 |
<code php index1.php> | <code php index1.php> | ||
行 162: | 行 145: | ||
spl_autoload_register(function ($class) | spl_autoload_register(function ($class) | ||
{ | { | ||
- | require __DIR__.'/ | + | require __DIR__.'/ |
}); | }); | ||
行 168: | 行 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, | ||
行 176: | 行 158: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
+ | D: | ||
+ | D: | ||
D: | D: | ||
- | D: | ||
- | D: | ||
- | D: | ||
</ | </ | ||
行 190: | 行 170: | ||
==== テスト2 ==== | ==== テスト2 ==== | ||
- | > TODO: | + | |
+ | > DIコンテナ― によるコンストラクター・インジェクションの例 | ||
<code php index2.php> | <code php index2.php> | ||
+ | </ | ||
+ | |||
+ | === 結果 === | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | ==== テスト3 ==== | ||
+ | League/ | ||
+ | |||
+ | >Auto Wiring はデフォルトで無効になっているので、有効にするにはコンテナデリゲートとして '' | ||
+ | |||
+ | <code php index3.php> | ||
<?php declare(strict_types=1); | <?php declare(strict_types=1); | ||
require __DIR__.'/ | require __DIR__.'/ | ||
行 199: | 行 192: | ||
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; | ||
- | // リフレクションコンテナーをデリゲートとして登録してAuto Wiringを有効にする | + | // ReflectionContainerをデリゲートとして登録してAuto Wiringを有効にする |
- | // (注)既定では、リフレクションコンテナーは、要求するたびに要求を解決します。 | + | // (注)デフォルトでは、ReflectionContainerは、要求するたびにそれを解決します。 |
$container-> | $container-> | ||
new League\Container\ReflectionContainer | new League\Container\ReflectionContainer | ||
行 218: | 行 211: | ||
var_dump($foo-> | var_dump($foo-> | ||
var_dump($foo-> | var_dump($foo-> | ||
- | var_dump($foo-> | ||
</ | </ | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | + | D: |
- | D: | + | D: |
- | D: | + | D: |
- | D: | + | |
</ | </ | ||
\\ | \\ | ||
- | ==== テスト3 ==== | + | ==== テスト4 ==== |
- | > TODO: | + | デフォルトでは ReflectionContainer は、要求の度にそれを解決しようとします。ReflectionContainer でキャッシュ機能を有効にするには以下のようにcacheResolutions()を使用します。 |
- | <code php index3.php> | + | <code php index4.php> |
<?php declare(strict_types=1); | <?php declare(strict_types=1); | ||
require __DIR__.'/ | require __DIR__.'/ | ||
行 241: | 行 232: | ||
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のキャッシュを有効にするに、以下のようにします |
$container-> | $container-> | ||
(new League\Container\ReflectionContainer)-> | (new League\Container\ReflectionContainer)-> | ||
行 262: | 行 253: | ||
=== 結果 === | === 結果 === | ||
< | < | ||
- | D: | + | D: |
- | </ | + | |
\\ | \\ | ||