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     
00015     abstract class MethodMappedController implements Controller
00016     {
00017         private $methodMap      = array();
00018         private $defaultAction  = null;
00019         
00023         public function handleRequest(HttpRequest $request)
00024         {
00025             if ($action = $this->chooseAction($request)) {
00026                 
00027                 $method = $this->methodMap[$action];
00028                 $mav = $this->{$method}($request);
00029                 
00030                 if ($mav->viewIsRedirect())
00031                     return $mav;
00032                     
00033                 $mav->getModel()->set('action', $action);
00034                 
00035                 return $mav;
00036                 
00037             } else
00038                 return ModelAndView::create();
00039                 
00040             Assert::isUnreachable();
00041         }
00042         
00043         public function chooseAction(HttpRequest $request)
00044         {
00045             $action = Primitive::choice('action')->setList($this->methodMap);
00046 
00047             if ($this->defaultAction)
00048                 $action->setDefault($this->defaultAction);
00049 
00050             Form::create()->
00051                 add($action)->
00052                 import($request->getGet())->
00053                 importMore($request->getPost())->
00054                 importMore($request->getAttached());
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 ?>