SlideShare une entreprise Scribd logo
1  sur  64
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);
     session_start();
   }

    function set($key, $value) {
       $_SESSION[$key] = $value;
    }
// ...
class User
{
   protected $storage;

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

    function setLanguage($language)
    {
        $this->storage->set('language', $language);
    }
    // ...
}

$user = new User();
class User
{
   protected $storage;

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

    function setLanguage($role)
    {
        $this->storage->set('role', $role);
    }
    // ...
}

$user = new User();
class User
{
   protected $storage;

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




$storage = new Session_Storage();
$user = new User($storage);
class User
{
   protected $storage;

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




$storage = new Session_Storage();
$user = new User($storage);
class User
{
   protected $storage;

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




$storage = new Session_Storage();
$user = new User($storage);
=
    Dependency Injection

      Nothing more !
class Session_Storage
{
   function __construct($cookieName = 'PHP_SESS_ID')
   {
     session_name($cookieName);
     session_start();
   }

    function set($key, $value) {
       $_SESSION[$key] = $value;
    }
// ...
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();
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();
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ʼ);
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ʼ));
Session_Storage

            MySQL, Memcached, etc....
Global Registry
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();
User   Registry
User
Storage
   User Storage
class User
{
   protected $storage;

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

$storage = new Session_Storage(ʻSESSION_IDʼ);
$user = new User($storage);
DI
class User
{
   protected $storage;

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

$storage = My_Sql_Session_Storage();
$user = new User($storage);
class User
{
   protected $storage;

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

$storage = Session_Storage(ʻSESSION_IDʼ);
$user = new User($storage);
class User
{
   protected $storage;

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

Interface Storage_Interface
{
    public function set();
    public function get();
}
class Session_Test implements Storage_Interface
{
    public function get()
    {
     // test get
    }

    public function set()
     {
      // test set
     }
}

                                                  /
: User




         /
$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;
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_User', $config);
                                                             g
                                                (array) $confi


 +



$user = BEAR::get('App_User');
BEAR::dependency('App_User', $config);




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

                                                            nfig
                                              (O bject) $co
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'); //
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
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);
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;
    }
}
BEAR::dependency('App_User', $config);




class App_User extends BEAR_Base
{
    protected $_config;

    function __construct(array $config)
    {
        $this->_config = $config;
    }

    function onInject()
    {
        //
    }
}
(‘injector’ => $injector)
$options = array('injector' => 'onInjectMobile');
$user = BEAR::dependency('App_User', $config, $options);

class User extends BEAR_Base
{
    protected $_config;

    function onInject()
    {
    }

    function onInjectMobile()
    {
    //
    }
}
$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();
    }
}
=

(‘is_persistent’ => true)
$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']);
    }
}
...
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_EXPIRE_TIME', 3600);
define('SESSION_IDLE_TIME', 600);
function __construct($cookieName, $expire = 3600, $idle = 600)
{
  //...
}



                                                        !!
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
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’
Conclusion
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();
	 }
}
DI -
     $foo = BEAR::dependency('App_Foo', $config, $options);
     echo $foo->getBar();



•     new                  BEAR::dependency
•                     array $config1

•
•     $config

    (array) $config    (Object) $config   (string) $config
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: '                     '

Contenu connexe

Tendances

Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
Fabien Potencier
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
Fabien Potencier
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
Fabien Potencier
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 

Tendances (20)

Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
logic321
logic321logic321
logic321
 
Symfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il cliente
 
Drupal csu-open atriumname
Drupal csu-open atriumnameDrupal csu-open atriumname
Drupal csu-open atriumname
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
How I started to love design patterns
How I started to love design patternsHow I started to love design patterns
How I started to love design patterns
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form component
 
Laravel
LaravelLaravel
Laravel
 

Similaire à BEAR DI

Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010
Fabien Potencier
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
Fabien Potencier
 

Similaire à BEAR DI (20)

Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Oops in php
Oops in phpOops in php
Oops in php
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your Code
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 

Plus de Akihito Koriyama

PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」
Akihito Koriyama
 
An object graph visualizer for PHP - print_o
An object graph visualizer for PHP - print_oAn object graph visualizer for PHP - print_o
An object graph visualizer for PHP - print_o
Akihito Koriyama
 

Plus de Akihito Koriyama (15)

PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」
 
A resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleA resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangle
 
PHP Coding in BEAR.Sunday
PHP Coding in BEAR.SundayPHP Coding in BEAR.Sunday
PHP Coding in BEAR.Sunday
 
BEAR.Sunday 1.X
BEAR.Sunday 1.XBEAR.Sunday 1.X
BEAR.Sunday 1.X
 
BEAR.Sunday $app
BEAR.Sunday $appBEAR.Sunday $app
BEAR.Sunday $app
 
BEAR.Sunday@phpcon2012
BEAR.Sunday@phpcon2012BEAR.Sunday@phpcon2012
BEAR.Sunday@phpcon2012
 
An object graph visualizer for PHP - print_o
An object graph visualizer for PHP - print_oAn object graph visualizer for PHP - print_o
An object graph visualizer for PHP - print_o
 
BEAR.Sunday.meetup #0
BEAR.Sunday.meetup #0BEAR.Sunday.meetup #0
BEAR.Sunday.meetup #0
 
BEAR.Sunday Offline Talk
BEAR.Sunday Offline TalkBEAR.Sunday Offline Talk
BEAR.Sunday Offline Talk
 
BEAR.Sunday Note
BEAR.Sunday NoteBEAR.Sunday Note
BEAR.Sunday Note
 
PHP: Dis Is It
PHP: Dis Is ItPHP: Dis Is It
PHP: Dis Is It
 
The new era of PHP web development.
The new era of PHP web development.The new era of PHP web development.
The new era of PHP web development.
 
BEAR (Suday) design
BEAR (Suday) designBEAR (Suday) design
BEAR (Suday) design
 
BEAR Architecture
BEAR ArchitectureBEAR Architecture
BEAR Architecture
 
BEAR v0.9 (Saturday)
BEAR v0.9 (Saturday)BEAR v0.9 (Saturday)
BEAR v0.9 (Saturday)
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

BEAR DI

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

Notes de l'éditeur