ManiaLib 1.0b1 API Reference

Source for file RequestEngineMVC.class.php

Documentation is available at RequestEngineMVC.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  * @subpackage MVC
  7.  */
  8.  
  9. /**
  10.  * RequestEngine designed for MVC framework
  11.  * @package ManiaLib
  12.  * @subpackage MVC
  13.  */
  14. {
  15.     /**
  16.      * @ignore
  17.      */
  18.     static private $instance;
  19.     
  20.     /**#@+
  21.      * @ignore 
  22.      */
  23.     protected $action;
  24.     protected $controller;
  25.     /**#@-*/
  26.     
  27.     /**
  28.      * Gets the instance
  29.      * @return RequestEngineMVC 
  30.      */
  31.     public static function getInstance()
  32.     {
  33.         if (!self::$instance)
  34.         {
  35.             $class = __CLASS__;
  36.             self::$instance new $class;
  37.         }
  38.         return self::$instance;
  39.     }
  40.     
  41.     /**
  42.      * @ignore
  43.      */
  44.     protected function __construct()
  45.     {
  46.         parent::__construct();
  47.         $route array_keys($this->params);
  48.         $route reset($route);
  49.         if($route && substr($route01)=='/')
  50.         {
  51.             array_shift($this->params);
  52.             $route explode('/'$route);
  53.         }
  54.         else 
  55.         {
  56.             $route array();
  57.         }
  58.         // $route[0] is null because of the first '/'
  59.         $this->controller array_key_exists(1$route&& $route[1$route[1APP_MVC_DEFAULT_CONTROLLER;
  60.         $this->action array_key_exists(2$route&& $route[2$route[2null;
  61.         $this->controller Route::separatorToUpperCamelCase($this->controller);
  62.         $this->action $this->action Route::separatorToCamelCase($this->actionnull;
  63.     }
  64.     
  65.     public function getAction($defaultAction null)
  66.     {
  67.         return $this->action $this->action $defaultAction;
  68.     }
  69.         
  70.     public function getController()
  71.     {
  72.         return $this->controller;
  73.     }
  74.     
  75.     /**
  76.      * Redirects to the specified route with all defined GET vars in the URL
  77.      * @param string Can be the name of a controller or a class const of Route
  78.      * @param string Can be the name of an action or a class const of Route
  79.      */
  80.     public function redirectManialink($controller Route::CUR$action Route::CUR)
  81.     {
  82.         $manialink $this->createLink($controller$action);
  83.         $this->redirectManialinkAbsolute($manialink);
  84.     }
  85.     
  86.     /**
  87.      * Redirects to the specified route with, with names of GET vars as parameters of the method
  88.      * @param string Can be the name of a controller or a class const of Route
  89.      * @param string Can be the name of an action or a class const of Route
  90.      */
  91.     public function redirectManialinkArgList($controller$action)
  92.     {
  93.         $arr func_get_args();
  94.         array_shift($arr);
  95.         $args array();
  96.         foreach($arr as $elt)
  97.         {
  98.             if(array_key_exists($elt$this->params))
  99.             {
  100.                 $args[$elt$this->params[$elt];
  101.             }    
  102.         }
  103.         $manialink $this->createLinkString($controller$action$args);
  104.         $this->redirectManialinkAbsolute($manialink);
  105.     }
  106.     
  107.     /**
  108.      * Creates a link to the specified route with all defined GET vars in the URL
  109.      * @param string Can be the name of a controller or a class const of Route
  110.      * @param string Can be the name of an action or a class const of Route
  111.      */
  112.     public function createLink($controller Route::CUR$action Route::CUR)
  113.     {
  114.         return $this->createLinkString($controller$action$this->params);
  115.     }
  116.     
  117.     /**
  118.      * Creates a link to the specified route with, with names of GET vars as parameters of the method
  119.      * @param string Can be the name of a controller or a class const of Route
  120.      * @param string Can be the name of an action or a class const of Route
  121.      */
  122.     function createLinkArgList($controller Route::CUR$action Route::CUR)
  123.     {
  124.         $arr func_get_args();
  125.         array_shift($arr);
  126.         $args array();
  127.         foreach($arr as $elt)
  128.         {
  129.             if(array_key_exists($elt$this->params))
  130.             {
  131.                 $args[$elt$this->params[$elt];
  132.             }    
  133.         }
  134.         return $this->createLinkString($controller$action$args);
  135.     }
  136.     
  137.     /**
  138.      * @ignore
  139.      */
  140.     protected function createLinkString($controller Route::CUR$action Route::CUR$params array())
  141.     {
  142.         switch($controller)
  143.         {
  144.             case Route::CUR:
  145.             case null:
  146.                 $controller $this->getController();
  147.                 break;
  148.                 
  149.             case Route::DEF:
  150.                 $controller APP_MVC_DEFAULT_CONTROLLER;
  151.                 break;
  152.                 
  153.             case Route::NONE:
  154.                 $controller null;
  155.                 break;
  156.                 
  157.             default:
  158.                 // Nothing here
  159.         }
  160.         
  161.         switch($action)
  162.         {
  163.             case Route::CUR:
  164.             case null:
  165.                  $action $this->getAction(null);
  166.                  break;
  167.                  
  168.             case Route::DEF:
  169.             case Route::NONE:
  170.                 $action null;
  171.                 break;
  172.                 
  173.             default:
  174.                 // Nothing here
  175.         }
  176.         $controller Route::camelCaseToSeparator($controller);
  177.         $action Route::camelCaseToSeparator($action);
  178.         
  179.         // URL string
  180.         if(APP_MVC_USE_URL_REWRITE)
  181.         {
  182.             $url APP_URL.$controller.'/';
  183.             if($action)
  184.             {
  185.                 $url .= $action.'/';
  186.             }
  187.         }
  188.         else
  189.         {
  190.             $url APP_URL;
  191.             $route '';
  192.             if($controller)
  193.             {
  194.                 $route '/'.$controller.'/';
  195.                 if($action)
  196.                 {
  197.                     $route .= $action.'/';
  198.                 }
  199.             }
  200.             if($route)
  201.             {
  202.                 $url $url.'?'.$route;
  203.             }
  204.         }
  205.         // Create parameter string
  206.         if(count($params))
  207.         {
  208.             $params http_build_query($params'''&');
  209.         }
  210.         else
  211.         {
  212.             $params '';
  213.         }
  214.         if(APP_MVC_USE_URL_REWRITE)
  215.         {
  216.             return $url.($params'?'.$params:'');
  217.         }    
  218.         else
  219.         {
  220.             return $url.($params($route?'&':'?').$params '');
  221.         }
  222.         
  223.     }
  224. }
  225.  
  226. ?>