SlideShare una empresa de Scribd logo
1 de 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: '                     '

Más contenido relacionado

La actualidad más candente

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.4Fabien Potencier
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Fabien Potencier
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010Fabien Potencier
 
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 FrameworksNate Abele
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of LithiumNate Abele
 
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.3Fabien Potencier
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
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 EditionNate Abele
 
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 clienteLeonardo Proietti
 
Drupal csu-open atriumname
Drupal csu-open atriumnameDrupal csu-open atriumname
Drupal csu-open atriumnameEmanuele Quinto
 
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 UnConRafael Dohms
 
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 patternsSamuel ROZE
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form componentSamuel ROZE
 

La actualidad más candente (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
 

Similar a BEAR DI

Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Fabien Potencier
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Fabien Potencier
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsSam Hennessy
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにYuya Takeyama
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
Tidy Up Your Code
Tidy Up Your CodeTidy Up Your Code
Tidy Up Your CodeAbbas Ali
 
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.5Leonardo Proietti
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Fabien Potencier
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 

Similar a 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
 

Más de Akihito Koriyama

PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」PHPカンファレンス関西2014 「全てを結ぶ力」
PHPカンファレンス関西2014 「全てを結ぶ力」Akihito Koriyama
 
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 triangleAkihito 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_oAkihito Koriyama
 
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.Akihito Koriyama
 

Más 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)
 

Último

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 

Último (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 

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: ' '

Notas del editor