# ProxyController.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2008 by Dmitry V. Sokolov, Denis M. Gabaidulin           *
    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     // TODO: add action => requestType mapper
    00016     final class ProxyController implements Controller
    00017     {
    00018         private $innerController    = null;
    00019         private $request            = null;
    00020         private $requestType        = null;
    00021         private $requestGetter      = null;
    00022
    00023         private static $requestGetterMap = array(
    00024             RequestType::ATTACHED   => 'Attached',
    00025             RequestType::GET        => 'Get',
    00026             RequestType::POST       => 'Post'
    00027         );
    00028
    00032         public static function create()
    00033         {
    00034             return new self;
    00035         }
    00036
    00037         public function __construct()
    00038         {
    00039             $this->requestType = RequestType::post();
    00040         }
    00041
    00045         public function setInner(Controller $controller)
    00046         {
    00047             $this->innerController = $controller;
    00048
    00049             return $this;
    00050         }
    00051
    00055         public function getInner()
    00056         {
    00057             return $this->innerController;
    00058         }
    00059
    00060         public function getName()
    00061         {
    00062             return get_class($this->innerController);
    00063         }
    00064
    00068         public function handleRequest(HttpRequest $request)
    00069         {
    00070             return $this->getInner()->handleRequest($request);
    00071         }
    00072
    00076         public function setRequestType(RequestType $requestType)
    00077         {
    00078             $this->requestType = $requestType;
    00079
    00080             return $this;
    00081         }
    00082
    00083         public function isActive($request)
    00084         {
    00085             return $this->fetchVariable('controller', $request)
    00086                 ? (
    00087                     $this->fetchVariable('controller', $request)
    00088                     == get_class($this->getInner())
    00089                 )
    00090                 : false;
    00091         }
    00092
    00093         public function getRequestGetter()
    00094         {
    00095             Assert::isNotNull($this->requestType);
    00096
    00097             if (!$this->requestGetter)
    00098                 $this->requestGetter =
    00099                     self::$requestGetterMap[$this->requestType->getId()];
    00100
    00101             return $this->requestGetter;
    00102         }
    00103
    00104         private function fetchVariable($name, HttpRequest $request)
    00105         {
    00106             return $request->{'has'.$this->getRequestGetter().'Var'}($name)
    00107                 ? $request->{'get'.$this->getRequestGetter().'Var'}($name)
    00108                 : false;
    00109         }
    00110     }
    00111 ?>