17

Click here to load reader

Dependency Injection and Pimple

  • Upload
    dqneo

  • View
    4.017

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Dependency Injection and Pimple

Dependency Injection And Pimple

2015.11.23 PHP BLT @DQNEO

Page 2: Dependency Injection and Pimple

What is Dependency Injection?

http://fabien.potencier.org/what-is-dependency-injection.html

Fabien explained it well

Page 3: Dependency Injection and Pimple

Primitive Code

class User { public function __construct() { $this->storage = new Storage(); }

Page 4: Dependency Injection and Pimple

Dependency Injection

class User { public function __construct($storage) { $this->storage = $storage; }

Page 5: Dependency Injection and Pimple

class MyController { public function indexAction() { $user = new User(new Storage()); }

Caller does

Page 6: Dependency Injection and Pimple

class MyController { public function indexAction($id) { $user = new User(new Storage(),$id); }

Caller does

Page 7: Dependency Injection and Pimple

class MyController { public function indexAction($id) { $user = new User(new Storage(new Account()), $id); }

Caller does

Page 8: Dependency Injection and Pimple

class MyController { public function indexAction($id) { $user = new User(new Storage(new Account()), $id); }

Caller does

Too Complicated

Page 9: Dependency Injection and Pimple

class MyController { public function indexAction($id) { $user = new User(new Storage(new Account()), $id); }

Caller does

new 書いとるやんけ

Page 10: Dependency Injection and Pimple

Solution

• Depenjency Injection Container

• etc

Page 11: Dependency Injection and Pimple

Pimple

https://github.com/silexphp/Pimple

Page 12: Dependency Injection and Pimple

How to use Pimple// in bootstrap phase of your codes

use Pimple\Container; $container = new Container();

// define a service $container['storage'] = function ($c) { return new Storage(); };

Page 13: Dependency Injection and Pimple

How to use Pimple// in business logic of your codes

public function indexAction() { $storage = $this->$container[‘storage’]; … }

Page 14: Dependency Injection and Pimple

Internal of Pimpleclass Container implements \ArrayAccess { protected $values = [];

public function __construct(array $values = []) { $this->values = $values; }

public function offsetSet($key, $value) { $this->values[$key] = $value; }

public function offsetGet($key) { $value = $this->values[$key]; return is_callable($value)? $value($this) : $value; }

public function offsetExists($key) { return array_key_exists($key, $this->values); }

public function offsetUnset($key) { unset($this->values[$key]); } }

Page 15: Dependency Injection and Pimple

Internal of Pimple

class Container implements \ArrayAccess

Page 16: Dependency Injection and Pimple

Internal of Pimple

public function offsetGet($key) { $value = $this->values[$key]; return is_callable($value)? $value($this) : $value; }

Page 17: Dependency Injection and Pimple

Pimple pros/cons

• Loosely Coupling

• can make your app more testable

Pros

Cons• reduce the power of IDE (wait for PHP7)

• Indirect programming