メモ:SymfonyではじめるDIを読んだので実際に写経した。


メモ:SymfonyではじめるDIを読んだので実際に写経した。 | Bamboo lath 日々の記録vol.71は楽しい記事が多いなぁ~。
WEB+DB PRESSでSymfonyではじめるDIを読んだので実際に写経する。

■まずphpのバージョン確認(Composerの兼ね合いで。。)

$ php -v
PHP 5.3.3 (cli) (built: Jul  3 2012 16:40:30)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

■Composerのインストール

$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

Your PHP (5.3.3) is quite old, upgrading to PHP 5.3.4 or higher is recommended.
Composer works with 5.3.2+ for most people, but there might be edge case issues.

Downloading...

Composer successfully installed to: /path/symfony_DI/composer.phar
Use it: php composer.phar

バージョンで文句言われたけど無視。

■Composer用の設定ファイル作成
$ vim composer.json

{
        "require":{
                "symfony/dependency-injection": "2.1.*",
                "symfony/config": "2.1.*",
                "symfony/yaml": "2.1.*"
        }
}

 

$ ll
total 632
drwxrwxr-x 2 takeuchi takeuchi   4096 Oct 31 15:21 ./
drwxrwxrwx 8 takeuchi takeuchi   4096 Oct 31 15:18 ../
-rw-rw-r-- 1 takeuchi takeuchi    118 Oct 31 15:21 composer.json
-rwxr-xr-x 1 takeuchi takeuchi 631264 Oct 31 15:18 composer.phar*

■依存パッケージの取得

$ php composer.phar install
Loading composer repositories with package information
Installing dependencies
  - Installing symfony/dependency-injection (v2.1.3)
    Downloading: 100%

  - Installing symfony/config (v2.1.3)
    Downloading: 100%

  - Installing symfony/yaml (v2.1.3)
    Downloading: 100%

Writing lock file
Generating autoload files
$ ll
total 644
drwxrwxr-x 3 takeuchi takeuchi   4096 Oct 31 15:23 ./
drwxrwxrwx 8 takeuchi takeuchi   4096 Oct 31 15:18 ../
-rw-rw-r-- 1 takeuchi takeuchi    118 Oct 31 15:21 composer.json
-rw-rw-r-- 1 takeuchi takeuchi   4939 Oct 31 15:23 composer.lock
-rwxr-xr-x 1 takeuchi takeuchi 631264 Oct 31 15:18 composer.phar*
drwxrwxr-x 4 takeuchi takeuchi   4096 Oct 31 15:23 vendor/

■依存のないクラスの実装とかそれ以降のコード
$ vim initial.php

namespace App{
         class Controller{
                private $memberRegistration;
                private $router;
                private $templating;
                public function __construct(){
                        echo __METHOD__.PHP_EOL;
                }
                public function registerAction(){
                        echo __METHOD__.PHP_EOL;
                        $memberData=$this->getRequestData();
                        $this->memberRegistration->register($memberData);
                        $html=$this->templating->render('register');
                        return $html;
                }
                public function setMemberRegistration(\Domain\MemberRegistration $memberRegistration){
                        echo __METHOD__.PHP_EOL;
                        $this->memberRegistration=$memberRegistration;
                }
                public function setRouter(\App\Router $router){
                        echo __METHOD__.PHP_EOL;
                        $this->router=$router;
                }
                public function setTemplating(\App\Templating $templating){
                        echo __METHOD__.PHP_EOL;
                        $this->templating=$templating;
                }
                private function getRequestData(){
                        return array();
                }

        }

        class Templating{
                private $router;
                public function __construct(){
                        echo __METHOD__.PHP_EOL;
                }
                public function render($templateName){
                        echo __METHOD__.PHP_EOL;
                        $url=$this->router->generateUrl('test');
                }
                //セッターインジェクション
                public function setRouter(\App\Router $router){
                        $this->router=$router;
                }
        }

        class Router{
                public function __construct(){
                        echo __METHOD__.PHP_EOL;
                }
                public function generateUrl(){
                        echo __METHOD__.PHP_EOL;
                }
        }
}

namespace Domain{
        class MemberRegistration{
                public function __construct(){
                        echo __METHOD__.PHP_EOL;
                }
                public function register($memberData){
                        echo __METHOD__.PHP_EOL;
                }
        }
}

namespace{
        require_once 'vendor/autoload.php';
        use Symfony\Component\DependencyInjection\ContainerBuilder;
        use Symfony\Component\DependencyInjection\Reference;

        $container=new ContainerBuilder();

        $container->register('app.controller', '\App\Controller')
                ->addMethodCall('setTemplating', array(new Reference('app.templating'),))
                ->addMethodCall('setRouter', array(new Reference('app.router'),))
                ->addMethodCall('setMemberRegistration', array(new Reference('domain.member_registration'),));

        $container->register('app.templating', '\App\Templating')
                ->addMethodCall('setRouter', array(new Reference('app.router'),));

        $container->register('app.router', '\App\Router');

        $container->register('domain.member_registration', '\Domain\MemberRegistration');

        $controller=$container->get('app.controller');
        $controller->registerAction();
}

■実行結果

$ php initial.php
App\Controller::__construct
App\Templating::__construct
App\Router::__construct
App\Controller::setTemplating
App\Controller::setRouter
Domain\MemberRegistration::__construct
App\Controller::setMemberRegistration
App\Controller::registerAction
Domain\MemberRegistration::register
App\Templating::render
App\Router::generateUrl

このあとはYAMLにしたりダンプで再利用とか。

■参考URL
PHPの外部ライブラリの管理にComposerを使う

コメントを残す

メールアドレスが公開されることはありません。