Skip to content

Commit

Permalink
Implement router, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Apr 19, 2012
1 parent 1626e50 commit ca62257
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 12 deletions.
48 changes: 48 additions & 0 deletions libs/controller/class.Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
class Router {
/**
* Path/controller associative array
* @var array
*/
public static $routes = array();
/**
* Path/parameter associative array
* @var array
*/
public static $route_parameters=array();
/**
* Route request and return results of appropriate controller call.
* @return str
*/
public function route($session_started=false) {
$url = explode('?',$_SERVER['REQUEST_URI']);
$path = mb_strtolower($url[0]);
while (substr($path, -1) == '/') {
$path = mb_substr($path,0,(mb_strlen($path)-1));
}
$path_components = explode('/', $path);

$slug = $path_components[1];
$slug = ($slug=='')?'index':$slug;
if (isset(self::$routes[$slug])) {
if (isset(self::$route_parameters[$slug])) {
foreach (self::$route_parameters[$slug] as $index=>$parameter) {
$_GET[$parameter] = $path_components[$index+2];
}
}
$controller = new self::$routes[$slug]($session_started);
return $controller->go();
} else {
return "404 route not found: ".$slug;
}
}
/**
* Add route.
* @param $slug
* @param $controller_name
*/
public function addRoute($slug, $controller_name, $parameters=null) {
self::$routes[$slug] = $controller_name;
self::$route_parameters[$slug] = $parameters;
}
}
6 changes: 6 additions & 0 deletions libs/controller/class.TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public function control() {
$this->setViewTemplate('testcontroller.tpl');
}
$this->addToView('test', 'Testing, testing, 123');
if (isset($_GET['username'])) {
$this->addToView('username', $_GET['username']);
}
if (isset($_GET['network'])) {
$this->addToView('network', $_GET['network']);
}
return $this->generateView();
}
}
10 changes: 9 additions & 1 deletion libs/model/class.PDODAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ public final function connect(){
$timezone = $this->config->getValue('timezone');
$time = new DateTime("now", new DateTimeZone($timezone) );
$tz_offset = $time->format('P');
self::$PDO->exec("SET time_zone = '$tz_offset'");
try {
self::$PDO->exec("SET time_zone = '$timezone'");
} catch (PDOException $e) {
// Time zone couldn't be set; use offset instead, but if the timezone is one for which offset changes
// throughout year, such as during daylight saving time, dates converted from/to UTC by the database
// from a different offset will be incorrect.
self::$PDO->exec("SET time_zone = '$tz_offset'");
}

}
}

Expand Down
7 changes: 6 additions & 1 deletion libs/view/testcontroller.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{include file="_header.tpl"}

<a href="{$site_root_path}{$logo_link}">{$app_title}</a>: {$test} | {if isset($logged_in_user)}Logged in as {$logged_in_user}{else}Not logged in{/if}
<a href="{$site_root_path}{$logo_link}">{$app_title}</a>: {$test} | {if isset($logged_in_user)}Logged in as {$logged_in_user}{else}Not logged in{/if}
{if isset($username) and isset($network)}
<br><br><br>
Username:{$username}<br>
Network:{$network}
{/if}
12 changes: 6 additions & 6 deletions tests/TestOfPDODAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ public function testCompareTimezoneOffsets() {
$test_dao = new TestMySQLDAO();
$tz_server = $test_dao->getTimezoneOffset();

// if ($this->isTimeZoneSupported()) {
// $this->assertEqual('Europe/London', $tz_server['tz_offset']);
//} else {
$this->assertEqual($tz_config, $tz_server['tz_offset']);
// }
if ($this->isTimeZoneSupported()) {
$this->assertEqual('Europe/London', $tz_server['tz_offset']);
} else {
$this->assertEqual($tz_config, $tz_server['tz_offset']);
}
Config::destroyInstance();
}

