Your SlideShare is downloading. ×
0
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

BEAR DI

3,809

Published on

BEARでのDI実装を、Symfony 2.0のDIスライドの一部抜粋とともに紹介。

BEARでのDI実装を、Symfony 2.0のDIスライドの一部抜粋とともに紹介。

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,809
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
































































  • Transcript

    • 1. BEAR DI Dependency Injection
    • 2. Dependency Injection with PHP5.3 (Symfony 2.0)
    • 3. User
    • 4. class Session_Storage { function __construct($cookieName = 'PHP_SESS_ID') { session_name($cookieName); session_start(); } function set($key, $value) { $_SESSION[$key] = $value; } // ...
    • 5. class User { protected $storage; function __construct() { $this->storage = new Session_Storage(); function setLanguage($language) { $this->storage->set('language', $language); } // ... } $user = new User();
    • 6. class User { protected $storage; function __construct() { $this->storage = new Session_Storage(); function setLanguage($role) { $this->storage->set('role', $role); } // ... } $user = new User();
    • 7. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    • 8. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    • 9. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    • 10. = Dependency Injection Nothing more !
    • 11. class Session_Storage { function __construct($cookieName = 'PHP_SESS_ID') { session_name($cookieName); session_start(); } function set($key, $value) { $_SESSION[$key] = $value; } // ...
    • 12. class User { protected $storage; User function __construct() { $this->storage = new Session_Storage(ʻSESSION_IDʼ); function setRole($role) { $this->storage->set('role', $role); } // ... } $user = new User();
    • 13. class User { protected $storage; function __construct() { $this->storage = new Session_Storage(SESSION_NAME); function setRole($role) { $this->storage->set('role', $role); } // ... } define(ʻSESSION_NAMEʼ , ʻSESSION_IDʼ); $user = new User();
    • 14. class User { protected $storage; function __construct($sessionName) { $this->storage = new Session_Storage($sessionName); function setRole($role) { $this->storage->set('role', $role); } // ... } User $user = new User(ʻSESSION_IDʼ);
    • 15. class User { protected $storage; function __construct(array $sessionOptions) { $this->storage = new Session_Storage($sessionOptions[ʻsess_nameʼ]); function setRole($role) { $this->storage->set('role', $role); } // ... } array User $user = new User(array(ʻsession_nameʻ=>USER_SESSʼ));
    • 16. Session_Storage MySQL, Memcached, etc....
    • 17. Global Registry
    • 18. class User { protected $storage; Glob al Registry function __construct() { $this->storage = Registry::get(ʻSESSION_STORAGEʼ); } } $storage = new SessionStorage(); Registry::set(ʻSESSION_STORAGEʼ, $storage); $user = new User();
    • 19. User Registry
    • 20. User Storage User Storage
    • 21. class User { protected $storage; function __construct($storage) User Storage { $this->storage = $storage; } } $storage = new Session_Storage(ʻSESSION_IDʼ); $user = new User($storage);
    • 22. DI
    • 23. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = My_Sql_Session_Storage(); $user = new User($storage);
    • 24. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = Session_Storage(ʻSESSION_IDʼ); $user = new User($storage);
    • 25. class User { protected $storage; function __construct(Storage_Interface $storage) { $this->storage = $storage; } } Interface Storage_Interface { public function set(); public function get(); }
    • 26. class Session_Test implements Storage_Interface { public function get() { // test get } public function set() { // test set } } /
    • 27. : User /
    • 28. $storage = new SessionStorage(); // constructor injection $user = new User($storage); // setter injection $user = new User(); $user->setStorage($storage); // property injection $user = new User(); $user->storage = $storage;
    • 29. PHP DI (?)
    • 30. BEAR DI
    • 31. new BEAR::dependency
    • 32. new App_User($name, $type, $age, $session...); BEAR::dependency('App_User', $config);
    • 33. array $config new User($name, $type, $age, $session...);
    • 34. BEAR::dependency('App_User', $config); $config = array('name'=> 'BEAR', 'age'=>10, ...); $user = BEAR::dependency('App_User', $config); g (array) $confi + $user = BEAR::get('App_User');
    • 35. BEAR::dependency('App_User', $config); $config = new App_Admin_User($adminUserConfig); $user = BEAR::dependency('App_User', $config); nfig (O bject) $co
    • 36. BEAR::dependency('App_User', $config); $user = new App_Admin_User($config); BEAR::set('user', $user); $config = 'user'; $user = BEAR::dependency('App_User', $config); fig ... (st ring) $con $user = BEAR::get('user'); //
    • 37. BEAR::dependency('App_User', $config); $user = array('App_Admin_User', $config); BEAR::set('user', $user); $config = 'user'; $user = BEAR::dependency('App_User', $config); g (arr ay) $confi
    • 38. BEAR::dependency('App_User', $config); class App_User { private $_instance; function getInstance(array $config) { if (isset(self::$_instance)){ return self::$_instance; } $class = __CLASS__; return new $class($config); } } $user = App_User::getInstance($config);
    • 39. BEAR::dependency('App_User', $config); class App_User extends BEAR_Factory { public function factory() { if (isset($this->_config['is_admin'])) { $obj = BEAR::factory('App_Admin_User', $this->_config); } else { $obj = BEAR::factory('App_User', $this->_config); } return $obj; } }
    • 40. BEAR::dependency('App_User', $config); class App_User extends BEAR_Base { protected $_config; function __construct(array $config) { $this->_config = $config; } function onInject() { // } }
    • 41. (‘injector’ => $injector)
    • 42. $options = array('injector' => 'onInjectMobile'); $user = BEAR::dependency('App_User', $config, $options); class User extends BEAR_Base { protected $_config; function onInject() { } function onInjectMobile() { // } }
    • 43. $options = array('injector' => array ('App_Injector','userInjector')); $user = BEAR::dependency('App_User', $config, $options); class App_Injector { public static function userInjector(&$obj) { $obj->_session = new Session_Storage(); } }
    • 44. = (‘is_persistent’ => true)
    • 45. $options = array('is_persistent' => true); $user = BEAR::dependency('App_User', $config, $options); class User extends BEAR_Base { function onInject() { //30 $this->_map = $this->_convertHugeTable($this->_config['file']); } }
    • 46. ... function __construct($cookieName, $expire = 3600, $idle = 600) { //... }
    • 47. ... function __construct($cookieName, $expire = 3600, $idle = 600) { //... }
    • 48. function __construct($cookieName, $expire = SESSION_EXPIRE_TIME, $idle = SESSION_IDLE_TIME) { //... } define('SESSION_EXPIRE_TIME', 3600); define('SESSION_IDLE_TIME', 600);
    • 49. function __construct($cookieName, $expire = 3600, $idle = 600) { //... } !!
    • 50. App.php $app = BEAR::loadConfig(_BEAR_APP_HOME . '/App/app.yml'); BEAR::init($app); app.yml App_Cache: expire: 3600 idle: 600 BEAR_Emoji: submit: 'entity' emoji_path: '/emoji' App_Db: dsn: default: 'mysql://root:@localhost/bear_demo' slave: 'mysql://root:@localhost/bear_demo' $config = array('expire'=> 7200); $user = BEAR::dependency('App_Cache', $config); fig merge (a rray) $con
    • 51. App.php $app = BEAR::loadConfig(_BEAR_APP_HOME . “/App/app.{$lan}.yml'); BEAR::init($app); app.ja.yml Page_Hello_World: message: ‘ ’ app.en.yml Page_Hello_World: message: ‘Hello World’
    • 52. Conclusion
    • 53. DI - class App_Foo extends BEAR_Base { public function __construct(array $config) 1. { parent::__construct($config); __construct() } public function onInject() 2. { onInject() $this->_bar = BEAR::dependency('App_Bar'); } public function getBar(){ 3. return $this->_bar->get(); } }
    • 54. DI - $foo = BEAR::dependency('App_Foo', $config, $options); echo $foo->getBar(); • new BEAR::dependency • array $config1 • • $config (array) $config (Object) $config (string) $config
    • 55. DI - # config core: # debug: 0 # info: id: beardemo version: 0.0.2 BEAR_Cache: # int 0 | 1 PEAR::Cache_Lite | 2 MEMCACHE | 3 APC adaptor: 1 # path: dsn | file path | memcache host(s) # - localhost # App_Form_Blog_Entry: label: main: ' ' title: ' ' body: ' ' submit: ' ' error: title_required: ' '

    ×