Source for file router.php

Documentation is available at router.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Router
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Set the available masks for the routing mode
  14.  */
  15. const JROUTER_MODE_RAW 0;
  16. const JROUTER_MODE_SEF 1;
  17.  
  18. /**
  19.  * Class to create and parse routes
  20.  *
  21.  * @package     Joomla.Libraries
  22.  * @subpackage  Router
  23.  * @since       1.5
  24.  */
  25. class JRouter
  26. {
  27.     /**
  28.      * The rewrite mode
  29.      *
  30.      * @var    integer 
  31.      * @since  1.5
  32.      */
  33.     protected $mode = null;
  34.  
  35.     /**
  36.      * The rewrite mode
  37.      *
  38.      * @var    integer 
  39.      * @since  1.5
  40.      * @deprecated  4.0 Will convert to $mode
  41.      */
  42.     protected $_mode = null;
  43.  
  44.     /**
  45.      * An array of variables
  46.      *
  47.      * @var     array 
  48.      * @since   1.5
  49.      */
  50.     protected $vars = array();
  51.  
  52.     /**
  53.      * An array of variables
  54.      *
  55.      * @var     array 
  56.      * @since  1.5
  57.      * @deprecated  4.0 Will convert to $vars
  58.      */
  59.     protected $_vars = array();
  60.  
  61.     /**
  62.      * An array of rules
  63.      *
  64.      * @var    array 
  65.      * @since  1.5
  66.      */
  67.     protected $rules = array(
  68.         'build' => array(),
  69.         'parse' => array()
  70.     );
  71.  
  72.     /**
  73.      * An array of rules
  74.      *
  75.      * @var    array 
  76.      * @since  1.5
  77.      * @deprecated  4.0 Will convert to $rules
  78.      */
  79.     protected $_rules = array(
  80.         'build' => array(),
  81.         'parse' => array()
  82.     );
  83.  
  84.     /**
  85.      * JRouter instances container.
  86.      *
  87.      * @var    array 
  88.      * @since  1.7
  89.      */
  90.     protected static $instances array();
  91.  
  92.     /**
  93.      * Class constructor
  94.      *
  95.      * @param   array  $options  Array of options
  96.      *
  97.      * @since   1.5
  98.      */
  99.     public function __construct($options array())
  100.     {
  101.         if (array_key_exists('mode'$options))
  102.         {
  103.             $this->_mode = $options['mode'];
  104.         }
  105.         else
  106.         {
  107.             $this->_mode = JROUTER_MODE_RAW;
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Returns the global JRouter object, only creating it if it
  113.      * doesn't already exist.
  114.      *
  115.      * @param   string  $client   The name of the client
  116.      * @param   array   $options  An associative array of options
  117.      *
  118.      * @return  JRouter  A JRouter object.
  119.      *
  120.      * @since   1.5
  121.      * @throws  RuntimeException
  122.      */
  123.     public static function getInstance($client$options array())
  124.     {
  125.         if (empty(self::$instances[$client]))
  126.         {
  127.             // Create a JRouter object
  128.             $classname 'JRouter' ucfirst($client);
  129.  
  130.             if (!class_exists($classname))
  131.             {
  132.                 // @deprecated 4.0 Everything in this block is deprecated but the warning is only logged after the file_exists
  133.                 // Load the router object
  134.                 $info JApplicationHelper::getClientInfo($clienttrue);
  135.  
  136.                 if (is_object($info))
  137.                 {
  138.                     $path $info->path '/includes/router.php';
  139.  
  140.                     if (file_exists($path))
  141.                     {
  142.                         JLog::add('Non-autoloadable JRouter subclasses are deprecated, support will be removed in 4.0.'JLog::WARNING'deprecated');
  143.                         include_once $path;
  144.                     }
  145.                 }
  146.             }
  147.  
  148.             if (class_exists($classname))
  149.             {
  150.                 self::$instances[$clientnew $classname($options);
  151.             }
  152.             else
  153.             {
  154.                 throw new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_ROUTER_LOAD'$client)500);
  155.             }
  156.         }
  157.  
  158.         return self::$instances[$client];
  159.     }
  160.  
  161.     /**
  162.      * Function to convert a route to an internal URI
  163.      *
  164.      * @param   JUri  &$uri  The uri.
  165.      *
  166.      * @return  array 
  167.      *
  168.      * @since   1.5
  169.      */
  170.     public function parse(&$uri)
  171.     {
  172.         // Process the parsed variables based on custom defined rules
  173.         $vars $this->_processParseRules($uri);
  174.  
  175.         // Parse RAW URL
  176.         if ($this->_mode == JROUTER_MODE_RAW)
  177.         {
  178.             $vars += $this->_parseRawRoute($uri);
  179.         }
  180.  
  181.         // Parse SEF URL
  182.         if ($this->_mode == JROUTER_MODE_SEF)
  183.         {
  184.             $vars += $this->_parseSefRoute($uri);
  185.         }
  186.  
  187.         return array_merge($this->getVars()$vars);
  188.     }
  189.  
  190.     /**
  191.      * Function to convert an internal URI to a route
  192.      *
  193.      * @param   string  $url  The internal URL
  194.      *
  195.      * @return  string  The absolute search engine friendly URL
  196.      *
  197.      * @since   1.5
  198.      */
  199.     public function build($url)
  200.     {
  201.         // Create the URI object
  202.         $uri $this->_createURI($url);
  203.  
  204.         // Process the uri information based on custom defined rules
  205.         $this->_processBuildRules($uri);
  206.  
  207.         // Build RAW URL
  208.         if ($this->_mode == JROUTER_MODE_RAW)
  209.         {
  210.             $this->_buildRawRoute($uri);
  211.         }
  212.  
  213.         // Build SEF URL : mysite/route/index.php?var=x
  214.         if ($this->_mode == JROUTER_MODE_SEF)
  215.         {
  216.             $this->_buildSefRoute($uri);
  217.         }
  218.  
  219.         return $uri;
  220.     }
  221.  
  222.     /**
  223.      * Get the router mode
  224.      *
  225.      * @return  integer 
  226.      *
  227.      * @since   1.5
  228.      */
  229.     public function getMode()
  230.     {
  231.         return $this->_mode;
  232.     }
  233.  
  234.     /**
  235.      * Set the router mode
  236.      *
  237.      * @param   integer  $mode  The routing mode.
  238.      *
  239.      * @return  void 
  240.      *
  241.      * @since   1.5
  242.      */
  243.     public function setMode($mode)
  244.     {
  245.         $this->_mode = $mode;
  246.     }
  247.  
  248.     /**
  249.      * Set a router variable, creating it if it doesn't exist
  250.      *
  251.      * @param   string   $key     The name of the variable
  252.      * @param   mixed    $value   The value of the variable
  253.      * @param   boolean  $create  If True, the variable will be created if it doesn't exist yet
  254.      *
  255.      * @return  void 
  256.      *
  257.      * @since   1.5
  258.      */
  259.     public function setVar($key$value$create true)
  260.     {
  261.         if ($create || array_key_exists($key$this->_vars))
  262.         {
  263.             $this->_vars[$key$value;
  264.         }
  265.     }
  266.  
  267.     /**
  268.      * Set the router variable array
  269.      *
  270.      * @param   array    $vars   An associative array with variables
  271.      * @param   boolean  $merge  If True, the array will be merged instead of overwritten
  272.      *
  273.      * @return  void 
  274.      *
  275.      * @since   1.5
  276.      */
  277.     public function setVars($vars array()$merge true)
  278.     {
  279.         if ($merge)
  280.         {
  281.             $this->_vars = array_merge($this->_vars$vars);
  282.         }
  283.         else
  284.         {
  285.             $this->_vars = $vars;
  286.         }
  287.     }
  288.  
  289.     /**
  290.      * Get a router variable
  291.      *
  292.      * @param   string  $key  The name of the variable
  293.      *
  294.      * @return  mixed  Value of the variable
  295.      *
  296.      * @since   1.5
  297.      */
  298.     public function getVar($key)
  299.     {
  300.         $result null;
  301.  
  302.         if (isset($this->_vars[$key]))
  303.         {
  304.             $result $this->_vars[$key];
  305.         }
  306.  
  307.         return $result;
  308.     }
  309.  
  310.     /**
  311.      * Get the router variable array
  312.      *
  313.      * @return  array  An associative array of router variables
  314.      *
  315.      * @since   1.5
  316.      */
  317.     public function getVars()
  318.     {
  319.         return $this->_vars;
  320.     }
  321.  
  322.     /**
  323.      * Attach a build rule
  324.      *
  325.      * @param   callback  $callback  The function to be called
  326.      *
  327.      * @return  void 
  328.      *
  329.      * @since   1.5
  330.      */
  331.     public function attachBuildRule($callback)
  332.     {
  333.         $this->_rules['build'][$callback;
  334.     }
  335.  
  336.     /**
  337.      * Attach a parse rule
  338.      *
  339.      * @param   callback  $callback  The function to be called.
  340.      *
  341.      * @return  void 
  342.      *
  343.      * @since   1.5
  344.      */
  345.     public function attachParseRule($callback)
  346.     {
  347.         $this->_rules['parse'][$callback;
  348.     }
  349.  
  350.     /**
  351.      * Function to convert a raw route to an internal URI
  352.      *
  353.      * @param   JUri  &$uri  The raw route
  354.      *
  355.      * @return  boolean 
  356.      *
  357.      * @since   1.5
  358.      * @deprecated  4.0  Use parseRawRoute() instead
  359.      */
  360.     protected function _parseRawRoute(&$uri)
  361.     {
  362.         return $this->parseRawRoute($uri);
  363.     }
  364.  
  365.     /**
  366.      * Function to convert a raw route to an internal URI
  367.      *
  368.      * @param   JUri  &$uri  The raw route
  369.      *
  370.      * @return  boolean 
  371.      *
  372.      * @since   3.2
  373.      */
  374.     protected function parseRawRoute(&$uri)
  375.     {
  376.         return false;
  377.     }
  378.  
  379.     /**
  380.      * Function to convert a sef route to an internal URI
  381.      *
  382.      * @param   JUri  &$uri  The sef URI
  383.      *
  384.      * @return  string  Internal URI
  385.      *
  386.      * @since   1.5
  387.      * @deprecated  4.0  Use parseSefRoute() instead
  388.      */
  389.     protected function _parseSefRoute(&$uri)
  390.     {
  391.         return $this->parseSefRoute($uri);
  392.     }
  393.  
  394.     /**
  395.      * Function to convert a sef route to an internal URI
  396.      *
  397.      * @param   JUri  &$uri  The sef URI
  398.      *
  399.      * @return  string  Internal URI
  400.      *
  401.      * @since   3.2
  402.      */
  403.     protected function parseSefRoute(&$uri)
  404.     {
  405.         return false;
  406.     }
  407.  
  408.     /**
  409.      * Function to build a raw route
  410.      *
  411.      * @param   JUri  &$uri  The internal URL
  412.      *
  413.      * @return  string  Raw Route
  414.      *
  415.      * @since   1.5
  416.      * @deprecated  4.0  Use buildRawRoute() instead
  417.      */
  418.     protected function _buildRawRoute(&$uri)
  419.     {
  420.         return $this->buildRawRoute($uri);
  421.     }
  422.  
  423.     /**
  424.      * Function to build a raw route
  425.      *
  426.      * @param   JUri  &$uri  The internal URL
  427.      *
  428.      * @return  string  Raw Route
  429.      *
  430.      * @since   3.2
  431.      */
  432.     protected function buildRawRoute(&$uri)
  433.     {
  434.     }
  435.  
  436.     /**
  437.      * Function to build a sef route
  438.      *
  439.      * @param   JUri  &$uri  The uri
  440.      *
  441.      * @return  string  The SEF route
  442.      *
  443.      * @since   1.5
  444.      * @deprecated  4.0  Use buildSefRoute() instead
  445.      */
  446.     protected function _buildSefRoute(&$uri)
  447.     {
  448.         return $this->buildSefRoute($uri);
  449.     }
  450.  
  451.     /**
  452.      * Function to build a sef route
  453.      *
  454.      * @param   JUri  &$uri  The uri
  455.      *
  456.      * @return  string  The SEF route
  457.      *
  458.      * @since   3.2
  459.      */
  460.     protected function buildSefRoute(&$uri)
  461.     {
  462.     }
  463.  
  464.     /**
  465.      * Process the parsed router variables based on custom defined rules
  466.      *
  467.      * @param   JUri  &$uri  The URI to parse
  468.      *
  469.      * @return  array  The array of processed URI variables
  470.      *
  471.      * @since   1.5
  472.      * @deprecated  4.0  Use processParseRules() instead
  473.      */
  474.     protected function _processParseRules(&$uri)
  475.     {
  476.         return $this->processParseRules($uri);
  477.     }
  478.  
  479.     /**
  480.      * Process the parsed router variables based on custom defined rules
  481.      *
  482.      * @param   JUri  &$uri  The URI to parse
  483.      *
  484.      * @return  array  The array of processed URI variables
  485.      *
  486.      * @since   3.2
  487.      */
  488.     protected function processParseRules(&$uri)
  489.     {
  490.         $vars array();
  491.  
  492.         foreach ($this->_rules['parse'as $rule)
  493.         {
  494.             $vars += call_user_func_array($rulearray(&$this&$uri));
  495.         }
  496.  
  497.         return $vars;
  498.     }
  499.  
  500.     /**
  501.      * Process the build uri query data based on custom defined rules
  502.      *
  503.      * @param   JUri  &$uri  The URI
  504.      *
  505.      * @return  void 
  506.      *
  507.      * @since   1.5
  508.      * @deprecated  4.0  Use processBuildRules() instead
  509.      */
  510.     protected function _processBuildRules(&$uri)
  511.     {
  512.         $this->processBuildRules($uri);
  513.     }
  514.  
  515.     /**
  516.      * Process the build uri query data based on custom defined rules
  517.      *
  518.      * @param   JUri  &$uri  The URI
  519.      *
  520.      * @return  void 
  521.      *
  522.      * @since   3.2
  523.      */
  524.     protected function processBuildRules(&$uri)
  525.     {
  526.         foreach ($this->_rules['build'as $rule)
  527.         {
  528.             call_user_func_array($rulearray(&$this&$uri));
  529.         }
  530.     }
  531.  
  532.     /**
  533.      * Create a uri based on a full or partial url string
  534.      *
  535.      * @param   string  $url  The URI
  536.      *
  537.      * @return  JUri 
  538.      *
  539.      * @since   1.5
  540.      * @deprecated  4.0  Use createURI() instead
  541.      */
  542.     protected function _createURI($url)
  543.     {
  544.         return $this->createURI($url);
  545.     }
  546.  
  547.     /**
  548.      * Create a uri based on a full or partial url string
  549.      *
  550.      * @param   string  $url  The URI
  551.      *
  552.      * @return  JUri 
  553.      *
  554.      * @since   3.2
  555.      */
  556.     protected function createURI($url)
  557.     {
  558.         // Create full URL if we are only appending variables to it
  559.         if (substr($url01== '&')
  560.         {
  561.             $vars array();
  562.  
  563.             if (strpos($url'&amp;'!== false)
  564.             {
  565.                 $url str_replace('&amp;''&'$url);
  566.             }
  567.  
  568.             parse_str($url$vars);
  569.  
  570.             $vars array_merge($this->getVars()$vars);
  571.  
  572.             foreach ($vars as $key => $var)
  573.             {
  574.                 if ($var == "")
  575.                 {
  576.                     unset($vars[$key]);
  577.                 }
  578.             }
  579.  
  580.             $url 'index.php?' JUri::buildQuery($vars);
  581.         }
  582.  
  583.         // Decompose link into url component parts
  584.         return new JUri($url);
  585.     }
  586.  
  587.     /**
  588.      * Encode route segments
  589.      *
  590.      * @param   array  $segments  An array of route segments
  591.      *
  592.      * @return  array  Array of encoded route segments
  593.      *
  594.      * @since   1.5
  595.      * @deprecated  4.0  Use encodeSegments() instead
  596.      */
  597.     protected function _encodeSegments($segments)
  598.     {
  599.         return $this->encodeSegments($segments);
  600.     }
  601.  
  602.     /**
  603.      * Encode route segments
  604.      *
  605.      * @param   array  $segments  An array of route segments
  606.      *
  607.      * @return  array  Array of encoded route segments
  608.      *
  609.      * @since   3.2
  610.      */
  611.     protected function encodeSegments($segments)
  612.     {
  613.         $total count($segments);
  614.  
  615.         for ($i 0$i $total$i++)
  616.         {
  617.             $segments[$istr_replace(':''-'$segments[$i]);
  618.         }
  619.  
  620.         return $segments;
  621.     }
  622.  
  623.     /**
  624.      * Decode route segments
  625.      *
  626.      * @param   array  $segments  An array of route segments
  627.      *
  628.      * @return  array  Array of decoded route segments
  629.      *
  630.      * @since   1.5
  631.      * @deprecated  4.0  Use decodeSegments() instead
  632.      */
  633.     protected function _decodeSegments($segments)
  634.     {
  635.         return $this->decodeSegments($segments);
  636.     }
  637.  
  638.     /**
  639.      * Decode route segments
  640.      *
  641.      * @param   array  $segments  An array of route segments
  642.      *
  643.      * @return  array  Array of decoded route segments
  644.      *
  645.      * @since   3.2
  646.      */
  647.     protected function decodeSegments($segments)
  648.     {
  649.         $total count($segments);
  650.  
  651.         for ($i 0$i $total$i++)
  652.         {
  653.             $segments[$ipreg_replace('/-/'':'$segments[$i]1);
  654.         }
  655.  
  656.         return $segments;
  657.     }
  658. }

Documentation generated on Tue, 19 Nov 2013 15:12:28 +0100 by phpDocumentor 1.4.3