フレームワークのコントローラー部分の動作を調べるために試しに自分で書いてみた。
Javaと違い、リフレクションが簡単にできるPHPだから、意外と簡単だった。コントローラー部分だけならすぐ作れることがわかった。O/Rマッパーの自動処理がPAKEで実装してあるSymfonyがすごいなということがわかった。formからの遷移をしっかりやってくれるEthnaがすごいなというのがよくわかった。
ob_start(); ////Smarty設定 require_once "/home/p/smarty.ini.php"; ////PEAR require_once "Var_Dump.php"; ob_end_clean(); ////////////////////////////////////////////// //なんちゃってフレームワーク ////////////////////////////////////////////// ////////// //コントローラークラス 基底 class Controller{ protected $template_prefix = ""; protected $template_error = "error"; private $layout = "layout.tpl.php"; public function __construct(){ $this->smarty = SmartyIntance( $_SESSION['user'] ); } public function forward( $action ){ ob_end_clean(); $this->{$action."Action"}(); ob_end_flush(); } public function assign( $key, $val=null ){ if( $val ){ $this->smarty->assign( $key, $val ); }else if( $key ) { $this->smarty->assign( $key ); } } public function setLayout($name){ $this->layout = $name; } public function display($template_partial_name){ $this->smarty->assign("mcontent", $this->template_prefix.$template_partial_name ); ob_end_clean(); $this->smarty->display( $this->layout ); } public function errorAction($array=null){ $this->display($this->template_error); } public function sendDownload( $filename, $filepath ){ if(! file_exists( $filepath ) ){ $this->assign("notice", "ファイルを指定してください"); $this->forward("error"); return; } require_once "HTTP/Download.php"; ob_end_clean(); $down = new HTTP_Download(); $down->setFile($filepath); $down->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, $filename ); $down->setContentType( mime_content_type( $filepath )); $down->send(); ob_end_flush(); return; } } class Dispatcher { public static function dispatch( $contoller, $action ){ try{ ob_start(); $action = ($action) ? $action : "index" ; $contoller->{$action."Action"}(); ob_end_flush(); }catch(Exception $e){ throw new Exception("アクションがありません"); } } } ///////////////////////////////////////// //$_GETを元にActionマッピングするクラス class GetDispatcher extends Dispatcher { public static $actionKey; public static function dispatch( $contoller ){ parent::dispatch( $contoller, $_GET[GetDispatcher::$actionKey] ); } } ///////////////////////////// //チョット便利な関数 ///////////////////////////// function link_to( $url, $text, $attributes ){ if( is_array($attributes) ){ foreach( $attributes as $key => $val){ $attrs[] = " $key='$val' "; } $attrs = implode("", $attrs); }else{ $attrs = $attributes; } return "<a href='$url' $attrs >$text</a>"; } /////////////////////////////////////////// //validator /////////////////////////////////////////// class Validator{ public $message; public function __construct( $message ){ $this->message = $message; } public function isValid($value){ return true; } } class nullValidator extends Validator { public function __construct($message=null){ if($message==null || $message==""){ $message = "必須項目を入力してください"; } parent::__construct( $message); } public function isValid($value){ return !($value==null || $value ==""); } } class minValidator extends Validator { public function __construct($message=null, $length=10 ){ if($message == null || $message == "" ){ $message = $length."文字以上で入力してください"; } $this->minLength = $length; parent::__construct( $message); } public function isValid($value){ return !( strlen( $value ) < $this->minLength ); } } class maxValidator extends Validator { public function __construct($message=null, $length=10 ){ if($message==null || $message==""){ $message = $length."文字以上で入力してください"; } $this->minLength = $length; parent::__construct( $message); } public function isValid($value){ return !( strlen( $value ) > $this->minLength ); } } class rangeValidator extends Validator{ public function __construct($message=null, $min=1,$max=10 ){ if($message==null || $message==""){ $message = $min."から".$max."の値を入力してください"; } $this->min = $min; $this->max = $max; parent::__construct( $message); } public function isValid($value){ return !( $this->min <= $value && $value <= $this->max ); } } ///////////// //REUQESTをマップする //いちいち三項演算子呼ぶのが面倒だ。 class Utena_Request{ protected $params; public function __construct($params=array()){ $this->params = $params; } public static function GET_Reqeuest(){ return Utena_Request::factory($_GET,"Utena_Request_GET"); } public static function POST_Reqeuest(){ return Utena_Request::factory($_POST,"Utena_Request_POST"); } public static function Utena_Request_COOKIE(){ return Utena_Request::factory($_COOKIE,"Utena_Request_POST"); } private static function factory($array, $ClassName){ return new $ClassName($array); } public function getParam($name,$_default=null){ $val = @trim($this->params[$name]); if($val!=null || $val!="" ){ return $this->params[$name]; }else if( $_default != null ){ return $_default; }else{ return null; } } } class Utena_Request_GET extends Utena_Request {} class Utena_Request_POST extends Utena_Request { } class Utena_Request_COOKIE extends Utena_Request {} //main処理 class SampleCtrl extends Controller{ } ////////////////////// //GETに基づいて処理する ////////////////////// //ob_start(); GetDispatcher::$actionKey = "action"; GetDispatcher::dispatch(new SampleCtrl()); //ob_end_flush();