# ControllersCollection.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2008 by Dmitry V. Sokolov                                *
    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
    00012     abstract class ControllersCollection implements Controller
    00013     {
    00014         private $innerControllers   = array();
    00015
    00016         private $defaultRequestType = null;
    00017         private $mav                = null;
    00018
    00019         public function __construct()
    00020         {
    00021             $this->mav =
    00022                 ModelAndView::create()->
    00023                 setModel(Model::create());
    00024
    00025             $this->defaultRequestType = RequestType::post();
    00026         }
    00027
    00031         public function handleRequest(HttpRequest $request)
    00032         {
    00033             Assert::isNotEmptyArray(
    00034                 $this->innerControllers,
    00035                 'Add atleast one innerController first'
    00036             );
    00037
    00038             $activeController = $this->getActiveController($request);
    00039
    00040             $model = $this->mav->getModel();
    00041
    00042             if ($activeController) {
    00043                 $controllerName = $activeController->getName();
    00044                 $activeMav      = $activeController->handleRequest($request);
    00045
    00046                 $model->set(
    00047                     TextUtils::downFirst($controllerName),
    00048                     $activeMav->getModel()
    00049                 );
    00050
    00051                 unset($this->innerControllers[$controllerName]);
    00052             }
    00053
    00054             foreach ($this->innerControllers as $controller) {
    00055                 $passedRequest = clone $request;
    00056
    00057                 $passedRequest->
    00058                     {'set'.$controller->getRequestGetter().'Var'}
    00059                     ('action', null);
    00060
    00061                 $subMav = $controller->handleRequest($passedRequest);
    00062
    00063                 $model->set(
    00064                     TextUtils::downFirst($controller->getName()),
    00065                     $subMav->getModel()
    00066                 );
    00067             }
    00068
    00069             return
    00070                 isset($activeMav) && $activeMav->viewIsRedirect()
    00071                     ? $activeMav
    00072                     : $this->mav;
    00073         }
    00074
    00078         public function setMav(ModelAndView $mav)
    00079         {
    00080             $this->mav = $mav;
    00081
    00082             return $this;
    00083         }
    00084
    00088         public function getMav()
    00089         {
    00090             return $this->mav;
    00091         }
    00092
    00096         public function add(
    00097             Controller $controller,
    00098             RequestType $requestType = null
    00099         )
    00100         {
    00101             if (!$requestType)
    00102                 $requestType = $this->defaultRequestType;
    00103
    00104             $this->innerControllers[get_class($controller)] =
    00105                 ProxyController::create()->
    00106                 setInner($controller)->
    00107                 setRequestType($requestType);
    00108
    00109             return $this;
    00110         }
    00111
    00115         public function setDefaultRequestType(RequestType $requestType)
    00116         {
    00117             $this->defaultRequestType = $requestType;
    00118
    00119             return $this;
    00120         }
    00121
    00125         private function getActiveController(HttpRequest $request)
    00126         {
    00127             foreach ($this->innerControllers as $controller)
    00128                 if ($controller->isActive($request)) {
    00129                     unset($this->innerControllers[
    00130                         get_class($controller->getInner())]);
    00131
    00132                     return $controller;
    00133                 }
    00134
    00135             return null;
    00136         }
    00137     }
    00138 ?>