Source for file Backend.php

Documentation is available at Backend.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Backend.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: 7185d42e299dc7cdcb0178a9fdece9f58635bf24 $
  10.  */
  11.  
  12. // {{{ Ethna_Backend
  13. /**
  14.  *  バックエンド処理クラス
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**#@+
  22.      *  @access     private
  23.      */
  24.  
  25.     /** @protected    object  Ethna_Controller    controllerオブジェクト */
  26.     protected $controller;
  27.  
  28.     /** @protected    object  Ethna_Controller    controllerオブジェクト($controllerの省略形) */
  29.     protected $ctl;
  30.  
  31.     /** @protected    object  Ethna_ClassFactory  クラスファクトリオブジェクト */
  32.     protected $class_factory;
  33.  
  34.     /** @protected    object  Ethna_Config        設定オブジェクト */
  35.     protected $config;
  36.  
  37.     /** @protected    object  Ethna_I18N          i18nオブジェクト */
  38.     protected $i18n;
  39.  
  40.     /** @protected    object  Ethna_ActionError   アクションエラーオブジェクト */
  41.     protected $action_error;
  42.  
  43.     /** @protected    object  Ethna_ActionError   アクションエラーオブジェクト($action_errorの省略形) */
  44.     protected $ae;
  45.  
  46.     /** @protected    object  Ethna_ActionForm    アクションフォームオブジェクト */
  47.     protected $action_form;
  48.  
  49.     /** @protected    object  Ethna_ActionForm    アクションフォームオブジェクト($action_formの省略形) */
  50.     protected $af;
  51.  
  52.     /** @protected    object  Ethna_ActionClass   アクションクラスオブジェクト */
  53.     protected $action_class;
  54.  
  55.     /** @protected    object  Ethna_ActionClass   アクションクラスオブジェクト($action_classの省略形) */
  56.     protected $ac;
  57.  
  58.     /** @protected    object  Ethna_Session       セッションオブジェクト */
  59.     protected $session;
  60.  
  61.     /** @protected    object  Ethna_Plugin        プラグインオブジェクト */
  62.     protected $plugin;
  63.  
  64.     /** @protected    array   Ethna_DBオブジェクトを格納した配列 */
  65.     protected $db_list;
  66.  
  67.     /** @protected    object  Ethna_Logger        ログオブジェクト */
  68.     protected $logger;
  69.  
  70.     /**#@-*/
  71.  
  72.  
  73.     /**
  74.      *  Ethna_Backendクラスのコンストラクタ
  75.      *
  76.      *  @access public
  77.      *  @param  object  Ethna_Controller    $controller    コントローラオブジェクト
  78.      */
  79.     public function __construct($controller)
  80.     {
  81.         // オブジェクトの設定
  82.         $this->controller = $controller;
  83.         $this->ctl = $this->controller;
  84.  
  85.         $this->class_factory = $controller->getClassFactory();
  86.  
  87.         $this->config = $controller->getConfig();
  88.         $this->i18n = $controller->getI18N();
  89.  
  90.         $this->action_error = $controller->getActionError();
  91.         $this->ae = $this->action_error;
  92.         $this->action_form = $controller->getActionForm();
  93.         $this->af = $this->action_form;
  94.         $this->action_class = null;
  95.         $this->ac = $this->action_class;
  96.  
  97.         $this->session = $this->controller->getSession();
  98.         $this->plugin = $this->controller->getPlugin();
  99.         $this->db_list = array();
  100.         $this->logger = $this->controller->getLogger();
  101.     }
  102.  
  103.     /**
  104.      *  controllerオブジェクトへのアクセサ(R)
  105.      *
  106.      *  @access public
  107.      *  @return object  Ethna_Controller    controllerオブジェクト
  108.      */
  109.     public function getController()
  110.     {
  111.         return $this->controller;
  112.     }
  113.  
  114.     /**
  115.      *  設定オブジェクトへのアクセサ(R)
  116.      *
  117.      *  @access public
  118.      *  @return object  Ethna_Config        設定オブジェクト
  119.      */
  120.     public function getConfig()
  121.     {
  122.         return $this->config;
  123.     }
  124.  
  125.     /**
  126.      *  アプリケーションIDを返す
  127.      *
  128.      *  @access public
  129.      *  @return string  アプリケーションID
  130.      */
  131.     public function getAppId()
  132.     {
  133.         return $this->controller->getAppId();
  134.     }
  135.  
  136.     /**
  137.      *  I18Nオブジェクトのアクセサ(R)
  138.      *
  139.      *  @access public
  140.      *  @return object  Ethna_I18N  i18nオブジェクト
  141.      */
  142.     public function getI18N()
  143.     {
  144.         return $this->i18n;
  145.     }
  146.  
  147.     /**
  148.      *  アクションエラーオブジェクトのアクセサ(R)
  149.      *
  150.      *  @access public
  151.      *  @return object  Ethna_ActionError   アクションエラーオブジェクト
  152.      */
  153.     public function getActionError()
  154.     {
  155.         return $this->action_error;
  156.     }
  157.  
  158.     /**
  159.      *  アクションフォームオブジェクトのアクセサ(R)
  160.      *
  161.      *  @access public
  162.      *  @return object  Ethna_ActionForm    アクションフォームオブジェクト
  163.      */
  164.     public function getActionForm()
  165.     {
  166.         return $this->action_form;
  167.     }
  168.  
  169.     /**
  170.      *  アクションフォームオブジェクトのアクセサ(W)
  171.      *
  172.      *  @access public
  173.      */
  174.     public function setActionForm($action_form)
  175.     {
  176.         $this->action_form = $action_form;
  177.         $this->af = $action_form;
  178.     }
  179.  
  180.     /**
  181.      *  実行中のアクションクラスオブジェクトのアクセサ(R)
  182.      *
  183.      *  @access public
  184.      *  @return mixed   Ethna_ActionClass:アクションクラス null:アクションクラス未定
  185.      */
  186.     public function getActionClass()
  187.     {
  188.         return $this->action_class;
  189.     }
  190.  
  191.     /**
  192.      *  実行中のアクションクラスオブジェクトのアクセサ(W)
  193.      *
  194.      *  @access public
  195.      */
  196.     public function setActionClass($action_class)
  197.     {
  198.         $this->action_class = $action_class;
  199.         $this->ac = $action_class;
  200.     }
  201.  
  202.     /**
  203.      *  ログオブジェクトのアクセサ(R)
  204.      *
  205.      *  @access public
  206.      *  @return object  Ethna_Logger    ログオブジェクト
  207.      */
  208.     public function getLogger()
  209.     {
  210.         return $this->logger;
  211.     }
  212.  
  213.     /**
  214.      *  セッションオブジェクトのアクセサ(R)
  215.      *
  216.      *  @access public
  217.      *  @return object  Ethna_Session   セッションオブジェクト
  218.      */
  219.     public function getSession()
  220.     {
  221.         return $this->session;
  222.     }
  223.  
  224.     /**
  225.      *  プラグインオブジェクトのアクセサ(R)
  226.      *
  227.      *  @access public
  228.      *  @return object  Ethna_Plugin    プラグインオブジェクト
  229.      */
  230.     public function getPlugin()
  231.     {
  232.         return $this->plugin;
  233.     }
  234.  
  235.     /**
  236.      *  マネージャオブジェクトへのアクセサ(R)
  237.      *
  238.      *  @access public
  239.      *  @return object  Ethna_AppManager    マネージャオブジェクト
  240.      */
  241.     public function getManager($type$weak false)
  242.     {
  243.         $_ret_object $this->class_factory->getManager($type$weak);
  244.         return $_ret_object;
  245.     }
  246.  
  247.     /**
  248.      *  オブジェクトへのアクセサ(R)
  249.      *
  250.      *  @access public
  251.      *  @return mixed   $keyに対応するオブジェクト(or null)
  252.      */
  253.     public function getObject($key)
  254.     {
  255.         $arg_list func_get_args();
  256.         array_shift($arg_list);
  257.         $_ret_object $this->class_factory->getObject($key$arg_list);
  258.         return $_ret_object;
  259.     }
  260.  
  261.     /**
  262.      *  アプリケーションのベースディレクトリを取得する
  263.      *
  264.      *  @access public
  265.      *  @return string  ベースディレクトリのパス名
  266.      */
  267.     public function getBasedir()
  268.     {
  269.         return $this->controller->getBasedir();
  270.     }
  271.  
  272.     /**
  273.      *  アプリケーションのテンプレートディレクトリを取得する
  274.      *
  275.      *  @access public
  276.      *  @return string  テンプレートディレクトリのパス名
  277.      */
  278.     public function getTemplatedir()
  279.     {
  280.         return $this->controller->getTemplatedir();
  281.     }
  282.  
  283.     /**
  284.      *  アプリケーションの設定ディレクトリを取得する
  285.      *
  286.      *  @access public
  287.      *  @return string  設定ディレクトリのパス名
  288.      */
  289.     public function getEtcdir()
  290.     {
  291.         return $this->controller->getDirectory('etc');
  292.     }
  293.  
  294.     /**
  295.      *  アプリケーションのテンポラリディレクトリを取得する
  296.      *
  297.      *  @access public
  298.      *  @return string  テンポラリディレクトリのパス名
  299.      */
  300.     public function getTmpdir()
  301.     {
  302.         return $this->controller->getDirectory('tmp');
  303.     }
  304.  
  305.     /**
  306.      *  アプリケーションのテンプレートファイル拡張子を取得する
  307.      *
  308.      *  @access public
  309.      *  @return string  テンプレートファイルの拡張子
  310.      */
  311.     public function getTemplateext()
  312.     {
  313.         return $this->controller->getExt('tpl');
  314.     }
  315.  
  316.     /**
  317.      *  ログを出力する
  318.      *
  319.      *  @access public
  320.      *  @param  int     $level      ログレベル(LOG_DEBUG, LOG_NOTICE...)
  321.      *  @param  string  $message    ログメッセージ(printf形式)
  322.      */
  323.     public function log($level$message)
  324.     {
  325.         $args func_get_args();
  326.         if (count($args2{
  327.             array_splice($args02);
  328.             $message vsprintf($message$args);
  329.         }
  330.         $this->logger->log($level$message);
  331.     }
  332.  
  333.     /**
  334.      *  バックエンド処理を実行する
  335.      *
  336.      *  @access public
  337.      *  @param  string  $action_name    実行するアクションの名称
  338.      *  @return mixed   (string):Forward名(nullならforwardしない) Ethna_Error:エラー
  339.      */
  340.     public function perform($action_name)
  341.     {
  342.         $forward_name null;
  343.  
  344.         $action_class_name $this->controller->getActionClassName($action_name);
  345.         $this->action_class = new $action_class_name($this);
  346.         $this->ac = $this->action_class;
  347.  
  348.         // アクションの実行
  349.         $forward_name $this->ac->authenticate();
  350.         if ($forward_name === false{
  351.             return null;
  352.         else if ($forward_name !== null{
  353.             return $forward_name;
  354.         }
  355.  
  356.         $forward_name $this->ac->prepare();
  357.         if ($forward_name === false{
  358.             return null;
  359.         else if ($forward_name !== null{
  360.             return $forward_name;
  361.         }
  362.  
  363.         $forward_name $this->ac->perform();
  364.  
  365.         return $forward_name;
  366.     }
  367.  
  368.     /**
  369.      *  DBオブジェクトを返す
  370.      *
  371.      *  @access public
  372.      *  @param  string  $db_key DBキー
  373.      *  @return mixed   Ethna_DB:DBオブジェクト null:DSN設定なし Ethna_Error:エラー
  374.      *  @todo   この中でnewしないでclass factoryを利用する
  375.      */
  376.     public function getDB($db_key "")
  377.     {
  378.         $null null;
  379.         $db_varname $this->_getDBVarname($db_key);
  380.  
  381.         if (Ethna::isError($db_varname)) {
  382.             return $db_varname;
  383.         }
  384.  
  385.         if (isset($this->db_list[$db_varname]&& $this->db_list[$db_varname!= null{
  386.             return $this->db_list[$db_varname];
  387.         }
  388.  
  389.         $dsn $this->controller->getDSN($db_key);
  390.  
  391.         if ($dsn == ""{
  392.             // DB接続不要
  393.             return $null;
  394.         }
  395.  
  396.         $dsn_persistent $this->controller->getDSN_persistent($db_key);
  397.  
  398.         $class_factory $this->controller->getClassFactory();
  399.         $db_class_name $class_factory->getObjectName('db');
  400.  
  401.         // BC: Ethna_DB -> Ethna_DB_PEAR
  402.         if ($db_class_name == 'Ethna_DB'{
  403.             $db_class_name 'Ethna_DB_PEAR';
  404.         }
  405.         if (class_exists($db_class_name=== false{
  406.             $class_factory->_include($db_class_name);
  407.         }
  408.  
  409.         $this->db_list[$db_varnamenew $db_class_name($this->controller$dsn$dsn_persistent);
  410.         $r $this->db_list[$db_varname]->connect();
  411.         if (Ethna::isError($r)) {
  412.             $this->db_list[$db_varnamenull;
  413.             return $r;
  414.         }
  415.  
  416.         register_shutdown_function(array($this'shutdownDB'));
  417.  
  418.         return $this->db_list[$db_varname];
  419.     }
  420.  
  421.     /**
  422.      *  DBオブジェクト(全て)を取得する
  423.      *
  424.      *  @access public
  425.      *  @return mixed   array:Ethna_DBオブジェクトの一覧 Ethan_Error:(いずれか一つ以上の接続で)エラー
  426.      */
  427.     public function getDBList()
  428.     {
  429.         $r array();
  430.         $db_define_list $this->controller->getDBType();
  431.         foreach ($db_define_list as $db_key => $db_type{
  432.             $db $this->getDB($db_key);
  433.             if (Ethna::isError($db)) {
  434.                 return $r;
  435.             }
  436.             $elt array();
  437.             $elt['db'$db;
  438.             $elt['key'$db_key;
  439.             $elt['type'$db_type;
  440.             $elt['varname'"db";
  441.             if ($db_key != ""{
  442.                 $elt['varname'sprintf("db_%s"strtolower($db_key));
  443.             }
  444.             $r[$elt;
  445.         }
  446.         return $r;
  447.     }
  448.  
  449.     /**
  450.      *  DBコネクションを切断する
  451.      *
  452.      *  @access public
  453.      */
  454.     public function shutdownDB()
  455.     {
  456.         foreach (array_keys($this->db_listas $key{
  457.             if ($this->db_list[$key!= null && $this->db_list[$key]->isValid()) {
  458.                 $this->db_list[$key]->disconnect();
  459.                 unset($this->db_list[$key]);
  460.             }
  461.         }
  462.     }
  463.  
  464.     /**
  465.      *  指定されたDBキーに対応する(当該DBオブジェクトを格納するための)メンバ変数名を取得する
  466.      *
  467.      *  正直もう要らないのですが、後方互換性維持のために一応残してある状態です
  468.      *  (Ethna_AppManagerクラスなどで、$this->dbとかしている箇所が少なからずあ
  469.      *  るので)
  470.      *
  471.      *  @access private
  472.      *  @param  string  $db_key DBキー
  473.      *  @return mixed   string:メンバ変数名 Ethna_Error:不正なDB種別
  474.      */
  475.     protected function _getDBVarname($db_key "")
  476.     {
  477.         $r $this->controller->getDBType($db_key);
  478.         if (is_null($r)) {
  479.             return Ethna::raiseError("Undefined DB Type [%s]"E_DB_INVALIDTYPE$db_key);
  480.         }
  481.  
  482.         if ($db_key == ""{
  483.             $db_varname "";
  484.         else {
  485.             $db_varname sprintf("%s"strtolower($db_key));
  486.         }
  487.  
  488.         return $db_varname;
  489.     }
  490. }
  491. // }}}

Documentation generated on Fri, 11 Nov 2011 03:57:42 +0900 by phpDocumentor 1.4.3