MethodMappedController.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2007 by Anton E. Lebedevich                              *
00004  *                                                                          *
00005  *   This program is free software; you can redistribute it and/or modify   *
00006  *   it under the terms of the GNU Lesser General Public License as         *
00007  *   published by the Free Software Foundation; either version 3 of the     *
00008  *   License, or (at your option) any later version.                        *
00009  *                                                                          *
00010  ****************************************************************************/
00011 /* $Id: MethodMappedController.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012     
00016     abstract class MethodMappedController implements Controller
00017     {
00018         private $methodMap      = array();
00019         private $defaultAction  = null;
00020         
00024         public function handleRequest(HttpRequest $request)
00025         {
00026             if ($action = $this->chooseAction($request)) {
00027                 
00028                 $method = $this->methodMap[$action];
00029                 $mav = $this->{$method}($request);
00030                 
00031                 if ($mav->viewIsRedirect())
00032                     return $mav;
00033                     
00034                 $mav->getModel()->set('action', $action);
00035                 
00036                 return $mav;
00037                 
00038             } else
00039                 return ModelAndView::create();
00040                 
00041             Assert::isUnreachable();
00042         }
00043         
00044         public function chooseAction(HttpRequest $request)
00045         {
00046             $action = Primitive::choice('action')->setList($this->methodMap);
00047 
00048             if ($this->defaultAction)
00049                 $action->setDefault($this->defaultAction);
00050 
00051             Form::create()->
00052                 add($action)->
00053                 import($request->getGet())->
00054                 importMore($request->getPost());
00055             
00056             if (!$command = $action->getValue())
00057                 return $action->getDefault();
00058             
00059             return $command;
00060         }
00061         
00065         public function setMethodMapping($action, $methodName)
00066         {
00067             $this->methodMap[$action] = $methodName;
00068             return $this;
00069         }
00070         
00074         public function dropMethodMapping($action)
00075         {
00076             unset($this->methodMap[$action]);
00077             
00078             return $this;
00079         }
00080         
00081         public function getMethodMapping()
00082         {
00083             return $this->methodMap;
00084         }
00085         
00089         public function setDefaultAction($action)
00090         {
00091             $this->defaultAction = $action;
00092             
00093             return $this;
00094         }
00095         
00099         public function setMethodMappingList($array)
00100         {
00101             foreach ($array as $action => $methodName)
00102                 $this->setMethodMapping($action, $methodName);
00103             
00104             return $this;
00105         }
00106         
00107         public function getDefaultAction()
00108         {
00109             return $this->defaultAction;
00110         }
00111     }
00112 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4