public function match($path, Request $request = null) { if ($this->pattern === null) { $this->compile(); } if (isset($this->config['options']['via']) && $request !== null && $this->config['options']['via'] != $request->getMethod()) { return false; } if (preg_match($this->pattern, $path, $matches) === 0) { return false; } preg_match_all('/:([a-z0-9_]+)/', $this->config['template'], $m); $sections = isset($m[1]) ? $m[1] : array(); $sections[] = 'extension'; $matches = array_intersect_key($matches, array_flip($sections)); $specialSections = array('controller', 'action', 'extension'); foreach ($specialSections as $key) { if (isset($matches[$key])) { $this->config['params'][$key] = $matches[$key]; unset($matches[$key]); } } return array_merge($this->config['params'], array('args' => $matches)); }
public function testIsIphone() { $request = new Request; $_SERVER['HTTP_USER_AGENT'] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3"; $this->assertTrue($request->isIphone()); $_SERVER['HTTP_USER_AGENT'] = "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3"; $this->assertTrue($request->isIphone()); // Firefox on Windows 7 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 GTB6 (.NET CLR 3.5.30729)'; $this->assertFalse($request->isIphone()); }
public static function normalize(Request $request) { list($contentType) = explode(';', $request->server->get('CONTENT_TYPE', '')); if (empty($contentType)) { return false; } if (isset(static::$_config['decoders'][$contentType])) { $raw = $request->rawBody(); if (!empty($raw)) { $class = self::$_config['decoders'][$contentType]; $decoder = new $class(); $request->data = new ParamCollection($decoder->decode($raw)); } } }
public function testIsSSLOnReverseProxy() { $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; $request = Request::fromGlobals(); $this->assertTrue($request->isSSL()); $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http'; $request = Request::fromGlobals(); $this->assertFalse($request->isSSL()); }
public function testParseUrl() { Router::resource('companies'); Router::resource('companies.people'); Router::connect('/:controller/:action/:id', array()); Router::connect('/:controller/:action', array()); Router::connect('/:controller', array('action' => 'index')); $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parseUrl('/companies', Request::fromGlobals()); $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'index', 'action_method' => 'indexAction', 'args' => array(), 'namespace' => false, 'extension' => 'html'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'POST'; $result = Router::parseUrl('/companies', Request::fromGlobals()); $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'add', 'action_method' => 'addAction', 'args' => array(), 'namespace' => false, 'extension' => 'html'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'PUT'; $result = Router::parseUrl('/companies/1'); $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'edit', 'action_method' => 'editAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'html'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'DELETE'; $result = Router::parseUrl('/companies/1', Request::fromGlobals()); $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'delete', 'action_method' => 'deleteAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'html'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parseUrl('/companies/1.json', Request::fromGlobals()); $expected = array('controller' => 'companies', 'controller_class' => 'CompaniesController', 'action' => 'view', 'action_method' => 'viewAction', 'args' => array('id' => '1'), 'namespace' => false, 'extension' => 'json'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parseUrl('/companies/1/people/25.xml', Request::fromGlobals()); $expected = array('controller' => 'people', 'controller_class' => 'PeopleController', 'action' => 'view', 'action_method' => 'viewAction', 'args' => array('company_id' => '1', 'id' => '25'), 'namespace' => false, 'extension' => 'xml'); $this->assertEquals($expected, $result); $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parseUrl('/users/edit/23', Request::fromGlobals()); $expected = array('controller' => 'users', 'controller_class' => 'UsersController', 'action' => 'edit', 'action_method' => 'editAction', 'args' => array('id' => '23'), 'namespace' => false, 'extension' => 'html'); $this->assertEquals($expected, $result); }
<?php /** * RoxPHP * * Copyright (C) 2008 - 2011 Ramon Torres * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) 2008 - 2011 Ramon Torres * @package App * @license The MIT License (http://www.opensource.org/licenses/mit-license.php) */ // include the bootstrap file require dirname(__DIR__) . '/config/bootstrap.php'; use rox\action\Dispatcher; use rox\http\Request; $dispatcher = new Dispatcher(); $response = $dispatcher->dispatch(Request::fromGlobals()); $response->render();