BEAR DI
Dependency Injection
Dependency Injection with PHP5.3 (Symfony 2.0)
User
class Session_Storage
{
   function __construct($cookieName = 'PHP_SESS_ID')
   {
     session_name($cookieName);
     ses...
class User
{
   protected $storage;

    function __construct()
    {
       $this->storage = new Session_Storage();

    ...
class User
{
   protected $storage;

    function __construct()
    {
      $this->storage = new Session_Storage();

    f...
class User
{
   protected $storage;

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



...
class User
{
   protected $storage;

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



...
class User
{
   protected $storage;

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



...
=
    Dependency Injection

      Nothing more !
class Session_Storage
{
   function __construct($cookieName = 'PHP_SESS_ID')
   {
     session_name($cookieName);
     ses...
class User
{
   protected $storage;

                                              User
    function __construct()
    {
 ...
class User
{
   protected $storage;

    function __construct()
    {
      $this->storage = new Session_Storage(SESSION_N...
class User
{
   protected $storage;

    function __construct($sessionName)
    {
      $this->storage = new Session_Stora...
class User
{
   protected $storage;

    function __construct(array $sessionOptions)
    {
      $this->storage =
       n...
Session_Storage

            MySQL, Memcached, etc....
Global Registry
class User
{
   protected $storage;
                                 Glob al Registry
    function __construct()
    {
   ...
User   Registry
User
Storage
   User Storage
class User
{
   protected $storage;

    function __construct($storage)
                                 User   Storage
  ...
DI
class User
{
   protected $storage;

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

$s...
class User
{
   protected $storage;

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

$s...
class User
{
   protected $storage;

    function __construct(Storage_Interface $storage)
    {
      $this->storage = $st...
class Session_Test implements Storage_Interface
{
    public function get()
    {
     // test get
    }

    public funct...
: User




         /
$storage = new SessionStorage();

// constructor injection
$user = new User($storage);

// setter injection
$user = new Us...
PHP   DI   (?)
BEAR DI
new   BEAR::dependency
new App_User($name, $type, $age, $session...);



  BEAR::dependency('App_User', $config);
array $config

new User($name, $type, $age, $session...);
BEAR::dependency('App_User', $config);




$config = array('name'=> 'BEAR', 'age'=>10, ...);
$user = BEAR::dependency('App...
BEAR::dependency('App_User', $config);




$config = new App_Admin_User($adminUserConfig);
$user = BEAR::dependency('App_U...
BEAR::dependency('App_User', $config);




$user = new App_Admin_User($config);
BEAR::set('user', $user);
$config = 'user'...
BEAR::dependency('App_User', $config);




$user = array('App_Admin_User', $config);
BEAR::set('user', $user);
$config = '...
BEAR::dependency('App_User', $config);




class App_User
{
    private $_instance;

    function getInstance(array $confi...
BEAR::dependency('App_User', $config);




class App_User extends BEAR_Factory
{
    public function factory()
    {
     ...
BEAR::dependency('App_User', $config);




class App_User extends BEAR_Base
{
    protected $_config;

    function __cons...
(‘injector’ => $injector)
$options = array('injector' => 'onInjectMobile');
$user = BEAR::dependency('App_User', $config, $options);

class User ext...
$options = array('injector' => array ('App_Injector','userInjector'));
$user = BEAR::dependency('App_User', $config, $opti...
=

(‘is_persistent’ => true)
$options = array('is_persistent' => true);
$user = BEAR::dependency('App_User', $config, $options);

class User extends BE...
...
function __construct($cookieName, $expire = 3600, $idle = 600)
{
  //...
}
...
function __construct($cookieName, $expire = 3600, $idle = 600)
{
  //...
}
function __construct($cookieName, $expire =
SESSION_EXPIRE_TIME, $idle = SESSION_IDLE_TIME)
{
  //...
}

define('SESSION_E...
function __construct($cookieName, $expire = 3600, $idle = 600)
{
  //...
}



                                            ...
App.php
$app = BEAR::loadConfig(_BEAR_APP_HOME . '/App/app.yml');
BEAR::init($app);



                                   ...
App.php
$app = BEAR::loadConfig(_BEAR_APP_HOME . “/App/app.{$lan}.yml');
BEAR::init($app);



                           a...
Conclusion
DI -
class App_Foo extends BEAR_Base
{
	 public function __construct(array $config)     1.
	 {
	 	 parent::__construct($co...
DI -
     $foo = BEAR::dependency('App_Foo', $config, $options);
     echo $foo->getBar();



•     new                  B...
DI -
#                config
core:
  #
    debug: 0
    #
    info:
      id: beardemo
      version: 0.0.2

BEAR_Cache:
 ...
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
BEAR DI
Upcoming SlideShare
Loading in …5
×

BEAR DI

4,281 views

Published on

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
4,281
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
































































  • BEAR DI

    1. 1. BEAR DI Dependency Injection
    2. 2. Dependency Injection with PHP5.3 (Symfony 2.0)
    3. 3. User
    4. 4. class Session_Storage { function __construct($cookieName = 'PHP_SESS_ID') { session_name($cookieName); session_start(); } function set($key, $value) { $_SESSION[$key] = $value; } // ...
    5. 5. class User { protected $storage; function __construct() { $this->storage = new Session_Storage(); function setLanguage($language) { $this->storage->set('language', $language); } // ... } $user = new User();
    6. 6. class User { protected $storage; function __construct() { $this->storage = new Session_Storage(); function setLanguage($role) { $this->storage->set('role', $role); } // ... } $user = new User();
    7. 7. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    8. 8. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    9. 9. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = new Session_Storage(); $user = new User($storage);
    10. 10. = Dependency Injection Nothing more !
    11. 11. class Session_Storage { function __construct($cookieName = 'PHP_SESS_ID') { session_name($cookieName); session_start(); } function set($key, $value) { $_SESSION[$key] = $value; } // ...
    12. 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. 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. 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. 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. 16. Session_Storage MySQL, Memcached, etc....
    17. 17. Global Registry
    18. 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. 19. User Registry
    20. 20. User Storage User Storage
    21. 21. class User { protected $storage; function __construct($storage) User Storage { $this->storage = $storage; } } $storage = new Session_Storage(ʻSESSION_IDʼ); $user = new User($storage);
    22. 22. DI
    23. 23. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = My_Sql_Session_Storage(); $user = new User($storage);
    24. 24. class User { protected $storage; function __construct($storage) { $this->storage = $storage; } } $storage = Session_Storage(ʻSESSION_IDʼ); $user = new User($storage);
    25. 25. class User { protected $storage; function __construct(Storage_Interface $storage) { $this->storage = $storage; } } Interface Storage_Interface { public function set(); public function get(); }
    26. 26. class Session_Test implements Storage_Interface { public function get() { // test get } public function set() { // test set } } /
    27. 27. : User /
    28. 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. 29. PHP DI (?)
    30. 30. BEAR DI
    31. 31. new BEAR::dependency
    32. 32. new App_User($name, $type, $age, $session...); BEAR::dependency('App_User', $config);
    33. 33. array $config new User($name, $type, $age, $session...);
    34. 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. 35. BEAR::dependency('App_User', $config); $config = new App_Admin_User($adminUserConfig); $user = BEAR::dependency('App_User', $config); nfig (O bject) $co
    36. 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. 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. 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. 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. 40. BEAR::dependency('App_User', $config); class App_User extends BEAR_Base { protected $_config; function __construct(array $config) { $this->_config = $config; } function onInject() { // } }
    41. 41. (‘injector’ => $injector)
    42. 42. $options = array('injector' => 'onInjectMobile'); $user = BEAR::dependency('App_User', $config, $options); class User extends BEAR_Base { protected $_config; function onInject() { } function onInjectMobile() { // } }
    43. 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. 44. = (‘is_persistent’ => true)
    45. 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. 46. ... function __construct($cookieName, $expire = 3600, $idle = 600) { //... }
    47. 47. ... function __construct($cookieName, $expire = 3600, $idle = 600) { //... }
    48. 48. function __construct($cookieName, $expire = SESSION_EXPIRE_TIME, $idle = SESSION_IDLE_TIME) { //... } define('SESSION_EXPIRE_TIME', 3600); define('SESSION_IDLE_TIME', 600);
    49. 49. function __construct($cookieName, $expire = 3600, $idle = 600) { //... } !!
    50. 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. 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. 52. Conclusion
    53. 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. 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. 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: ' '

    ×