Source for file Ethna_ClassFactory.php

Documentation is available at Ethna_ClassFactory.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_ClassFactory.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: Ethna_ClassFactory.php 527 2008-05-07 02:40:15Z mumumu-org $
  10.  */
  11.  
  12. // {{{ Ethna_ClassFactory
  13. /**
  14.  *  Ethnaフレームワークのオブジェクト生成ゲートウェイ
  15.  *
  16.  *  DIコンテナか、ということも考えましたがEthnaではこの程度の単純なものに
  17.  *  留めておきます。アプリケーションレベルDIしたい場合はフィルタチェインを
  18.  *  使って実現することも出来ます。
  19.  *
  20.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  21.  *  @access     public
  22.  *  @package    Ethna
  23.  */
  24. {
  25.     /**#@+
  26.      *  @access private
  27.      */
  28.  
  29.     /** @var    object  Ethna_Controller    controllerオブジェクト */
  30.     var $controller;
  31.  
  32.     /** @var    object  Ethna_Controller    controllerオブジェクト(省略形) */
  33.     var $ctl;
  34.     
  35.     /** @var    array   クラス定義 */
  36.     var $class array();
  37.  
  38.     /** @var    array   生成済みオブジェクトキャッシュ */
  39.     var $object array();
  40.  
  41.     /** @var    array   生成済みアプリケーションマネージャオブジェクトキャッシュ */
  42.     var $manager array();
  43.  
  44.     /** @var    array   メソッド一覧キャッシュ */
  45.     var $method_list array();
  46.  
  47.     /**#@-*/
  48.  
  49.  
  50.     /**
  51.      *  Ethna_ClassFactoryクラスのコンストラクタ
  52.      *
  53.      *  @access public
  54.      *  @param  object  Ethna_Controller    &$controller    controllerオブジェクト
  55.      *  @param  array                       $class          クラス定義
  56.      */
  57.     function Ethna_ClassFactory(&$controller$class)
  58.     {
  59.         $this->controller =$controller;
  60.         $this->ctl =$controller;
  61.         $this->class $class;
  62.     }
  63.  
  64.     /**
  65.      *  typeに対応するアプリケーションマネージャオブジェクトを返す
  66.      *
  67.      *  @access public
  68.      *  @param  string  $type   クラスキー
  69.      *  @param  bool    $weak   オブジェクトが未生成の場合の強制生成フラグ(default: false)
  70.      *  @return object  Ethna_AppManager    マネージャオブジェクト
  71.      *
  72.      *   TODO: 現状の実装では、typeを名前として扱っているのに、
  73.      *         大文字小文字を区別して違うインスタンスを返しているのを修正する
  74.      */
  75.     function &getManager($type$weak false)
  76.     {
  77.         $obj null;
  78.  
  79.         // check if object class exists
  80.         $obj_class_name $this->controller->getObjectClassName($type);
  81.         if (class_exists($obj_class_name=== false{
  82.             // try include.
  83.             $this->_include($obj_class_name);
  84.         }
  85.  
  86.         // check if manager class exists
  87.         $class_name $this->controller->getManagerClassName($type);
  88.         if (class_exists($class_name=== false
  89.             && $this->_include($class_name=== false{
  90.             return $obj;
  91.         }
  92.  
  93.         if (isset($this->method_list[$class_name]== false{
  94.             $this->method_list[$class_nameget_class_methods($class_name);
  95.             for ($i 0$i count($this->method_list[$class_name])$i++{
  96.                 $this->method_list[$class_name][$istrtolower($this->method_list[$class_name][$i]);
  97.             }
  98.         }
  99.  
  100.         // see if this should be singlton or not
  101.         if ($this->_isCacheAvailable($class_name$this->method_list[$class_name]$weak)) {
  102.             if (isset($this->manager[$type]&& is_object($this->manager[$type])) {
  103.                 return $this->manager[$type];
  104.             }
  105.         }
  106.  
  107.         // see if we have helper methods
  108.         if (in_array("getinstance"$this->method_list[$class_name])) {
  109.             $obj call_user_func(array($class_name'getInstance'));
  110.         else {
  111.             $backend =$this->controller->getBackend();
  112.             $obj =new $class_name($backend);
  113.         }
  114.  
  115.         if (isset($this->manager[$type]== false || is_object($this->manager[$type]== false{
  116.             $this->manager[$type=$obj;
  117.         }
  118.  
  119.         return $obj;
  120.     }
  121.  
  122.     /**
  123.      *  クラスキーに対応するオブジェクトを返す/クラスキーが未定義の場合はAppObjectを探す
  124.      *
  125.      *  @access public
  126.      *  @param  string  $key    クラスキー
  127.      *  @param  bool    $weak   オブジェクトが未生成の場合の強制生成フラグ(default: false)
  128.      *  @return object  生成されたオブジェクト(エラーならnull) 
  129.      *
  130.      *   TODO: 現状の実装では、typeを名前として扱っているのに、
  131.      *         大文字小文字を区別して違うインスタンスを返しているのを修正する
  132.      */
  133.     function &getObject($key$ext false)
  134.     {
  135.         $object null;
  136.  
  137.         $ext to_array($ext);
  138.         if (isset($this->class[$key]== false{
  139.             // app object
  140.             $class_name $this->controller->getObjectClassName($key);
  141.             $ext array_pad($ext3null);
  142.             list($key_type$key_value$prop$ext;
  143.         else {
  144.             // ethna classes
  145.             $class_name $this->class[$key];
  146.             $ext array_pad($ext1null);
  147.             list($weak$ext;
  148.         }
  149.  
  150.         // try to include if not defined
  151.         if (class_exists($class_name== false{
  152.             if ($this->_include($class_name== false{
  153.                 return $object;
  154.             }
  155.         }
  156.  
  157.         // handle app object first
  158.         if (isset($this->class[$key]== false{
  159.             $backend =$this->controller->getBackend();
  160.             $object =new $class_name($backend$key_type$key_value$prop);
  161.             return $object;
  162.         }
  163.  
  164.         if (isset($this->method_list[$class_name]== false{
  165.             $this->method_list[$class_nameget_class_methods($class_name);
  166.             for ($i 0$i count($this->method_list[$class_name])$i++{
  167.                 $this->method_list[$class_name][$istrtolower($this->method_list[$class_name][$i]);
  168.             }
  169.         }
  170.  
  171.         // see if this should be singlton or not
  172.         if ($this->_isCacheAvailable($class_name$this->method_list[$class_name]$weak)) {
  173.             if (isset($this->object[$key]&& is_object($this->object[$key])) {
  174.                 return $this->object[$key];
  175.             }
  176.         }
  177.  
  178.         // see if we have helper methods
  179.         $method sprintf('_getObject_%s'ucfirst($key));
  180.         if (method_exists($this$method)) {
  181.             $object =$this->$method($class_name);
  182.         else if (in_array("getinstance"$this->method_list[$class_name])) {
  183.             $object call_user_func(array($class_name'getInstance'));
  184.         else {
  185.             $object =new $class_name();
  186.         }
  187.  
  188.         if (isset($this->object[$key]== false || is_object($this->object[$key]== false{
  189.             $this->object[$key=$object;
  190.         }
  191.  
  192.         return $object;
  193.     }
  194.  
  195.     /**
  196.      *  クラスキーに対応するクラス名を返す
  197.      *
  198.      *  @access public
  199.      *  @param  string  $key    クラスキー
  200.      *  @return string  クラス名
  201.      */
  202.     function getObjectName($key)
  203.     {
  204.         if (isset($this->class[$key]== false{
  205.             return null;
  206.         }
  207.  
  208.         return $this->class[$key];
  209.     }
  210.  
  211.     /**
  212.      *  オブジェクト生成メソッド(backend)
  213.      *
  214.      *  @access protected
  215.      *  @param  string  $class_name     クラス名
  216.      *  @return object  生成されたオブジェクト(エラーならnull) 
  217.      */
  218.     function &_getObject_Backend($class_name)
  219.     {
  220.         $_ret_object =new $class_name($this->ctl);
  221.         return $_ret_object;
  222.     }
  223.  
  224.     /**
  225.      *  オブジェクト生成メソッド(config)
  226.      *
  227.      *  @access protected
  228.      *  @param  string  $class_name     クラス名
  229.      *  @return object  生成されたオブジェクト(エラーならnull) 
  230.      */
  231.     function &_getObject_Config($class_name)
  232.     {
  233.         $_ret_object =new $class_name($this->ctl);
  234.         return $_ret_object;
  235.     }
  236.  
  237.     /**
  238.      *  オブジェクト生成メソッド(i18n)
  239.      *
  240.      *  @access protected
  241.      *  @param  string  $class_name     クラス名
  242.      *  @return object  生成されたオブジェクト(エラーならnull) 
  243.      */
  244.     function &_getObject_I18n($class_name)
  245.     {
  246.         $_ret_object =new $class_name($this->ctl->getDirectory('locale')$this->ctl->getAppId());
  247.         return $_ret_object;
  248.     }
  249.  
  250.     /**
  251.      *  オブジェクト生成メソッド(logger)
  252.      *
  253.      *  @access protected
  254.      *  @param  string  $class_name     クラス名
  255.      *  @return object  生成されたオブジェクト(エラーならnull) 
  256.      */
  257.     function &_getObject_Logger($class_name)
  258.     {
  259.         $_ret_object =new $class_name($this->ctl);
  260.         return $_ret_object;
  261.     }
  262.  
  263.     /**
  264.      *  オブジェクト生成メソッド(plugin)
  265.      *
  266.      *  @access protected
  267.      *  @param  string  $class_name     クラス名
  268.      *  @return object  生成されたオブジェクト(エラーならnull) 
  269.      */
  270.     function &_getObject_Plugin($class_name)
  271.     {
  272.         $_ret_object =new $class_name($this->ctl);
  273.         return $_ret_object;
  274.     }
  275.  
  276.     /**
  277.      *  オブジェクト生成メソッド(renderer)
  278.      *
  279.      *  @access protected
  280.      *  @param  string  $class_name     クラス名
  281.      *  @return object  生成されたオブジェクト(エラーならnull) 
  282.      */
  283.     function &_getObject_Renderer($class_name)
  284.     {
  285.         $_ret_object =new $class_name($this->ctl);
  286.         return $_ret_object;
  287.     }
  288.  
  289.     /**
  290.      *  オブジェクト生成メソッド(session)
  291.      *
  292.      *  @access protected
  293.      *  @param  string  $class_name     クラス名
  294.      *  @return object  生成されたオブジェクト(エラーならnull) 
  295.      */
  296.     function &_getObject_Session($class_name)
  297.     {
  298.         $_ret_object =new $class_name($this->ctl->getAppId()$this->ctl->getDirectory('tmp')$this->ctl->getLogger());
  299.         return $_ret_object;
  300.     }
  301.  
  302.     /**
  303.      *  オブジェクト生成メソッド(sql)
  304.      *
  305.      *  @access protected
  306.      *  @param  string  $class_name     クラス名
  307.      *  @return object  生成されたオブジェクト(エラーならnull) 
  308.      */
  309.     function &_getObject_Sql($class_name)
  310.     {
  311.         $_ret_object =new $class_name($this->ctl);
  312.         return $_ret_object;
  313.     }
  314.  
  315.     /**
  316.      *  指定されたクラスから想定されるファイルをincludeする
  317.      *
  318.      *  @access protected
  319.      */
  320.     function _include($class_name)
  321.     {
  322.         $file sprintf("%s.%s"$class_name$this->controller->getExt('php'));
  323.         if (file_exists_ex($file)) {
  324.             include_once $file;
  325.             return true;
  326.         }
  327.  
  328.         if (preg_match('/^(\w+?)_(.*)/'$class_name$match)) {
  329.             // try ethna app style
  330.             // App_Foo_Bar_Baz -> Foo/Bar/App_Foo_Bar_Baz.php
  331.             $tmp explode("_"$match[2]);
  332.             $tmp[count($tmp)-1$class_name;
  333.             $file sprintf('%s.%s',
  334.                             implode(DIRECTORY_SEPARATOR$tmp),
  335.                             $this->controller->getExt('php'));
  336.             if (file_exists_ex($file)) {
  337.                 include_once $file;
  338.                 return true;
  339.             }
  340.  
  341.             // try ethna app & pear mixed style
  342.             // App_Foo_Bar_Baz -> Foo/Bar/Baz.php
  343.             $file sprintf('%s.%s',
  344.                             str_replace('_'DIRECTORY_SEPARATOR$match[2]),
  345.                             $this->controller->getExt('php'));
  346.             if (file_exists_ex($file)) {
  347.                 include_once $file;
  348.                 return true;
  349.             }
  350.  
  351.             // try ethna master style
  352.             // Ethna_Foo_Bar -> class/Ethna/Foo/Ethna_Foo_Bar.php
  353.             array_unshift($tmp'Ethna''class');
  354.             $file sprintf('%s.%s',
  355.                             implode(DIRECTORY_SEPARATOR$tmp),
  356.                             $this->controller->getExt('php'));
  357.             if (file_exists_ex($file)) {
  358.                 include_once $file;
  359.                 return true;
  360.             }
  361.  
  362.             // try pear style
  363.             // Foo_Bar_Baz -> Foo/Bar/Baz.php
  364.             $file sprintf('%s.%s',
  365.                             str_replace('_'DIRECTORY_SEPARATOR$class_name),
  366.                             $this->controller->getExt('php'));
  367.             if (file_exists_ex($file)) {
  368.                 include_once $file;
  369.                 return true;
  370.             }
  371.         }
  372.         return false;
  373.     }
  374.  
  375.     /**
  376.      *  指定されたクラスがキャッシュを利用可能かどうかをチェックする
  377.      *
  378.      *  @access protected
  379.      */
  380.     function _isCacheAvailable($class_name$method_list$weak)
  381.     {
  382.         // if we have getInstance(), use this anyway
  383.         if (in_array('getinstance'$method_list)) {
  384.             return false;
  385.         }
  386.  
  387.         // if not, see if weak or not
  388.         return $weak false true;
  389.     }
  390. }
  391. // }}}
  392. ?>

Documentation generated on Thu, 08 May 2008 00:14:37 +0900 by phpDocumentor 1.4.2