Source for file Container.php

Documentation is available at Container.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. /**
  19.  * DIコンテナ
  20.  *
  21.  * @package     teeple
  22.  */
  23. {
  24.  
  25.     const SESSION_KEY '__SESSION_COMPONENTS';
  26.     
  27.     /**
  28.      * Teeple_Container
  29.      * @var unknown_type 
  30.      */
  31.     private static $instance;
  32.     
  33.     /**
  34.      * Containerとして管理するインスタンスを格納
  35.      * @var array 
  36.      */
  37.     protected $_components = array();
  38.  
  39.     /**
  40.      * 設定内容を保持する配列
  41.      * @var array 
  42.      */
  43.     protected $_dicon = array();
  44.     
  45.     /**
  46.      * @var Logger 
  47.      */
  48.     protected $log;
  49.     
  50.     /**
  51.      * ContainerのSingletonインスタンスを取得します。
  52.      *
  53.      * @return Teeple_Container 
  54.      */
  55.     public static function getInstance({
  56.  
  57.         if (self::$instance === NULL{
  58.             self::$instance new Teeple_Container();
  59.         }
  60.         return self::$instance;
  61.     }
  62.  
  63.     /**
  64.      * コンストラクタ
  65.      *
  66.      */
  67.     public function __construct({
  68.         $this->log = LoggerManager::getLogger(get_class($this));
  69.         // 自分を登録
  70.         $this->_components['container'$this;
  71.     }
  72.  
  73.     /**
  74.      * コンテナをセットアップします。
  75.      *
  76.      * @param string $configfile 
  77.      */
  78.     public function setup($configfile{
  79.  
  80.         $config parse_ini_file($configfileTRUE);
  81.         if (count($config1{
  82.             throw new Teeple_Exception("コンテナの設定ファイルを読込めませんでした。");
  83.         }
  84.         
  85.         foreach($config as $key => $value{
  86.             list($clsname$pathexplode(':'$key);
  87.             $this->_dicon[$clsnamearray(
  88.                 'path' => $path,
  89.                 'attributes' => $value
  90.             );
  91.         }
  92.         
  93.     }
  94.     
  95.     /**
  96.      * ContainerにComponentのインスタンスをセット
  97.      *
  98.      * @param   string  $name   Component名
  99.      * @param   Object  $component  Componentのインスタンス
  100.      */
  101.     public function register($name$component{
  102.         if (!is_object($component)) {
  103.             throw new Teeple_Exception("登録しようとしたコンポーネントはオブジェクトではありません。({$name})");
  104.             return;
  105.         }
  106.         $this->_components[$name$component;
  107.         return;
  108.     }
  109.     
  110.     /**
  111.      * Componentのインスタンスを取得します。
  112.      * (RequestスコープのComponent)
  113.      *
  114.      * @param   string  $name   Component名
  115.      * @return  Object  Componentのインスタンス
  116.      */
  117.     public function getComponent($name{
  118.         
  119.         $component NULL;
  120.         if (isset($this->_components[$name])) {
  121.             $component $this->_components[$name];
  122.         else {
  123.             $component $this->_createComponent($name);
  124.         }
  125.         
  126.         return $component;
  127.     }
  128.     
  129.     /**
  130.      * SessionスコープのComponentを取得します。
  131.      *
  132.      * @param string $name 
  133.      * @return Object 
  134.      */
  135.     public function getSessionComponent($name{
  136.  
  137.         $component NULL;
  138.         if (isset($_SESSION[self::SESSION_KEY])) {
  139.             $_SESSION[self::SESSION_KEYarray();
  140.         }
  141.         if (isset($_SESSION[self::SESSION_KEY][$name])) {
  142.             $component $_SESSION[self::SESSION_KEY][$name];
  143.         else {
  144.             $this->log->debug("セッションに存在しないので作成します。");
  145.             $component $this->_createComponent($nameFALSE);
  146.             $_SESSION[self::SESSION_KEY][$name$component;
  147.         }
  148.         
  149.         return $component;
  150.     }
  151.  
  152.     /**
  153.      * prototypeのComponentを取得します。
  154.      *
  155.      * @param string $name 
  156.      * @return Object 
  157.      */
  158.     public function getPrototype($name{
  159.         return $this->_createComponent($nameFALSE);
  160.     }
  161.  
  162.     /**
  163.      * Teeple_ActiveRecordのエンティティを取得します。
  164.      * DefaultTxから取得されます。
  165.      *
  166.      * @param string $name 
  167.      * @return Teeple_ActiveRecord 
  168.      */
  169.     public function getEntity($name{
  170.         
  171.         $defaultTx $this->getComponent('DefaultTx');
  172.         if (is_object($defaultTx)) {
  173.             return $defaultTx->$name;
  174.         }
  175.         return NULL;
  176.     }
  177.     
  178.     /**
  179.      * コンポーネントをインスタンス化します。
  180.      *
  181.      * @param string $name 
  182.      * @return Object 
  183.      */
  184.     private function _createComponent($name$register=TRUE{
  185.         
  186.         $this->log->debug("コンポーネント {$name} を作成します。");
  187.  
  188.         // インスタンスを作成
  189.         if (isset($this->_dicon[$name])) {
  190.             $className $this->_dicon[$name]['class'];
  191.         else {
  192.             $className $name;
  193.         }
  194.         if (Teeple_Util::includeClassFile($className)) {
  195.             throw new Teeple_Exception("クラス{$className}の定義が存在しません。");
  196.         }
  197.         $instance new $className();
  198.         if ($register{
  199.             $this->register($name$instance);
  200.         }
  201.         
  202.         // 自動インジェクション
  203.         $methods get_class_methods($className);
  204.         foreach($methods as $method{
  205.             if (preg_match('/^setComponent_(.+)$/'$method$m)) {
  206.                 $this->log->debug("自動セット: {$m[1]}");
  207.                 $cp $this->getComponent($m[1]);
  208.                 $instance->$method($cp);
  209.             }
  210.             elseif (preg_match('/^setSessionComponent_(.+)$/'$method$m)) {
  211.                 $this->log->debug("自動セット(s): {$m[1]}");
  212.                 $cp $this->getSessionComponent($m[1]);
  213.                 $instance->$method($cp);
  214.             }
  215.             elseif (preg_match('/^setPrototype_(.+)$/'$method$m)) {
  216.                 $this->log->debug("自動セット(p): {$m[1]}");
  217.                 $cp $this->getPrototype($m[1]);
  218.                 $instance->$method($cp);
  219.             }
  220.             elseif (preg_match('/^set(Entity_.+)$/'$method$m)) {
  221.                 $this->log->debug("自動セット(e): {$m[1]}");
  222.                 $cp $this->getEntity($m[1]);
  223.                 $instance->$method($cp);
  224.             }
  225.         }
  226.         
  227.         return $instance;
  228.     }
  229.  
  230. }
  231. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:37 +0900 by phpDocumentor 1.4.3