Expand Down Expand Up @@ -423,6 +423,6 @@ public function testCompareMySQLAndPHPTimezoneOffsets() {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$mysql_time = $row['time'];
$php_time = strtotime('2011-09-01 00:00:00');
//$this->assertEqual($mysql_time, $php_time);
$this->assertEqual($mysql_time, $php_time);
}
}
59 changes: 59 additions & 0 deletions tests/TestOfRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
require_once dirname(__FILE__).'/init.tests.php';
require_once ISOSCELES_PATH.'extlibs/simpletest/autorun.php';
require_once ISOSCELES_PATH.'libs/config.inc.php';

class TestOfRouter extends IsoscelesBasicUnitTestCase {

public function setUp(){
parent::setUp();
}

public function tearDown(){
parent::tearDown();
}

public function testConstructor() {
$router = new Router();
$this->assertTrue(isset($router), 'constructor test');
}

public function testAddRoute() {
$router = new Router();
$router->addRoute('test', 'TestController');
$this->assertEqual(array('test'=>'TestController'), Router::$routes);
$this->assertEqual(1, sizeof(Router::$routes));

$router->addRoute('test2', 'Test2Controller');
$this->assertEqual(array('test'=>'TestController', 'test2'=>'Test2Controller'), Router::$routes);
$this->assertEqual(2, sizeof(Router::$routes));
}

public function testRouteNoParameters() {
$router = new Router();
$router->addRoute('test', 'TestController');
$router->addRoute('index', 'TestController');

$_SERVER['REQUEST_URI'] = "/";
$results = $router->route(true);
$this->assertPattern('/My Isosceles Application/', $results);


$_SERVER['REQUEST_URI'] = "/test/user/ginatrapani";
$results = $router->route(true);
$this->assertPattern('/My Isosceles Application/', $results);

$_SERVER['REQUEST_URI'] = "/nonexistent/user/ginatrapani";
$results = $router->route(true);
$this->assertPattern('/404 route not found: nonexistent/', $results);
}

public function testRouteWithParameters() {
$router = new Router();
$router->addRoute('user', 'TestController', array('username', 'network'));

$_SERVER['REQUEST_URI'] = "/user/twitter/username";
$results = $router->route(true);
$this->assertPattern('/My Isosceles Application/', $results);
}
}
2 changes: 1 addition & 1 deletion tests/TestOfViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testViewManagerDefaultValues() {
$this->assertTrue(sizeof($v_mgr->template_dir), 2);
$this->assertEqual($v_mgr->template_dir[1], '/path/to/isosceles/tests/view/');
$this->assertTrue(sizeof($v_mgr->plugins_dir), 2);
$this->assertEqual($v_mgr->plugins_dir[0], 'plugins/');
$this->assertEqual($v_mgr->plugins_dir[0], ISOSCELES_PATH.'libs/view/plugins/');
$this->assertEqual($v_mgr->cache_dir, FileDataManager::getDataPath('compiled_view/cache/'));
$this->assertEqual($v_mgr->cache_lifetime, $cfg->getValue('cache_lifetime'));
$this->assertTrue($v_mgr->caching);
Expand Down
1 change: 1 addition & 0 deletions tests/all_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

$test_suite = new TestSuite('Isosceles tests');
$test_suite->add(new TestOfConfig());
$test_suite->add(new TestOfRouter());
$test_suite->add(new TestOfDAOFactory());
$test_suite->add(new TestOfFileDataManager());
$test_suite->add(new TestOfLoader());
Expand Down
2 changes: 1 addition & 1 deletion tests/classes/class.IsoscelesUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function setUp() {
$this->table_prefix = $config->getValue('table_prefix');
PDODAO::$prefix = $this->table_prefix;

$this->testdb_helper->create($ISOSCELES_CFG['source_root_path']."install/sql/build-db_mysql.sql");
$this->testdb_helper->create(ISOSCELES_PATH."install/sql/build-db_mysql.sql");
}

/**
Expand Down
7 changes: 5 additions & 2 deletions www-example/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
require_once dirname(dirname(__FILE__)).'/libs/model/class.Loader.php';
Loader::register();
$controller = new TestController();
echo $controller->control();

$router = new Router();
$router->addRoute('test', 'TestController', array('network', 'username'));
$router->addRoute('index', 'TestController');
echo $router->route();

0 comments on commit ca62257

Please sign in to comment.