/** * Update a Mapper instance with a new $environ. If PATH_INFO * is present, try to match it and update mapperDict. * * @param Horde_Routes_Mapper $mapper Mapper instance to update * @param array $environ Environ to set in Mapper * @return void */ public static function updateMapper($mapper, $environ) { $mapper->environ = $environ; $mapper->utils->mapperdict = null; if (isset($environ['PATH_INFO'])) { $result = $mapper->routeMatch($environ['PATH_INFO']); $mapper->utils->mapperDict = isset($result) ? $result[0] : null; } }
public function testScanFilesystem() { $mapper = new Horde_Routes_Mapper(); $mapper->connect(':controller/:action/:id'); $scanner = new Mad_Controller_Scanner($mapper); $controllers = $scanner->scanFilesystem(MAD_ROOT . '/app/controllers'); sort($controllers); $this->assertEquals(array('application', 'error', 'unit_test'), $controllers); }
/** * Return the match dictionary for the incoming request. * * @return array The match dictionary. */ public function getMatchDict() { if ($this->_match_dict === null) { $path = $this->_request->getPath(); if (($pos = strpos($path, '?')) !== false) { $path = substr($path, 0, $pos); } if (!$path) { $path = '/'; } $this->_match_dict = new Horde_Support_Array($this->_mapper->match($path)); } return $this->_match_dict; }
public function getRequestConfiguration(Horde_Injector $injector) { $request = $injector->getInstance('Horde_Controller_Request'); $registry = $injector->getInstance('Horde_Registry'); $settingsFinder = $injector->getInstance('Horde_Core_Controller_SettingsFinder'); $config = $injector->createInstance('Horde_Core_Controller_RequestConfiguration'); $uri = substr($request->getPath(), strlen($registry->get('webroot', 'horde'))); $uri = trim($uri, '/'); if (strpos($uri, '/') === false) { $app = $uri; } else { list($app, ) = explode('/', $uri, 2); } $config->setApplication($app); // Check for route definitions. $fileroot = $registry->get('fileroot', $app); $routeFile = $fileroot . '/config/routes.php'; if (!file_exists($routeFile)) { $config->setControllerName('Horde_Core_Controller_NotFound'); return $config; } // Push $app onto the registry $registry->pushApp($app); // Application routes are relative only to the application. Let the // mapper know where they start. $this->_mapper->prefix = $registry->get('webroot', $app); // Set the application controller directory $this->_mapper->directory = $registry->get('fileroot', $app) . '/app/controllers'; // Load application routes. $mapper = $this->_mapper; include $routeFile; if (file_exists($fileroot . '/config/routes.local.php')) { include $fileroot . '/config/routes.local.php'; } // Match // @TODO Cache routes $path = $request->getPath(); if (($pos = strpos($path, '?')) !== false) { $path = substr($path, 0, $pos); } $match = $this->_mapper->match($path); if (isset($match['controller'])) { $config->setControllerName(Horde_String::ucfirst($app) . '_' . Horde_String::ucfirst($match['controller']) . '_Controller'); $config->setSettingsExporterName($settingsFinder->getSettingsExporterName($config->getControllerName())); } else { $config->setControllerName('Horde_Core_Controller_NotFound'); } return $config; }
/** * Check if request path matches any Routes to get the controller * * @return Mad_Controller_Base * @throws Mad_Controller_Exception */ public function recognize($request) { // pass a subset of the request environment // horde_routes_mapper for route matching $environ = array('REQUEST_METHOD' => $request->getMethod()); foreach (array('HTTP_HOST', 'SERVER_NAME', 'HTTPS') as $k) { $environ[$k] = $request->getServer($k); } $this->_mapper->environ = $environ; $path = $request->getPath(); if (substr($path, 0, 1) != '/') { $path = "/{$path}"; } $matchdata = $this->_mapper->match($path); if ($matchdata) { $hash = $this->formatMatchdata($matchdata); } if (empty($hash) || !isset($hash[':controller'])) { $msg = 'No routes in config/routes.php match the path: "' . $request->getPath() . '"'; throw new Mad_Controller_Exception($msg); return false; } $request->setPathParams($hash); // try to load the class $controllerName = $hash[':controller']; if (!class_exists($controllerName, false)) { $path = MAD_ROOT . '/app/controllers/' . $controllerName . '.php'; if (file_exists($path)) { require_once $path; } else { $msg = "The Controller \"{$controllerName}\" does not exist at " . $path; throw new Mad_Controller_Exception($msg); } } $controllerClassName = Mad_Support_Inflector::classify($controllerName); return new $controllerClassName(); }
public function testOtherSpecialChars() { $m = new Horde_Routes_Mapper(); $m->connect('/:year/:(slug).:(format),:(locale)', array('locale' => 'en', 'format' => 'html')); $m->createRegs(array('content')); $this->assertEquals('/2007/test', $m->generate(array('year' => 2007, 'slug' => 'test'))); $this->assertEquals('/2007/test.xml', $m->generate(array('year' => 2007, 'slug' => 'test', 'format' => 'xml'))); $this->assertEquals('/2007/test.xml,ja', $m->generate(array('year' => 2007, 'slug' => 'test', 'format' => 'xml', 'locale' => 'ja'))); $this->assertNull($m->generate(array('year' => 2007, 'format' => 'html'))); }
public function testWithResourceRouteNames() { $m = new Horde_Routes_Mapper(); $utils = $m->utils; $utils->mapperDict = array(); $m->resource('message', 'messages', array('member' => array('mark' => 'GET'), 'collection' => array('rss' => 'GET'))); $m->createRegs(array('messages')); $this->assertNull($utils->urlFor(array('controller' => 'content', 'action' => 'view'))); $this->assertNull($utils->urlFor(array('controller' => 'content'))); $this->assertNull($utils->urlFor(array('controller' => 'admin/comments'))); $this->assertEquals('/messages', $utils->urlFor('messages')); $this->assertEquals('/messages/rss', $utils->urlFor('rss_messages')); $this->assertEquals('/messages/4', $utils->urlFor('message', array('id' => 4))); $this->assertEquals('/messages/4/edit', $utils->urlFor('edit_message', array('id' => 4))); $this->assertEquals('/messages/4/mark', $utils->urlFor('mark_message', array('id' => 4))); $this->assertEquals('/messages/new', $utils->urlFor('new_message')); $this->assertEquals('/messages.xml', $utils->urlFor('formatted_messages', array('format' => 'xml'))); $this->assertEquals('/messages/rss.xml', $utils->urlFor('formatted_rss_messages', array('format' => 'xml'))); $this->assertEquals('/messages/4.xml', $utils->urlFor('formatted_message', array('id' => 4, 'format' => 'xml'))); $this->assertEquals('/messages/4/edit.xml', $utils->urlFor('formatted_edit_message', array('id' => 4, 'format' => 'xml'))); $this->assertEquals('/messages/4/mark.xml', $utils->urlFor('formatted_mark_message', array('id' => 4, 'format' => 'xml'))); $this->assertEquals('/messages/new.xml', $utils->urlFor('formatted_new_message', array('format' => 'xml'))); }
protected function getTestMatchDict($type = 'fetch') { switch ($type) { case 'empty': $route = 'trigger/*(folder).pfb'; $match = array('controller' => 'freebusy', 'action' => 'trigger'); $path = '/trigger/.pfb'; break; case 'invalid': $route = 'trigger/*(folder).pfb'; $match = array('controller' => 'freebusy', 'action' => 'trigger'); $path = '/trigger/INVALID.pfb'; break; case 'trigger': $route = 'trigger/*(folder).pfb'; $match = array('controller' => 'freebusy', 'action' => 'trigger'); $path = '/trigger/owner@example.org/Kalender.pfb'; break; case 'fetch': default: $route = ':(owner).:(type)'; $match = array('controller' => 'freebusy', 'action' => 'fetch', 'requirements' => array('type' => '(i|x|v)fb', 'owner' => '[^/]+')); $path = '/owner@example.org.xfb'; break; } $mapper = new Horde_Routes_Mapper(); $mapper->connect($route, $match); $request = new Horde_Controller_Request_Mock(); $request->setPath($path); return new Horde_Kolab_FreeBusy_Controller_MatchDict($mapper, $request); }
<?php /** * Copyright 2008-2016 Horde LLC (http://www.horde.org/) * * See the enclosed file LICENSE for license information (BSD). If you did * did not receive this file, see http://cvs.horde.org/co.php/jonah/LICENSE. * * @author Ben Klang <*****@*****.**> */ require_once __DIR__ . '/lib/Application.php'; $jonah = Horde_Registry::appInit('jonah', array('authentication' => 'none', 'session_control' => 'readonly')); $m = new Horde_Routes_Mapper(); require JONAH_BASE . '/config/routes.php'; if (file_exists(JONAH_BASE . '/config/routes.local.php')) { include JONAH_BASE . '/config/routes.local.php'; } $templates = Horde::loadConfiguration('templates.php', 'templates', 'jonah'); // Grab, and hopefully match, the URL $request = new Horde_Controller_Request_Http(); $url = $request->getPath(); $args = $request->getGetParams(); $result = $m->match('/' . $url); $criteria = array(); // @TODO: This should be handled by controller objects, but for now just use // a switch conditional until we move to Horde_Controller switch ($result['controller']) { case 'admin': // TODO: exit; case 'feed':
/** * Generates a URL. * * All keys given to urlFor are sent to the Routes Mapper instance for * generation except for:: * * anchor specified the anchor name to be appened to the path * host overrides the default (current) host if provided * protocol overrides the default (current) protocol if provided * qualified creates the URL with the host/port information as * needed * * The URL is generated based on the rest of the keys. When generating a new * URL, values will be used from the current request's parameters (if * present). The following rules are used to determine when and how to keep * the current requests parameters: * * * If the controller is present and begins with '/', no defaults are used * * If the controller is changed, action is set to 'index' unless otherwise * specified * * For example, if the current request yielded a dict (associative array) of * array('controller'=>'blog', 'action'=>'view', 'id'=>2), with the standard * ':controller/:action/:id' route, you'd get the following results:: * * urlFor(array('id'=>4)) => '/blog/view/4', * urlFor(array('controller'=>'/admin')) => '/admin', * urlFor(array('controller'=>'admin')) => '/admin/view/2' * urlFor(array('action'=>'edit')) => '/blog/edit/2', * urlFor(array('action'=>'list', id=NULL)) => '/blog/list' * * **Static and Named Routes** * * If there is a string present as the first argument, a lookup is done * against the named routes table to see if there's any matching routes. The * keyword defaults used with static routes will be sent in as GET query * arg's if a route matches. * * If no route by that name is found, the string is assumed to be a raw URL. * Should the raw URL begin with ``/`` then appropriate SCRIPT_NAME data will * be added if present, otherwise the string will be used as the url with * keyword args becoming GET query args. */ public function urlFor($first = array(), $second = array()) { if (is_array($first)) { // urlFor(array('controller' => 'foo', ...)) $routeName = null; $kargs = $first; } else { // urlFor('named_route') // urlFor('named_route', array('id' => 3, ...)) // urlFor('static_path') $routeName = $first; $kargs = $second; } $anchor = isset($kargs['anchor']) ? $kargs['anchor'] : null; $host = isset($kargs['host']) ? $kargs['host'] : null; $protocol = isset($kargs['protocol']) ? $kargs['protocol'] : null; $qualified = isset($kargs['qualified']) ? $kargs['qualified'] : null; unset($kargs['qualified']); // Remove special words from kargs, convert placeholders foreach (array('anchor', 'host', 'protocol') as $key) { if (array_key_exists($key, $kargs)) { unset($kargs[$key]); } if (array_key_exists($key . '_', $kargs)) { $kargs[$key] = $kargs[$key . '_']; unset($kargs[$key . '_']); } } $route = null; $static = false; $encoding = $this->mapper->encoding; $environ = $this->mapper->environ; $url = ''; if (isset($routeName)) { if (isset($this->mapper->routeNames[$routeName])) { $route = $this->mapper->routeNames[$routeName]; } if ($route && array_key_exists('_static', $route->defaults)) { $static = true; $url = $route->routePath; } // No named route found, assume the argument is a relative path if ($route === null) { $static = true; $url = $routeName; } if (substr($url, 0, 1) == '/' && isset($environ['SCRIPT_NAME'])) { $url = $environ['SCRIPT_NAME'] . $url; } if ($static) { if (!empty($kargs)) { $url .= '?'; $query_args = array(); foreach ($kargs as $key => $val) { $query_args[] = urlencode(utf8_decode($key)) . '=' . urlencode(utf8_decode($val)); } $url .= implode('&', $query_args); } } } if (!$static) { if ($route) { $newargs = $route->defaults; foreach ($kargs as $key => $value) { $newargs[$key] = $value; } // If this route has a filter, apply it if (!empty($route->filter)) { $newargs = call_user_func($route->filter, $newargs); } $newargs = $this->_subdomainCheck($newargs); } else { $newargs = $this->_screenArgs($kargs); } $anchor = isset($newargs['_anchor']) ? $newargs['_anchor'] : $anchor; unset($newargs['_anchor']); $host = isset($newargs['_host']) ? $newargs['_host'] : $host; unset($newargs['_host']); $protocol = isset($newargs['_protocol']) ? $newargs['_protocol'] : $protocol; unset($newargs['_protocol']); $url = $this->mapper->generate($newargs); } if (!empty($anchor)) { $url .= '#' . self::urlQuote($anchor, $encoding); } if (!empty($host) || !empty($qualified) || !empty($protocol)) { $http_host = isset($environ['HTTP_HOST']) ? $environ['HTTP_HOST'] : null; $server_name = isset($environ['SERVER_NAME']) ? $environ['SERVER_NAME'] : null; $fullhost = !is_null($http_host) ? $http_host : $server_name; if (empty($host) && empty($qualified)) { $host = explode(':', $fullhost); $host = $host[0]; } else { if (empty($host)) { $host = $fullhost; } } if (empty($protocol)) { if (!empty($environ['HTTPS']) && $environ['HTTPS'] != 'off') { $protocol = 'https'; } else { $protocol = 'http'; } } if ($url !== null) { $url = $protocol . '://' . $host . $url; } } return $url; }
public function testFormattedResourceMember() { $m = new Horde_Routes_Mapper(); $m->resource('message', 'messages'); $m->createRegs(array('messages')); $path = '/messages/42.xml'; $m->environ = array('REQUEST_METHOD' => 'GET'); $this->assertEquals(array('controller' => 'messages', 'action' => 'show', 'id' => 42, 'format' => 'xml'), $m->match($path)); $m->environ = array('REQUEST_METHOD' => 'POST'); $this->assertNull($m->match($path)); $m->environ = array('REQUEST_METHOD' => 'PUT'); $this->assertEquals(array('controller' => 'messages', 'action' => 'update', 'id' => 42, 'format' => 'xml'), $m->match($path)); $m->environ = array('REQUEST_METHOD' => 'DELETE'); $this->assertEquals(array('controller' => 'messages', 'action' => 'delete', 'id' => 42, 'format' => 'xml'), $m->match($path)); }
public function testAutoControllerScan() { $hereDir = __DIR__; $controllerDir = "{$hereDir}/fixtures/controllers"; $m = new Horde_Routes_Mapper(array('directory' => $controllerDir)); $m->alwaysScan = true; $m->connect(':controller/:action/:id'); $expected = array('action' => 'index', 'controller' => 'content', 'id' => null); $this->assertEquals($expected, $m->match('/content')); $expected = array('action' => 'index', 'controller' => 'users', 'id' => null); $this->assertEquals($expected, $m->match('/users')); $expected = array('action' => 'index', 'controller' => 'admin/users', 'id' => null); $this->assertEquals($expected, $m->match('/admin/users')); }