/**
  * Test the JInput::def method.
  *
  * @return  void
  *
  * @since   12.1
  */
 public function testDef()
 {
     $_REQUEST['foo'] = 'bar';
     $this->class->def('foo', 'nope');
     $this->assertThat($_REQUEST['foo'], $this->equalTo('bar'), 'Line: ' . __LINE__ . '.');
     $this->class->def('Joomla', 'is great');
     $this->assertThat($_REQUEST['Joomla'], $this->equalTo('is great'), 'Line: ' . __LINE__ . '.');
 }
Exemplo n.º 2
0
 /**
  * Parse the given route and return the name of a controller mapped to the given route.
  *
  * @param   string  $route  The route string for which to find and execute a controller.
  *
  * @return  string  The controller name for the given route excluding prefix.
  *
  * @since   1.0
  * @throws  \InvalidArgumentException
  */
 protected function parseRoute($route)
 {
     $controller = false;
     // Trim the query string off.
     $route = preg_replace('/([^?]*).*/u', '\\1', $route);
     // Sanitize and explode the route.
     $route = trim(static::parseUrl($route, PHP_URL_PATH), ' /');
     // If the route is empty then simply return the default route.  No parsing necessary.
     if ($route == '') {
         return $this->default;
     }
     // Iterate through all of the known route maps looking for a match.
     foreach ($this->maps as $key => $rule) {
         if (preg_match($rule['regex'], $route, $matches)) {
             // If we have gotten this far then we have a positive match.
             $controller = $rule['controller'];
             // Time to set the input variables.
             // We are only going to set them if they don't already exist to avoid overwriting things.
             foreach ($rule['vars'] as $i => $var) {
                 $this->input->def($var, $matches[$i + 1]);
                 // Don't forget to do an explicit set on the GET superglobal.
                 $this->input->get->def($var, $matches[$i + 1]);
             }
             $this->input->def('_rawRoute', $route);
             $name = $key;
             break;
         }
     }
     // We were unable to find a route match for the request.  Panic.
     if (!$controller) {
         throw new \InvalidArgumentException(sprintf('Unable to handle request for route `%s`.', $route), 404);
     }
     if (is_callable($this->parseHandler[$name])) {
         call_user_func_array($this->parseHandler[$name], array($controller, $this->input));
     }
     return $controller;
 }