diff --git a/source/Groundhog/Router/Exception.php b/source/Groundhog/Router/Exception.php index 9f32297..5628cfd 100644 --- a/source/Groundhog/Router/Exception.php +++ b/source/Groundhog/Router/Exception.php @@ -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; - } } diff --git a/source/Groundhog/Router/ExceptionMethodNotAllowed.php b/source/Groundhog/Router/ExceptionMethodNotAllowed.php new file mode 100644 index 0000000..27b5375 --- /dev/null +++ b/source/Groundhog/Router/ExceptionMethodNotAllowed.php @@ -0,0 +1,45 @@ +allowed_methods = $allowed_methods; + } + + /** + * Get the methods that would be allowed + * + * @return array + */ + public function getAllowedMethods() + { + return $this->allowed_methods; + } +} diff --git a/source/Groundhog/Router/ExceptionNotFound.php b/source/Groundhog/Router/ExceptionNotFound.php new file mode 100644 index 0000000..97c1175 --- /dev/null +++ b/source/Groundhog/Router/ExceptionNotFound.php @@ -0,0 +1,11 @@ + 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(); } } } diff --git a/source/Groundhog/Router/RoutingTableStoreInterface.php b/source/Groundhog/Router/RoutingTableStoreInterface.php index 89034cf..3480e3b 100644 --- a/source/Groundhog/Router/RoutingTableStoreInterface.php +++ b/source/Groundhog/Router/RoutingTableStoreInterface.php @@ -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 */ @@ -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 */ diff --git a/source/Groundhog/Router/RoutingTableStoreNoCache.php b/source/Groundhog/Router/RoutingTableStoreNoCache.php index f990b62..18836c0 100644 --- a/source/Groundhog/Router/RoutingTableStoreNoCache.php +++ b/source/Groundhog/Router/RoutingTableStoreNoCache.php @@ -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(); } } } diff --git a/source/Groundhog/Router/RoutingTableStoreSqlite.php b/source/Groundhog/Router/RoutingTableStoreSqlite.php index e462aa2..5d4ab4c 100644 --- a/source/Groundhog/Router/RoutingTableStoreSqlite.php +++ b/source/Groundhog/Router/RoutingTableStoreSqlite.php @@ -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) { @@ -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;") ) { @@ -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(); } } diff --git a/tests/Groundhog/Router/Tests/ExceptionMethodNotAllowedTest.php b/tests/Groundhog/Router/Tests/ExceptionMethodNotAllowedTest.php new file mode 100644 index 0000000..825e707 --- /dev/null +++ b/tests/Groundhog/Router/Tests/ExceptionMethodNotAllowedTest.php @@ -0,0 +1,17 @@ + 'array') + ); + + $this->assertSame( array('context' => 'array'), $exception->getAllowedMethods()); + } +} diff --git a/tests/Groundhog/Router/Tests/ExceptionTest.php b/tests/Groundhog/Router/Tests/ExceptionTest.php deleted file mode 100644 index 4581b5e..0000000 --- a/tests/Groundhog/Router/Tests/ExceptionTest.php +++ /dev/null @@ -1,18 +0,0 @@ - 'array') - ); - - $this->assertSame( array('context' => 'array'), $exception->getContext()); - } -}