Source for file Controller.php

Documentation is available at Controller.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.  * コントローラクラス
  20.  * @package     teeple
  21.  */
  22. {
  23.     /**
  24.      * @var Logger 
  25.      */
  26.     private $log;
  27.     
  28.     /**
  29.      * @var Teeple_ActionChain 
  30.      */
  31.     private $actionChain;
  32.     public function setComponent_Teeple_ActionChain($c{
  33.         $this->actionChain $c;
  34.     }
  35.     
  36.     /**
  37.      * @var Teeple_ConfigUtils 
  38.      */
  39.     private $configUtils;
  40.     public function setComponent_Teeple_ConfigUtils($c{
  41.         $this->configUtils $c;
  42.     }
  43.     
  44.     /**
  45.      * @var Teeple_FilterChain 
  46.      */
  47.     private $filterChain;
  48.     public function setComponent_Teeple_FilterChain($c{
  49.         $this->filterChain $c;
  50.     }
  51.     
  52.     /**
  53.      * @var Teeple_DevHelper 
  54.      */
  55.     private $devhelper;
  56.     public function setComponent_Teeple_DevHelper($c{
  57.         $this->devhelper $c;
  58.     }
  59.     
  60.     /**
  61.      * @var Teeple_TransactionManager 
  62.      */
  63.     private $txManager;
  64.     public function setComponent_Teeple_TransactionManager($c{
  65.         $this->txManager $c;
  66.     }
  67.     
  68.     /**
  69.      * @var Teeple_Container 
  70.      */
  71.     private $container;
  72.     public function setComponent_container($c{
  73.         $this->container $c;
  74.     }
  75.     
  76.     /**
  77.      * コントローラーを実行します。
  78.      *
  79.      */
  80.     public static function start({
  81.         
  82.         $log LoggerManager::getLogger($_SERVER['PATH_INFO']);
  83.         $log->info("*** リクエストを開始します。");
  84.         
  85.         try {
  86.             // コンテナの取得
  87.             $container Teeple_Container::getInstance();
  88.             //$container->setup(WEBAPP_DIR .'/config/dicon.ini');
  89.             
  90.             // セッションを作成 TODO セッションのパラメータ制御
  91.             $session $container->getComponent("Teeple_Session");
  92.             $session->start();
  93.             
  94.             // リダイレクトスコープのリクエスト復元
  95.             $request $session->getParameter("__REDIRECT_SCOPE_REQUEST");
  96.             if (is_object($request)) {
  97.                 $request->setActionMethod("execute");
  98.                 $request->resetCompleteFlag();
  99.                 $request->isRedirect TRUE;
  100.                 $container->register("Teeple_Request"$request);
  101.                 $session->removeParameter("__REDIRECT_SCOPE_REQUEST");
  102.             }
  103.             
  104.             // controllerの実行
  105.             $controller $container->getComponent('Teeple_Controller');
  106.             $controller->execute();
  107.             
  108.         catch (Exception $e{
  109.             $txManager $container->getComponent('Teeple_TransactionManager');
  110.             $txManager->rollbackAll();
  111.             Teeple_ErrorHandler::handle($e);
  112.         }
  113.         return;
  114.     }
  115.     
  116.     /**
  117.      * コンストラクター
  118.      */
  119.     public function __construct({
  120.  
  121.         $this->log LoggerManager::getLogger(get_class($this));
  122.         $this->log->debug("############ コントローラstart.");
  123.         return;
  124.     }
  125.     
  126.     /**
  127.      * フレームワークを起動させる
  128.      *
  129.      * @access  public
  130.      * @since   3.0.0
  131.      */
  132.     public function execute()
  133.     {
  134.         $this->log->debug("************** controller#execute called.");
  135.  
  136.         // デフォルトトランザクションをコンテナに登録
  137.         $defaultTx $this->txManager->getTransaction();
  138.         $this->container->register('DefaultTx'$defaultTx);
  139.  
  140.         // 実行するActionを決定
  141.         $actionName $this->makeActionName();
  142.         if ($actionName == NULL{
  143.             throw new Teeple_Exception("アクションが特定できません。");
  144.         }
  145.         
  146.         // 初期ActionをActionChainにセット
  147.         $this->log->debug("****actionName: $actionName");
  148.         try {
  149.             $this->actionChain->add($actionName);
  150.         catch (Exception $e{
  151.             // Action自動生成ヘルパー TODO Controllerに組み込むのは味が悪い
  152.             if (defined('USE_DEVHELPER'&& USE_DEVHELPER{
  153.                 $this->devhelper->execute($actionName);
  154.             else {
  155.                 // TODO 本当の404だけを見分ける
  156.                 $this->log->warn($e->getMessage());
  157.                 // 404で終了する
  158.                 header("HTTP/1.1 404 Not Found");
  159.                 print('Page Not Found');
  160.                 return;
  161.             }
  162.         }
  163.         
  164.         // FilterChainのセットアップと実行
  165.         $this->filterChain->build();
  166.         $this->filterChain->execute();
  167.         //$this->filterChain->clear();
  168.     }
  169.     
  170.     /**
  171.      * URIからAction名を特定します。
  172.      *
  173.      * @return string 
  174.      */
  175.     private function makeActionName({
  176.         $path $_SERVER['PATH_INFO'];
  177.         if ($path == NULL || strlen($path== || $path == '/'{
  178.             return 'index';
  179.         }
  180.         $path preg_replace('/^\/?(.*)$/''$1'$path);
  181.         $path preg_replace('/(\..*)?$/'''$path);
  182.         $path str_replace('/','_',$path);
  183.         
  184.         return $path;
  185.     }
  186. }
  187. ?>

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