Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Created October 3, 2010 07:17
Show Gist options
  • Save kriswallsmith/608359 to your computer and use it in GitHub Desktop.
Save kriswallsmith/608359 to your computer and use it in GitHub Desktop.
A symfony 1 route that includes host in the pattern
<?php
class HostAwareRoute extends sfRequestRoute
{
protected
$hostRoute = null;
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
{
if ('/' != $pattern[0])
{
list($host, $pattern) = explode('/', $pattern, 2);
$this->hostRoute = $this->createHostRoute($host, $defaults, $requirements, $options);
}
parent::__construct($pattern, $defaults, $requirements, $options);
}
public function matchesUrl($url, $context = array())
{
if (false === $parameters = parent::matchesUrl($url, $context))
{
// uri does not match
return false;
}
if ($this->hostRoute)
{
if (false === $hostParameters = $this->hostRoute->matchesUrl('/'.$context['host'], $context))
{
// host does not match
return false;
}
$parameters += $hostParameters;
}
return $parameters;
}
public function matchesParameters($params, $context = array())
{
if (!$this->hostRoute)
{
return parent::matchesParameters($params, $context);
}
$hostParams = $this->extractHostParams($params);
return parent::matchesParameters($params, $context) && $this->hostRoute->matchesParameters($hostParams, $context);
}
public function generate($params, $context = array(), $absolute = false)
{
if (!$this->hostRoute)
{
return parent::generate($params, $context, $absolute);
}
$hostParams = $this->extractHostParams($params);
$protocol = isset($context['is_secure']) && $context['is_secure'] ? 'https' : 'http';
$host = $this->hostRoute->generate($hostParams, $context, false);
$prefix = isset($context['prefix']) ? $context['prefix'] : '';
$uri = parent::generate($params, $context, false);
return $protocol.':/'.$host.$prefix.$uri;
}
protected function createHostRoute($pattern, $defaults, $requirements, $options)
{
$filteredDefaults = array();
$filteredRequirements = array();
// this temporary route is just for extracting variables from the pattern
$tmp = new sfRoute($pattern);
foreach (array_keys($tmp->getVariables()) as $name)
{
if (isset($defaults[$name]))
{
$filteredDefaults[$name] = $defaults[$name];
}
if (isset($requirements[$name]))
{
$filteredRequirements[$name] = $requirements[$name];
}
}
return new sfRoute($pattern, $filteredDefaults, $filteredRequirements, $options);
}
protected function extractHostParams(& $params)
{
$hostParams = array();
foreach (array_keys($this->hostRoute->getVariables()) as $name)
{
if (isset($params[$name]))
{
$hostParams[$name] = $params[$name];
unset($params[$name]);
}
}
return $hostParams;
}
}
<?php
require_once dirname(__FILE__).'/../../config/ProjectConfiguration.class.php';
$configuration = new ProjectConfiguration();
$autoload = sfSimpleAutoload::getInstance();
$autoload->addDirectory(sfConfig::get('sf_lib_dir'));
$autoload->register();
class HostAwareRouteTest extends PHPUnit_Framework_TestCase
{
public function testHostRoute()
{
$route = new HostAwareRoute(':username.localhost/dashboard/:section', array(
'module' => 'dashboard',
'action' => 'showSection',
));
$r = new ReflectionObject($route);
$p = $r->getProperty('hostRoute');
$p->setAccessible(true);
$this->assertInstanceOf('sfRoute', $p->getValue($route));
return $route;
}
/**
* @depends testHostRoute
*/
public function testMatchesUrl($route)
{
$parameters = $route->matchesUrl('/dashboard/account', array(
'host' => 'pierre.localhost',
'method' => 'get',
));
$this->assertEquals(array(
'module' => 'dashboard',
'action' => 'showSection',
'username' => 'pierre',
'section' => 'account',
), $parameters);
}
/**
* @depends testHostRoute
*/
public function testGenerate($route)
{
$parameters = array('username' => 'pierre', 'section' => 'account');
$this->assertEquals('http://pierre.localhost/dashboard/account', $route->generate($parameters));
}
/**
* @depends testHostRoute
*/
public function testGenerateSecure($route)
{
$parameters = array('username' => 'pierre', 'section' => 'account');
$context = array('is_secure' => true);
$this->assertEquals('https://pierre.localhost/dashboard/account', $route->generate($parameters, $context));
}
/**
* @depends testHostRoute
*/
public function testGeneratePrefix($route)
{
$parameters = array('username' => 'pierre', 'section' => 'account');
$context = array('prefix' => '/frontend_dev.php');
$this->assertEquals('http://pierre.localhost/frontend_dev.php/dashboard/account', $route->generate($parameters, $context));
}
/**
* @depends testHostRoute
*/
public function testMatchesParameters($route)
{
$this->assertTrue($route->matchesParameters(array(
'module' => 'dashboard',
'action' => 'showSection',
'username' => 'pierre',
'section' => 'account',
)));
$this->assertFalse($route->matchesParameters(array(
'module' => 'dashboard',
'action' => 'showSection',
'section' => 'account',
)));
$this->assertFalse($route->matchesParameters(array(
'module' => 'dashboard',
'action' => 'showSection',
'username' => 'pierre',
)));
}
public function testNoHostRoute()
{
$route = new HostAwareRoute('/dashboard/:section', array(
'module' => 'dashboard',
'action' => 'showSection',
));
$r = new ReflectionObject($route);
$p = $r->getProperty('hostRoute');
$p->setAccessible(true);
$this->assertSame(null, $p->getValue($route));
return $route;
}
/**
* @depends testNoHostRoute
*/
public function testNoHostMatchesUrl($route)
{
$parameters = $route->matchesUrl('/dashboard/account', array('method' => 'get'));
$this->assertEquals(array(
'module' => 'dashboard',
'action' => 'showSection',
'section' => 'account',
), $parameters);
}
/**
* @depends testNoHostRoute
*/
public function testNoHostGenerate($route)
{
$parameters = array('section' => 'account');
$this->assertEquals('/dashboard/account', $route->generate($parameters));
}
/**
* @depends testNoHostRoute
*/
public function testNoHostMatchesParameters($route)
{
$this->assertTrue($route->matchesParameters(array(
'module' => 'dashboard',
'action' => 'showSection',
'section' => 'account',
)));
$this->assertFalse($route->matchesParameters(array(
'module' => 'dashboard',
'action' => 'showSection',
)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment