Skip to content
This repository has been archived by the owner on Sep 14, 2019. It is now read-only.

Commit

Permalink
Replaced the quirky exception system with a collection of multiple
Browse files Browse the repository at this point in the history
exceptions that represent the error state.
  • Loading branch information
triplepoint committed Oct 28, 2012
1 parent 3ca18b5 commit e320c5a
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 105 deletions.
31 changes: 0 additions & 31 deletions source/Groundhog/Router/Exception.php
Expand Up @@ -7,35 +7,4 @@
*/
class Exception extends \Exception
{
/**
* The Exception's stored context structure
*
* @var array
*/
protected $context;

/**
* Adds a context array to the standard exception
*
* @param string $message
* @param array $context
* @param number $code
* @param \Exception $previous
*
* @return void
*/
public function __construct($message = '', $context = array(), $code = 0, \Exception $previous = null)
{
$this->context = $context;
}

/**
* Get the stored context array
*
* @return array
*/
public function getContext()
{
return $this->context;
}
}
45 changes: 45 additions & 0 deletions source/Groundhog/Router/ExceptionMethodNotAllowed.php
@@ -0,0 +1,45 @@
<?php

namespace Groundhog\Router;

/**
* This exception represents an error where a route
* exists, but the requested method is not allowed.
* It will usually provide a set of allowed methods.
*/
class ExceptionMethodNotAllowed extends \Exception
{
/**
* The methods that would be allowed
*
* @var array
*/
protected $allowed_methods;

/**
* Adds a context array to the standard exception
*
* @param string[] $allowed_methods The set of methods that would be allowed
* @param string $message
* @param integer $code
* @param \Exception $previous
*
* @return void
*/
public function __construct(array $allowed_methods = array(), $message = '', $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);

$this->allowed_methods = $allowed_methods;
}

/**
* Get the methods that would be allowed
*
* @return array
*/
public function getAllowedMethods()
{
return $this->allowed_methods;
}
}
11 changes: 11 additions & 0 deletions source/Groundhog/Router/ExceptionNotFound.php
@@ -0,0 +1,11 @@
<?php

namespace Groundhog\Router;

/**
* This exception represents the error condition where
* a requested route is not found.
*/
class ExceptionNotFound extends \Exception
{
}
15 changes: 2 additions & 13 deletions source/Groundhog/Router/RoutingTableStoreApc.php
Expand Up @@ -111,22 +111,11 @@ public function findMatchingRoute(RequestInterface $request)
// No match found
if ( !empty($arr_allowed_methods) ) {
// If there's a match on this URL, just not for the given HTTP method, return a 405
throw new Exception(
'',
array(
'http_status_code' => 405,
'headers' => array('Allow' => implode(', ', $arr_allowed_methods))
)
);
throw new ExceptionMethodNotAllowed($arr_allowed_methods);

} else {
// Else this URL has no resource at all. Return a 404
throw new Exception(
'',
array(
'http_status_code' => 404
)
);
throw new ExceptionNotFound();
}
}
}
6 changes: 3 additions & 3 deletions source/Groundhog/Router/RoutingTableStoreInterface.php
Expand Up @@ -26,7 +26,7 @@ public function storeNeedsRebuilding();
*
* @param Groundhog\Router\Routes[] $routes an array of Route objects
*
* @throws Exception if something goes wrong
* @throws Exception if something goes wrong during the save
*
* @return void
*/
Expand All @@ -49,8 +49,8 @@ public function getRoutingTable($query_string = '');
*
* @param RequestInterface $request
*
* @throws Exception if the route is not found
* @throws Exception if the route is found but the HTTP method is not supported
* @throws ExceptionNotFound if the route is not found
* @throws ExceptionMethodNotAllowed if the route is found but the request method is not supported
*
* @return Route
*/
Expand Down
15 changes: 2 additions & 13 deletions source/Groundhog/Router/RoutingTableStoreNoCache.php
Expand Up @@ -75,22 +75,11 @@ public function findMatchingRoute(RequestInterface $request)
// No match found
if ( !empty($arr_allowed_methods) ) {
// If there's a match on this URL, just not for the given HTTP method, return a 405
throw new Exception(
'',
array(
'http_status_code' => 405,
'headers' => array('Allow' => implode(', ', $arr_allowed_methods))
)
);
throw new ExceptionMethodNotAllowed($arr_allowed_methods);

} else {
// Else this URL has no resource at all. Return a 404
throw new Exception(
'',
array(
'http_status_code' => 404
)
);
throw new ExceptionNotFound();
}
}
}
43 changes: 16 additions & 27 deletions source/Groundhog/Router/RoutingTableStoreSqlite.php
Expand Up @@ -21,9 +21,12 @@ class RoutingTableStoreSqlite implements RoutingTableStoreInterface

/**
*
* @param string $routing_table_file the path to the sqlite database file
* @param integer $ttl The cached routing table's time to live
* @param string $routing_table_file the path to the sqlite database file
* @param integer $ttl The cached routing table's time to live
*
* @throws Exception when the SQlite3 extension isn't loaded
*
* @return void
*/
public function __construct($routing_table_file, $ttl = 28800)
{
Expand Down Expand Up @@ -51,18 +54,15 @@ public function saveRoutingTable(array $routes)
$db = new SQLite3($this->routing_table_file);
$db->exec('BEGIN;');

try {
@$db->exec(
'CREATE TABLE routing_table (
route_type integer,
route_http_method text,
route_regex text,
class_name text,
parameter_order text,
raw_route_string text);'
);
} catch (\Exception $e) {
};
@$db->exec(
'CREATE TABLE routing_table (
route_type integer,
route_http_method text,
route_regex text,
class_name text,
parameter_order text,
raw_route_string text);'
);

// Remove all the auto-generated routes
if ( ! $db->exec("DELETE FROM routing_table;") ) {
Expand Down Expand Up @@ -192,22 +192,11 @@ function ($str, $regex) {

if ( !empty($arr_allowed_methods) ) {
// If there's a match on this URL, just not for the given HTTP method, return a 405
throw new Exception(
'',
array(
'http_status_code' => 405,
'headers' => array('Allow' => implode(', ', $arr_allowed_methods))
)
);
throw new ExceptionMethodNotAllowed($arr_allowed_methods);

} else {
// Else this URL has no resource at all. Return a 404
throw new Exception(
'',
array(
'http_status_code' => 404
)
);
throw new ExceptionNotFound();
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Groundhog/Router/Tests/ExceptionMethodNotAllowedTest.php
@@ -0,0 +1,17 @@
<?php

namespace Groundhog\Router\Tests;

use Groundhog\Router\ExceptionMethodNotAllowed;

class ExceptionMethodNotAllowedTest extends \PHPUnit_Framework_TestCase
{
public function testgetAllowedMethods()
{
$exception = new ExceptionMethodNotAllowed(
array('context' => 'array')
);

$this->assertSame( array('context' => 'array'), $exception->getAllowedMethods());
}
}
18 changes: 0 additions & 18 deletions tests/Groundhog/Router/Tests/ExceptionTest.php

This file was deleted.

0 comments on commit e320c5a

Please sign in to comment.