SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
50.
IOC CONTAINER
IoC ... Inversion of Control
制御の逆転
13年5月29日水曜日
51.
IOC CONTAINER
“The IoC container is at the
heart of the entire application
and all dependencies are
resolved through that.”
Taylor Otwellさん
IoC Containerはアプリケーション全
体における心臓部で、すべての依存
性を解決します。
13年5月29日水曜日
52.
IOC CONTAINER
The Laravel inversion of control container is a powerful tool for managing class
dependencies. Dependency injection is a method of removing hard-coded class
dependencies. Instead, the dependencies are injected at run-time, allowing for
greater flexibility as dependency implementations may be swapped easily.
http://four.laravel.com/docs/ioc
LaravelのIoCコンテナはクラス依存性解決のためのパワフルなツールです。
DI(依存性の注入)はコードによるクラス解決を取り除き、代わりにアプリ
ケーションの実行時に依存性を注入します。このことにより依存の実装を容
易に入れ替えられるという柔軟性を得ることができます。
13年5月29日水曜日
55.
IOC CONTAINER
$foo = App::make('Foo');
13年5月29日水曜日
56.
IOC CONTAINER
App::bind('foo', function() {
return new Foo;
});
$foo = App::make('foo');
13年5月29日水曜日
57.
IOC CONTAINER
$view = new SmartyView;
"
App::bind('view', function() {
$smarty = new SmartyView;
$smarty->..
return $smarty;
});
$view = App::make('view');
13年5月29日水曜日
58.
IOC CONTAINER
$view = new TwigView;
"
App::bind('view', function() {
return new TwigView([
..
]);
});
$view = App::make('view');
13年5月29日水曜日
59.
IOC CONTAINER
クラスを「直接」呼ぶのではなく
IoC Containerを通して「間接的」に呼び出す
13年5月29日水曜日
60.
IOC CONTAINER
クラスが必要になった時
IoC Containerさんがよしなにつくってくれる
13年5月29日水曜日
61.
IOC CONTAINER
僕:IoCさん、僕がfeedくれっていったらAtomFeedのこと
ですからよろしくですよ
IoCさん:(o e o)アイヨ
∼そしてその時がきた∼
僕:「feedくれ!」
IoCさん:(o e o)つ アイヨ ∼【AtomFeed】
13年5月29日水曜日
62.
IOC CONTAINER
登録
App::bind('feed', function() {
return new AtomFeed;
});
使う
$feed = App::make('feed');
echo $feed->generate(); //render xml/atom
13年5月29日水曜日
63.
IOC CONTAINER
登録
App::bind('feed', function() {
return new RssFeed;
});
使う
$feed = App::make('feed');
echo $feed->generate(); //render xml/rss
13年5月29日水曜日
64.
IOC CONTAINER
singletonもIoCが解決
App::singleton('foo', function()
{
return new FooBar;
});
13年5月29日水曜日
65.
IOC CONTAINER
既にあるインスタンスを登録することも
$foo = new Foo;
App::instance('foo', $foo);
13年5月29日水曜日
66.
IOC CONTAINER
Automatic Resolution
The IoC container is powerful enough to resolve
classes without any configuration at all in many
scenarios.
IoCコンテナは強力なので、多くの場合においてクラス解
決のために何かをする必要はありません。
13年5月29日水曜日
67.
IOC CONTAINER
class FooBar {
public function __construct(Baz $baz)
{
$this->baz = $baz;
}
}
$foobar = new FooBar(new Baz);
13年5月29日水曜日
68.
IOC CONTAINER
class FooBar {
public function __construct(Baz $baz)
{
$this->baz = $baz;
}
}
$foobar = App::make(‘FooBar’);
13年5月29日水曜日
69.
IOC CONTAINER
class FooBar {
public function __construct(PiyoInterface $baz)
{
$this->baz = $baz;
}
}
IoCさん (o e o)Interfaceかー 解決できねー...orz
なんていうことはなく
13年5月29日水曜日
70.
IOC CONTAINER
App::bind(‘PiyoInterface’, Poyo);
# PiyoInterfaceが呼ばれたらPoyoのことなんだからね
13年5月29日水曜日
71.
IOC CONTAINER
class FooBar {
public function __construct(PiyoInterface $baz)
{
$this->baz = $baz;
}
}
App::bind(‘PiyoInterface’, Poyo);
App::make(‘FooBar’);
(o e o)/
13年5月29日水曜日
72.
IOC CONTAINER
class Eva {
public function __construct(PilotInterface $pilot)
{
$this->pilot = $pilot;
}
}
App::bind(‘PilotInterface’, ‘Ayanami’)
$eva = App::make(‘Eva’);
# get_class($eva->pilot) == ‘Ayanami’
13年5月29日水曜日
74.
IOC CONTAINER
class OrderController extends BaseController {
public function __construct(OrderRepository $orders)
{
$this->orders = $orders;
}
public function getIndex()
{
$all = $this->orders->all();
return View::make('orders', compact('all'));
}
}
13年5月29日水曜日
75.
IOC CONTAINER
$orders = new OrderRepository();
new OrderController($orders);
App::bind(‘OrderRepository’, ‘MockOrders’)
App::make(‘OrderControler’)
普通に書くとデータベースへの接続が発生
IoCを使うとスタブで置き換えられる
13年5月29日水曜日
76.
IOC CONTAINER
In this example, the OrderRepository class will automatically be injected
into the controller. This means that when unit testing a "mock"
OrderRepository may be bound into the container and injected into the
controller, allowing for painless stubbing of database layer interaction.
この例では、OrderRepositoryクラスは自動的にコントローラーに注入され
ます。このことは、ユニットテストの際にモックのOrderRepositoryクラスを
コントローラに注入することができ、データベース操作部分のスタブ化が容
易になることを意味しています。
13年5月29日水曜日
77.
IOC CONTAINER
まとめ
IoCとは、Inversion Of Control(制御の逆転)
よしなにクラスをつくってくれる
よしなに依存関係を解決してくれる
テストもしやすくなる
13年5月29日水曜日
88.
FACADE
<?php namespace PochikaFacades;
use IlluminateSupportFacadesFacade;
class Page extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'page'; // IoC Containerが解決できる名前
}
}
13年5月29日水曜日